appcmd

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	Name        string
	Description string
	Version     string
	BuildDate   string
	BuildCommit string
	CmdList     ICommands

	BeforeCommandRun BeforeCommandRunFunc
}

App is the root application that dispatches CLI arguments to commands.

func (*App) PrintUsage

func (app *App) PrintUsage(w io.Writer)

PrintUsage writes the full application usage to w.

func (*App) Run

func (app *App) Run(ctx context.Context, args []string) error

Run dispatches the command identified by args[1], handling built-in flags --version/-v, --help/-h, and the special "help [command]" sub-command before reaching user-defined commands.

type BeforeCommandRunFunc

type BeforeCommandRunFunc func(ctx context.Context, cmd ICommand) (context.Context, error)

BeforeCommandRunFunc is called once before a command's Run, allowing the caller to enrich or validate the context (logging, tracing, etc.).

type Command

type Command[T any] struct {
	Name        string
	HelpDesc    string
	Exec        CommandFunc[T]
	ContextInit ContextInitFunc
}

Command is a generic command with a typed configuration struct T. The configuration is loaded from defaults, environment variables, and CLI flags using the goconfig library, following the struct tags:

  • default:"…" — default value
  • env:"…" — environment variable name
  • envPrefix:"…" — prefix applied to all env tags in nested structs
  • cli:"…" — long CLI flag (without "--")
  • json:"…" — display name fallback
  • field:"…" — display name (highest priority)

func (*Command[T]) Cmd

func (c *Command[T]) Cmd() string

func (*Command[T]) Help

func (c *Command[T]) Help() string

func (*Command[T]) PrintHelp

func (c *Command[T]) PrintHelp(w io.Writer)

PrintHelp writes full command help including the configuration options table to w.

func (*Command[T]) Run

func (c *Command[T]) Run(ctx context.Context, args []string) error

Run executes the command. It intercepts --help / -h / help before loading config so users can request help even with missing required env vars. Scanning stops at "--" so downstream flags are not misinterpreted.

func (*Command[T]) String

func (c *Command[T]) String() string

func (*Command[T]) WithInitContext

func (c *Command[T]) WithInitContext(ctxWrapper ContextInitFunc) *Command[T]

WithInitContext returns a shallow copy of the command with the given context initializer.

type CommandFunc

type CommandFunc[T any] func(ctx context.Context, args []string, config *T) error

CommandFunc is the execution function for a typed command.

type ContextInitFunc

type ContextInitFunc func(ctx context.Context) (context.Context, error)

ContextInitFunc initializes or enriches the context before command execution.

type HelpProvider

type HelpProvider interface {
	PrintHelp(w io.Writer)
}

HelpProvider is an optional interface commands can implement to print detailed help that includes the configuration options table.

type ICommand

type ICommand interface {
	String() string
	Cmd() string
	Help() string
	Run(ctx context.Context, args []string) error
}

ICommand is the interface all commands must implement.

type ICommands

type ICommands []ICommand

ICommands is a list of commands

func (ICommands) Get

func (c ICommands) Get(name string) ICommand

Get returns a command by name

Jump to

Keyboard shortcuts

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