Documentation
¶
Overview ¶
Package cmd implements command line interface commands for the application.
This file contains the implementation of the bump command and its subcommands: - bump major: Bump the major version number (X.y.z -> X+1.0.0) - bump minor: Bump the minor version number (x.Y.z -> x.Y+1.0) - bump patch: Bump the patch version number (x.y.Z -> x.y.Z+1)
These commands manage semantic versioning operations including checking for uncommitted changes, changelog updates, and git tagging.
Index ¶
- Constants
- Variables
- func EnvPrefix() string
- func Execute() error
- func MustAddToRoot(cmd *cobra.Command)
- func MustNewCommand(meta config.CommandMetadata, runE func(*cobra.Command, []string) error) *cobra.Command
- func NewCommand(meta config.CommandMetadata, runE func(*cobra.Command, []string) error) (*cobra.Command, error)
- func RegisterFlagsForPrefixWithOverrides(cmd *cobra.Command, prefix string, overrides map[string]string) error
- type ConfigPathInfo
Constants ¶
const ( // ConfigPathModeXDG searches XDG-style config directory (default). // On macOS, this means ~/.config/<app> unless XDG_CONFIG_HOME is set. ConfigPathModeXDG = "xdg" // ConfigPathModeNative searches the OS-native config directory. // On macOS, this means ~/Library/Application Support/<app>. ConfigPathModeNative = "native" // ConfigPathModeBoth searches both XDG and native directories. ConfigPathModeBoth = "both" )
Variables ¶
var ( Version = "dev" Commit = "" Date = "" )
var RootCmd = &cobra.Command{ Use: "", Short: "A production-ready Go CLI application", Long: "", SilenceErrors: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if err := bindFlags(cmd); err != nil { return fmt.Errorf("failed to bind flags: %w", err) } if outputFlag := cmd.Root().PersistentFlags().Lookup("output"); outputFlag != nil && outputFlag.Changed { output.SetOutputMode(outputFlag.Value.String()) } output.SetCommandName(cmd.Name()) if err := initConfig(); err != nil { return err } if err := logger.Init(nil); err != nil { return fmt.Errorf("failed to initialize logger: %w", err) } outputFormat := viper.GetString(config.KeyAppOutputFormat) output.SetOutputMode(outputFormat) if output.IsJSONMode() { zerolog.SetGlobalLevel(zerolog.Disabled) } if configFileStatus != "" { if configFileUsed != "" { log.Info().Str("config_file", logger.SanitizePath(configFileUsed)).Msg(configFileStatus) } else { log.Debug().Msg(configFileStatus) } } return nil }, }
Export RootCmd so that tests in other packages can manipulate it without getters/setters.
Functions ¶
func EnvPrefix ¶
func EnvPrefix() string
EnvPrefix returns a sanitized environment variable prefix based on the binary name
func MustAddToRoot ¶ added in v1.2.0
MustAddToRoot adds a command to RootCmd and sets up configuration inheritance.
This is a convenience wrapper that combines two common operations:
- Adding the command to the root command
- Setting up command configuration to inherit from parent
Usage:
func init() {
MustAddToRoot(myCmd)
}
This should be called in the init() function of your command file.
func MustNewCommand ¶ added in v1.2.0
func MustNewCommand(meta config.CommandMetadata, runE func(*cobra.Command, []string) error) *cobra.Command
MustNewCommand creates a Cobra command and panics on error.
This is a convenience wrapper for NewCommand intended for use in init() functions where there's no way to handle errors gracefully. For testable code or runtime command creation, use NewCommand instead.
Usage in init():
var myCmd = MustNewCommand(config.MyMetadata, runMy)
The runE function signature must be: func(*cobra.Command, []string) error
func NewCommand ¶ added in v1.2.0
func NewCommand(meta config.CommandMetadata, runE func(*cobra.Command, []string) error) (*cobra.Command, error)
NewCommand creates a Cobra command from metadata following ckeletin-go patterns.
This helper enforces the ultra-thin command pattern by:
- Creating the command from metadata (Use, Short, Long)
- Auto-registering flags from the config registry
- Applying custom flag overrides from metadata
Returns an error if flag registration fails, allowing callers to handle errors gracefully.
Usage:
cmd, err := NewCommand(config.MyMetadata, runMy)
if err != nil {
return err
}
For init() functions where you want to panic on error, use MustNewCommand instead.
The runE function signature must be: func(*cobra.Command, []string) error
func RegisterFlagsForPrefixWithOverrides ¶ added in v1.2.0
func RegisterFlagsForPrefixWithOverrides(cmd *cobra.Command, prefix string, overrides map[string]string) error
RegisterFlagsForPrefixWithOverrides registers Cobra flags for all configuration options whose keys start with the provided prefix. It binds each flag to Viper using the option's key. Flag names are derived from the key suffix by converting underscores to hyphens, unless an explicit override is provided in the overrides map. Returns an error if flag binding fails.
Types ¶
type ConfigPathInfo ¶ added in v1.2.0
type ConfigPathInfo struct {
// ConfigName is the base config name without extension (e.g. "config")
// Viper will search for config.yaml, config.yml, config.json, config.toml
ConfigName string
// XDGDir is the XDG-style config directory (e.g. "$XDG_CONFIG_HOME/myapp" or "~/.config/myapp")
XDGDir string
// NativeDir is the OS-native config directory (e.g. macOS "~/Library/Application Support/myapp").
NativeDir string
// Mode controls which user config directory is searched: xdg, native, or both.
Mode string
// SearchPaths lists all viper search paths in priority order.
SearchPaths []string
}
ConfigPaths returns configuration paths for the application.
Config file search order (handled by viper):
- --config flag (explicit override)
- ./config.{yaml,yml,json,toml} (project-local config)
- User config directory based on path mode: - xdg (default): $XDG_CONFIG_HOME/<binaryName> or ~/.config/<binaryName> - native: OS-native config path (macOS: ~/Library/Application Support/<binaryName>) - both: xdg first, then native
Viper automatically detects the file format based on extension.
func ConfigPaths ¶
func ConfigPaths() ConfigPathInfo