Documentation
¶
Overview ¶
Package rootcmd provides a reusable foundation for cobra-based CLI tools in the go-atlas codegen family. It handles version display (sourced from appinfo.Version), command-suggestion distance, and panic recovery with full stack traces written to stderr.
New returns a configured cobra.Command that callers can extend or execute directly. Run is a convenience wrapper that sets args, installs an additional top-level panic recovery, and calls Execute:
cfg := rootcmd.Config{
Use: "mytool",
Short: "A sample CLI tool",
Subcommands: []*cobra.Command{generateCmd, convertCmd},
}
if err := rootcmd.Run(&cfg, os.Args[1:]); err != nil {
os.Exit(1)
}
Custom io.Writer values for stdout and stderr can be supplied via [Config.Out] and [Config.Err], which is useful for capturing output in tests.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New builds and returns a root cobra.Command from cfg. It panics if cfg is nil. The returned command has SilenceUsage enabled, a version template sourced from appinfo.Version, and a PersistentPreRunE that recovers panics from any subcommand's RunE.
Types ¶
type Command ¶
Command wraps a cobra.Command with shared pre-run and post-run hooks including panic recovery.
type Config ¶
type Config struct {
Use string
Short string
Long string
Example string
Subcommands []*cobra.Command
Out io.Writer
Err io.Writer
}
Config holds the declarative specification for a root command. At minimum, Use and Short must be set. Subcommands are added in the order provided. Out and Err default to os.Stdout and os.Stderr when nil.