rootcmd

package
v0.0.0-...-0b8e112 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 7 Imported by: 0

README

rootcmd

import "github.com/altessa-s/go-atlas/tools/codegen/shared/rootcmd"

Package rootcmd provides a reusable foundation for cobra-based CLI tools in the go-atlas codegen family. Handles version display sourced from appinfo.Version, command-suggestion distance, and panic recovery with full stack traces written to stderr.

Functions

Function Description
New Returns a configured *cobra.Command from a Config; callers extend or execute it directly
Run Convenience wrapper that sets args, installs panic recovery, and calls Execute

Config fields

Field Description
Use Command name (e.g. "optgen", "goconfig")
Short One-line description shown in help output
Long Multi-line description shown in detailed help
Example Usage examples shown in help output
Subcommands Slice of *cobra.Command to register as subcommands
Out Custom io.Writer for stdout (useful for tests; defaults to os.Stdout)
Err Custom io.Writer for stderr (useful for tests; defaults to os.Stderr)

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

func New(cfg *Config) *cobra.Command

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.

func Run

func Run(cfg *Config, args []string) (err error)

Run creates a root command from cfg, sets its args, and executes it. A top-level deferred recover wraps the entire Execute call, so panics that escape PersistentPreRunE (e.g., in a subcommand's RunE) are still caught, logged to stderr, and returned as an error.

Types

type Command

type Command struct {
	*cobra.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.

Jump to

Keyboard shortcuts

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