cli

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildRoot

func BuildRoot(ctx *AppContext) *cobra.Command

BuildRoot builds a root command using the global palette and provided context.

func ConfigureRoot

func ConfigureRoot(root *cobra.Command, ctx *AppContext, postInit func(*viper.Viper) error)

ConfigureRoot attaches common flags and a PersistentPreRunE to an existing root command to initialize the provided AppContext.Viper instance. If postInit is non-nil it will be invoked after the config has been read.

func Execute

func Execute(root *cobra.Command, args ...string) (string, error)

Execute runs the provided root command with args and returns captured stdout/stderr.

func ExecuteC

func ExecuteC(root *cobra.Command, args ...string) (*cobra.Command, string, error)

ExecuteC returns the executed command plus captured combined output.

func NewRoot

func NewRoot(ctx *AppContext, p Palette) *cobra.Command

NewRoot constructs the root command and composes the provided palette using the given AppContext. Config initialization is performed in PersistentPreRunE so construction is side-effect free and testable.

func Register

func Register(factories ...CommandFactory)

Register registers command factories into the global palette.

Types

type AppContext

type AppContext struct {
	Viper  *viper.Viper
	Logger Logger
	UI     Messenger
	// contains filtered or unexported fields
}

AppContext carries shared dependencies for command factories.

func NewAppContext

func NewAppContext(v *viper.Viper, l Logger) *AppContext

NewAppContext creates a default AppContext with provided logger and viper instance. Passing nil for the logger uses the standard library default logger.

func (*AppContext) AvailableTargets added in v0.5.0

func (ctx *AppContext) AvailableTargets() []string

AvailableTargets returns a list of registered transformer names.

func (*AppContext) Messenger added in v0.7.1

func (ctx *AppContext) Messenger() Messenger

Messenger returns the currently configured messenger.

func (*AppContext) RegisterTransformer added in v0.5.0

func (ctx *AppContext) RegisterTransformer(name string, t transform.Transformer)

RegisterTransformer adds a transformer to the context.

func (*AppContext) SetMessenger added in v0.7.1

func (ctx *AppContext) SetMessenger(m Messenger)

SetMessenger replaces the current messenger if a non-nil value is provided.

func (*AppContext) Transformer added in v0.5.0

func (ctx *AppContext) Transformer(target string) (transform.Transformer, error)

Transformer retrieves a transformer by name.

type CommandFactory

type CommandFactory func(*AppContext) *cobra.Command

CommandFactory builds a cobra command using the provided AppContext.

func FromCommand

func FromCommand(c *cobra.Command) CommandFactory

FromCommand wraps an existing *cobra.Command as a CommandFactory.

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
}

Logger is a minimal logging interface used by AppContext.

func NewGoBasetoolsAdapter

func NewGoBasetoolsAdapter() Logger

NewGoBasetoolsAdapter returns a Logger that routes to the go-basetools configured slog default logger. It is safe to call even before InitLogger; slog will use the standard library logger by default.

func NewReflectLoggerAdapter

func NewReflectLoggerAdapter(l interface{}) Logger

NewReflectLoggerAdapter returns a Logger that will call Printf on the provided logger value (useful to adapt third-party loggers). If l is nil, the standard library logger is used.

func NewStdLoggerAdapter

func NewStdLoggerAdapter(l *log.Logger) Logger

NewStdLoggerAdapter wraps the standard library logger as a Logger.

type Messenger added in v0.7.1

type Messenger interface {
	Success(format string, args ...interface{})
	Info(format string, args ...interface{})
	Warn(format string, args ...interface{})
	Error(format string, args ...interface{})
	Debug(format string, args ...interface{})
}

Messenger centralizes user-facing output formatting.

func NewMessenger added in v0.7.1

func NewMessenger(out, err io.Writer, level string) Messenger

NewMessenger returns a Messenger writing to the provided writers (or STDOUT/ERR if nil).

type Palette

type Palette []CommandFactory

Palette is an ordered collection of CommandFactory instances that can be composed into a root command.

var DefaultPalette Palette

Simple global palette registry for incremental migration.

func (Palette) Commands

func (p Palette) Commands(ctx *AppContext) []*cobra.Command

Commands builds concrete *cobra.Command slice using the provided context.

func (*Palette) Register

func (p *Palette) Register(factories ...CommandFactory)

Register adds factories to the palette.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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