cmdutil

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ModeDefault       = ""
	ModeTable         = "table"
	ModeJSON          = "json"
	ModeTemplate      = "template"
	ModeTableTemplate = "table-template"
)

Format mode constants for --format flag parsing.

Variables

View Source
var ErrAborted = errors.New("operation aborted by user")

ErrAborted is returned when user cancels an operation.

View Source
var SilentError = errors.New("SilentError")

SilentError signals that the error has already been displayed to the user. Main() will exit non-zero but not print anything additional.

Functions

func AgentArgsValidator

func AgentArgsValidator(minArgs int) cobra.PositionalArgs

AgentArgsValidator creates a Cobra Args validator for commands with --agent flag. When --agent is provided, no positional arguments are allowed (mutually exclusive). When --agent is not provided, at least minArgs positional arguments are required.

func AgentArgsValidatorExact

func AgentArgsValidatorExact(n int) cobra.PositionalArgs

AgentArgsValidatorExact creates a Cobra Args validator for commands with --agent flag that require exactly N positional arguments when --agent is not provided.

func DefaultFuncMap

func DefaultFuncMap() template.FuncMap

DefaultFuncMap returns the default template function map for --format templates. These functions are Docker CLI-compatible and available in all Go templates used by clawker commands.

func ExactArgs

func ExactArgs(number int) cobra.PositionalArgs

ExactArgs returns an error if there is not the exact number of args

func ExecuteTemplate

func ExecuteTemplate(w io.Writer, f Format, items []any) error

ExecuteTemplate parses and executes a Go template from the given Format for each item, writing results to w. For table-template formats, output is aligned through a tabwriter. Each item produces one line of output.

func FlagErrorWrap

func FlagErrorWrap(err error) error

FlagErrorWrap wraps an existing error as a FlagError.

func FlagErrorf

func FlagErrorf(format string, args ...any) error

FlagErrorf creates a FlagError with a formatted message.

func HandleError deprecated

func HandleError(ios *iostreams.IOStreams, err error)

Deprecated: Use fmt.Fprintf(ios.ErrOut, ...) with ios.ColorScheme() directly. Errors should be returned to Main() for centralized rendering via printError. This function will be removed once all commands are migrated.

func NoArgs

func NoArgs(cmd *cobra.Command, args []string) error

NoArgs validates args and returns an error if there are any args

func OutputJSON deprecated

func OutputJSON(ios *iostreams.IOStreams, data any) error

Deprecated: Inline the JSON encoding in the command's run function:

enc := json.NewEncoder(ios.Out)
enc.SetIndent("", "  ")
return enc.Encode(data)

This function will be removed once all commands are migrated.

func PrintErrorf deprecated added in v0.1.7

func PrintErrorf(ios *iostreams.IOStreams, format string, args ...any)

Deprecated: Use fmt.Fprintf(ios.ErrOut, "Error: "+format+"\n", args...) directly. Errors should be returned to Main() for centralized rendering. This function will be removed once all commands are migrated.

func PrintHelpHint deprecated

func PrintHelpHint(ios *iostreams.IOStreams, cmdPath string)

Deprecated: Inline the fprintf in the caller:

fmt.Fprintf(ios.ErrOut, "\nRun '%s --help' for more information.\n", cmdPath)

This function will be removed once all commands are migrated.

func PrintNextSteps deprecated

func PrintNextSteps(ios *iostreams.IOStreams, steps ...string)

Deprecated: Inline the next-steps output in the command's run function. Use fmt.Fprintf(ios.ErrOut, ...) with ios.ColorScheme() directly. This function will be removed once all commands are migrated.

func RequiresMaxArgs

func RequiresMaxArgs(maxArgs int) cobra.PositionalArgs

RequiresMaxArgs returns an error if there is not at most max args

func RequiresMinArgs

func RequiresMinArgs(minArgs int) cobra.PositionalArgs

RequiresMinArgs returns an error if there is not at least min args

func RequiresRangeArgs

func RequiresRangeArgs(minArgs int, maxArgs int) cobra.PositionalArgs

RequiresRangeArgs returns an error if there is not at least min args and at most max args

func ToAny

func ToAny[T any](items []T) []any

ToAny converts a typed slice to []any for use with ExecuteTemplate.

func ValidateFilterKeys

func ValidateFilterKeys(filters []Filter, validKeys []string) error

ValidateFilterKeys checks that every filter's key is in validKeys. Returns a FlagError listing valid keys if an unknown key is found.

func WriteJSON

func WriteJSON(w io.Writer, data any) error

WriteJSON encodes data as pretty-printed JSON to the given writer. Used by list commands when --format json or --json is specified.

Types

type ExitError

type ExitError struct {
	Code int
}

ExitError represents a container exit with a non-zero status code. Commands should return this instead of calling os.Exit() directly, allowing deferred cleanup to run. The root command handles os.Exit().

func (*ExitError) Error

func (e *ExitError) Error() string

type Factory

