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
- Variables
- func DetermineExitCode(err error) int
- func ExitWithCode(code int)
- func FormatExamples(examples string) string
- func GetFlagObjectsByCategory(cmd *cobra.Command) map[string][]*pflag.Flag
- func GetFlagsByCategory(cmd *cobra.Command) map[string][]string
- func GetRootCmd() *cobra.Command
- func GetSuggestions(cmd *cobra.Command, arg string) []string
- func InitHelp(cmd *cobra.Command)
- func JSONSuccess(message, file string)
- func OutputJSONError(err error, file string, exitCode int)
- func SetCommandContext(cmd *cobra.Command, cmdCtx *CommandContext)
- func ValidAuditModes(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)
- func ValidAuditPlugins(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)
- func ValidColorModes(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)
- func ValidFormats(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)
- func ValidSections(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)
- func ValidThemes(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective)
- func ValidXMLFiles(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective)
- type CommandContext
- type ConfigShowOutput
- type ConfigValue
- type JSONError
Constants ¶
const ( FormatMarkdown = "markdown" FormatJSON = "json" FormatYAML = "yaml" )
Format constants for output formats.
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.
const ( MinWrapWidth = 40 // Minimum recommended wrap width MaxWrapWidth = 200 // Maximum recommended wrap width )
Constants for flag validation.
const ExitConfigValidationError = 5
ExitConfigValidationError is the exit code for configuration validation errors.
Variables ¶
var ( ErrFailedToEnrichConfig = errors.New("failed to enrich configuration") ErrUnsupportedOutputFormat = errors.New("unsupported output format") )
Static errors for better error handling.
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
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
FormatExamples formats command examples for display. It ensures consistent indentation and formatting.
func GetFlagObjectsByCategory ¶ added in v1.1.0
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 ¶
GetFlagsByCategory returns flags grouped by their category annotation. This demonstrates how flag annotations can be used for programmatic flag management.
func GetRootCmd ¶
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
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
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
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
ValidAuditModes provides shell completion for audit mode values.
func ValidAuditPlugins ¶ added in v1.1.0
ValidAuditPlugins provides shell completion for audit plugin values.
func ValidColorModes ¶ added in v1.1.0
ValidColorModes provides shell completion for color mode values.
func ValidFormats ¶ added in v1.1.0
ValidFormats provides shell completion for output format values.
func ValidSections ¶ added in v1.1.0
ValidSections provides shell completion for section filter values.
func ValidThemes ¶ added in v1.1.0
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.