cmd

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package cmd implements the CLI commands using Cobra.

Global variables are used for flag binding (a Cobra convention), but runtime state is passed through CommandContext via Cobra's context mechanism. This enables isolated testing and cleaner dependency injection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute() error

Execute adds all child commands to the root command and sets flags appropriately.

func ExitCode added in v0.6.0

func ExitCode(executionError error) int

ExitCode maps execution errors to process exit codes.

func IsQuietIntent added in v0.7.0

func IsQuietIntent() bool

IsQuietIntent reports whether quiet mode was requested via CLI flags.

func ResetQuietIntentForTests added in v0.7.0

func ResetQuietIntentForTests()

ResetQuietIntentForTests resets the quiet-intent flag for test isolation.

func SetQuietIntent added in v0.7.0

func SetQuietIntent(v bool)

SetQuietIntent records that quiet mode was requested.

func SetVersionInfo

func SetVersionInfo(v, c, d string)

SetVersionInfo sets version information from main

func WithCommandContext added in v0.6.0

func WithCommandContext(ctx context.Context, cmdCtx *CommandContext) context.Context

WithCommandContext returns a new context with the CommandContext stored in it.

Types

type CommandContext added in v0.6.0

type CommandContext struct {
	// Runtime state (set during PersistentPreRunE)
	Config  *config.Config
	Manager *container.Manager

	// Flags - these are populated from the global flag variables after parsing.
	// While Cobra requires global variables for flag binding, we copy values
	// here for cleaner access and to enable test isolation.
	Flags CommandFlags
}

CommandContext holds all state for command execution.

Usage in commands:

RunE: func(cmd *cobra.Command, args []string) error {
    cmdCtx := GetCommandContext(cmd.Context())
    return cmdCtx.Manager.SomeMethod()
}

This replaces global variables to enable: - Concurrent command testing - Isolated unit tests - Clean test state - Dependency injection

func GetCommandContext added in v0.6.0

func GetCommandContext(ctx context.Context) *CommandContext

GetCommandContext retrieves the CommandContext from a context. Returns nil if no CommandContext is stored.

func NewCommandContext added in v0.6.0

func NewCommandContext() *CommandContext

NewCommandContext creates a fresh CommandContext for testing or execution.

type CommandFlags added in v0.6.0

type CommandFlags struct {
	CfgFile   string
	Memory    string
	CPUs      int
	Name      string
	Packages  string
	CfInclude string
}

CommandFlags holds all flag values for commands. These are copied from the global flag variables during initialization.

Jump to

Keyboard shortcuts

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