type Factory struct {
	// Eager (set at construction)
	Version   string
	IOStreams *iostreams.IOStreams
	TUI       *tui.TUI

	// Lazy nouns
	Client         func(context.Context) (*docker.Client, error)
	Config         func() (config.Config, error)
	Logger         func() (*logger.Logger, error)
	ProjectManager func() (project.ProjectManager, error)
	GitManager     func() (*git.GitManager, error)
	HostProxy      func() hostproxy.HostProxyService
	SocketBridge   func() socketbridge.SocketBridgeManager
	Prompter       func() *prompter.Prompter
	Firewall       func(context.Context) (firewall.FirewallManager, error)
}

Factory provides shared dependencies for CLI commands. It is a dependency injection container: the struct defines what dependencies exist (the contract), while internal/cmd/factory wires the real implementations.

Fields are either eager (set at construction) or lazy nouns (closures that return cached instances on first call). Commands extract only the fields they need into per-command Options structs.

type Filter

type Filter struct {
	Key   string
	Value string
}

Filter is a parsed filter key-value pair from --filter flags.

func ParseFilters

func ParseFilters(raw []string) ([]Filter, error)

ParseFilters parses raw --filter flag values into Filter structs. Each string must be "key=value" format, split on the first "=" only (the value may contain additional "=" characters). The key must not be empty, but the value may be empty.

type FilterFlags

type FilterFlags struct {
	// contains filtered or unexported fields
}

FilterFlags holds raw --filter flag values and provides parsing.

func AddFilterFlags

func AddFilterFlags(cmd *cobra.Command) *FilterFlags

AddFilterFlags registers a repeatable --filter flag on cmd and returns the FilterFlags receiver for later parsing.

func (*FilterFlags) Parse

func (ff *FilterFlags) Parse() ([]Filter, error)

Parse parses the raw filter values collected from the command flags.

type FlagError

type FlagError struct {
	// contains filtered or unexported fields
}

FlagError indicates bad flags or arguments. When Main() encounters this error type, it prints the error message followed by the command's usage string.

func (*FlagError) Error

func (e *FlagError) Error() string

func (*FlagError) Unwrap

func (e *FlagError) Unwrap() error

type Format

type Format struct {
	// contains filtered or unexported fields
}

Format is a parsed format specification from the --format flag.

func ParseFormat

func ParseFormat(raw string) (Format, error)

ParseFormat parses a raw --format flag value into a Format.

Recognized inputs:

  • "" → ModeDefault
  • "table" → ModeTable
  • "json" → ModeJSON
  • "table {{.Name}}\t{{.ID}}" → ModeTableTemplate (prefix "table ")
  • "{{.Name}} {{.ID}}" → ModeTemplate (contains "{{")
  • anything else → FlagError

func (Format) IsDefault

func (f Format) IsDefault() bool

IsDefault reports whether the format is the default table output.

func (Format) IsJSON

func (f Format) IsJSON() bool

IsJSON reports whether the format is JSON output.

func (Format) IsTableTemplate

func (f Format) IsTableTemplate() bool

IsTableTemplate reports whether the format is a table with a Go template.

func (Format) IsTemplate

func (f Format) IsTemplate() bool

IsTemplate reports whether the format uses a Go template (plain or table).

func (Format) Template

func (f Format) Template() string

Template returns the Go template string, or "" if not a template format.

type FormatFlags

type FormatFlags struct {
	Format Format
	Quiet  bool
}

FormatFlags holds parsed state for --format, --json, and --quiet flags.

func AddFormatFlags

func AddFormatFlags(cmd *cobra.Command) *FormatFlags

AddFormatFlags registers --format, --json, and -q/--quiet flags on the command and chains PreRunE validation for mutual exclusivity.

The returned FormatFlags is populated during PreRunE; commands read it in RunE after flag parsing is complete.

func (*FormatFlags) IsDefault

func (ff *FormatFlags) IsDefault() bool

IsDefault reports whether the format is the default table output.

func (*FormatFlags) IsJSON

func (ff *FormatFlags) IsJSON() bool

IsJSON reports whether the format is JSON output.

func (*FormatFlags) IsTableTemplate

func (ff *FormatFlags) IsTableTemplate() bool

IsTableTemplate reports whether the format is a table with a Go template.

func (*FormatFlags) IsTemplate

func (ff *FormatFlags) IsTemplate() bool

IsTemplate reports whether the format uses a Go template.

func (*FormatFlags) Template

func (ff *FormatFlags) Template() Format

Template returns the underlying Format value (for passing to ExecuteTemplate).

type WorktreeSpec

type WorktreeSpec struct {
	Branch string // Branch name to use/create
	Base   string // Base branch (empty if not specified)
}

WorktreeSpec holds the parsed --worktree flag value.

func ParseWorktreeFlag

func ParseWorktreeFlag(value, agentName string) (*WorktreeSpec, error)

ParseWorktreeFlag parses the --worktree flag value.

Syntax:

  • Empty string: auto-generate branch name
  • "branch": use existing or create from HEAD
  • "branch:base": create branch from specified base

Returns WorktreeSpec with Branch and Base fields. Returns error if branch name is invalid (contains shell metacharacters).

Jump to

Keyboard shortcuts

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