cmd

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Overview

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Package cmd provides the command-line interface for opnDossier.

Index

Constants

View Source
const (
	FormatMarkdown = "markdown"
	FormatJSON     = "json"
	FormatYAML     = "yaml"
)

Format constants for output formats.

View Source
const (
	// ExitSuccess indicates successful execution.
	ExitSuccess = 0

	// ExitGeneralError indicates a general/unknown error.
	ExitGeneralError = 1

	// ExitParseError indicates an XML parsing error.
	ExitParseError = 2

	// ExitValidationError indicates a configuration validation error.
	ExitValidationError = 3

	// ExitFileError indicates a file I/O error (file not found, permission denied, etc.).
	ExitFileError = 4
)

Exit codes for structured error handling in CI/CD pipelines. These codes allow automation to distinguish between different error types.

View Source
const (
	MinWrapWidth = 40  // Minimum recommended wrap width
	MaxWrapWidth = 200 // Maximum recommended wrap width
)

Constants for flag validation.

View Source
const ExitConfigValidationError = 5

ExitConfigValidationError is the exit code for configuration validation errors.

Variables

View Source
var (
	ErrFailedToEnrichConfig    = errors.New("failed to enrich configuration")
	ErrUnsupportedOutputFormat = errors.New("unsupported output format")
)

Static errors for better error handling.

View Source
var ErrOperationCancelled = errors.New("operation cancelled by user")

ErrOperationCancelled is returned when the user cancels an operation.

Functions

func DetermineExitCode added in v1.1.0

func DetermineExitCode(err error) int

DetermineExitCode returns the appropriate exit code based on the error type.

func ExitWithCode added in v1.1.0

func ExitWithCode(code int)

ExitWithCode exits the program with the specified exit code. This function should be used instead of os.Exit to ensure proper cleanup.

func FormatExamples added in v1.1.0

func FormatExamples(examples string) string

FormatExamples formats command examples for display. It ensures consistent indentation and formatting.

func GetFlagObjectsByCategory added in v1.1.0

func GetFlagObjectsByCategory(cmd *cobra.Command) map[string][]*pflag.Flag

GetFlagObjectsByCategory returns flag objects grouped by their category annotation. This is useful for organizing help output with full flag metadata. Unlike GetFlagsByCategory in root.go which returns flag names as strings, this function returns the actual pflag.Flag objects for richer help formatting.

func GetFlagsByCategory

func GetFlagsByCategory(cmd *cobra.Command) map[string][]string

GetFlagsByCategory returns flags grouped by their category annotation. This demonstrates how flag annotations can be used for programmatic flag management.

func GetRootCmd

func GetRootCmd() *cobra.Command

GetRootCmd returns the root Cobra command for the opnDossier CLI application. This provides access to the application's main command and its subcommands for integration or extension.

func GetSuggestions added in v1.1.0

func GetSuggestions(cmd *cobra.Command, arg string) []string

GetSuggestions returns suggested commands for a given invalid input. It uses Levenshtein distance to find similar command names.

func InitHelp added in v1.1.0

func InitHelp(cmd *cobra.Command)

InitHelp configures the root command with enhanced help features. This should be called after all commands are registered.

func JSONSuccess added in v1.1.0

func JSONSuccess(message, file string)

JSONSuccess outputs a success message in JSON format to stdout. This is used when --json-output flag is enabled for machine consumption.

func OutputJSONError added in v1.1.0

func OutputJSONError(err error, file string, exitCode int)

OutputJSONError outputs an error in JSON format to stderr. This is used when --json-output flag is enabled for machine consumption.

func SetCommandContext added in v1.1.0

func SetCommandContext(cmd *cobra.Command, cmdCtx *CommandContext)

SetCommandContext stores the CommandContext in the command's context. If the command's context is nil, a new background context is created.

This should be called in PersistentPreRunE of the root command to make the context available to all subcommands.

func ValidAuditModes added in v1.1.0

func ValidAuditModes(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)

ValidAuditModes provides shell completion for audit mode values.

func ValidAuditPlugins added in v1.1.0

func ValidAuditPlugins(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)

ValidAuditPlugins provides shell completion for audit plugin values.

func ValidColorModes added in v1.1.0

func ValidColorModes(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)

ValidColorModes provides shell completion for color mode values.

func ValidFormats added in v1.1.0

func ValidFormats(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)

ValidFormats provides shell completion for output format values.

func ValidSections added in v1.1.0

func ValidSections(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)

ValidSections provides shell completion for section filter values.

func ValidThemes added in v1.1.0

func ValidThemes(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)

ValidThemes provides shell completion for theme values.

func ValidXMLFiles added in v1.1.0

func ValidXMLFiles(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective)

ValidXMLFiles provides shell completion for XML configuration files. It returns a list of .xml files in the current directory and subdirectories, along with a completion directive for file completion.

Types

type CommandContext added in v1.1.0

type CommandContext struct {
	// Config holds the application's configuration loaded from file, environment, or flags.
	Config *config.Config

	// Logger is the application's structured logger instance.
	Logger *log.Logger
}

CommandContext encapsulates shared state for all CLI commands. It is set on the cobra.Command context during PersistentPreRunE and provides explicit dependency injection for configuration and logging.

This pattern replaces direct access to package-level globals, making dependencies explicit and testing easier.

func GetCommandContext added in v1.1.0

func GetCommandContext(cmd *cobra.Command) *CommandContext

GetCommandContext retrieves the CommandContext from a cobra.Command's context. Returns nil if the context is not set or does not contain a CommandContext.

Example usage in a command's RunE function:

func runMyCommand(cmd *cobra.Command, args []string) error {
    cmdCtx := GetCommandContext(cmd)
    if cmdCtx == nil {
        return errors.New("command context not initialized")
    }
    cmdCtx.Logger.Info("running command")
    // use cmdCtx.Config, cmdCtx.Logger
}

func MustGetCommandContext added in v1.1.0

func MustGetCommandContext(cmd *cobra.Command) *CommandContext

MustGetCommandContext retrieves the CommandContext from a cobra.Command's context and panics if it is not found. Use this only when you are certain the context has been set (e.g., after PersistentPreRunE has run).

For most cases, prefer GetCommandContext and handle the nil case explicitly.

type ConfigShowOutput added in v1.1.0

type ConfigShowOutput struct {
	Values []ConfigValue `json:"values"`
}

ConfigShowOutput represents the full configuration output for JSON format.

type ConfigValue added in v1.1.0

type ConfigValue struct {
	Key    string `json:"key"`
	Value  any    `json:"value"`
	Source string `json:"source"`
}

ConfigValue represents a configuration value with its source.

type JSONError added in v1.1.0

type JSONError struct {
	Error   string         `json:"error"`
	Code    int            `json:"code"`
	Type    string         `json:"type"`
	File    string         `json:"file,omitempty"`
	Details map[string]any `json:"details,omitempty"`
}

JSONError represents a machine-readable error output.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL