command

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package command provides CLI command implementations and helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RootCommandToCobra

func RootCommandToCobra(root Command) (*cobra.Command, cenclierrors.CencliError)

RootCommandToCobra is essentially toCobra(), with different naming to prevent non-root commands from using it. Useful for building the top-level command and for testing.

Types

type ArgCountError

type ArgCountError interface {
	cenclierrors.CencliError
}

func NewArgCountError

func NewArgCountError(err error) ArgCountError

type BaseCommand

type BaseCommand struct {
	*Context
	// contains filtered or unexported fields
}

BaseCommand is what each Command implementation must embed. This allows new Commands to not have to worry about dependency injection. BaseCommand intentionally does not implement the Command interface, to "force" subcommands to implement required methods.

func NewBaseCommand

func NewBaseCommand(cmdContext *Context) *BaseCommand

func (*BaseCommand) AddSubCommands

func (b *BaseCommand) AddSubCommands(cmds ...Command) error

func (*BaseCommand) Examples

func (b *BaseCommand) Examples() []string

func (*BaseCommand) Flags

func (b *BaseCommand) Flags() *pflag.FlagSet

func (*BaseCommand) HelpFunc

func (b *BaseCommand) HelpFunc(cmd *cobra.Command, examples []string)

func (*BaseCommand) Init

func (b *BaseCommand) Init() error

func (*BaseCommand) Long

func (b *BaseCommand) Long() string

func (*BaseCommand) PersistentFlags

func (b *BaseCommand) PersistentFlags() *pflag.FlagSet

func (*BaseCommand) PostRun

func (b *BaseCommand) PostRun(cmd *cobra.Command, args []string) cenclierrors.CencliError

func (*BaseCommand) UsageFunc

func (b *BaseCommand) UsageFunc(cmd *cobra.Command, examples []string)

type Command

type Command interface {
	// AddSubCommand adds one or more subcommands to the command.
	// Should not be implemented.
	AddSubCommands(cmds ...Command) error
	// Use returns the name of the command as it will be used in the CLI.
	// Must be implemented.
	Use() string
	// Short returns the short description of the command.
	// Must be implemented.
	Short() string
	// Long returns the long description of the command.
	// Not required to implement.
	Long() string
	// Examples returns the examples for the command.
	// Not required to implement.
	Examples() []string
	// Args returns the positional argument function for the command.
	// Must be implemented.
	Args() PositionalArgs
	// PreRun executes before the main command logic.
	// Must be implemented, since many commands can benefit from it.
	// If you really don't need it, just return nil.
	PreRun(cmd *cobra.Command, args []string) cenclierrors.CencliError
	// Run executes the main command logic.
	// Must be implemented.
	Run(cmd *cobra.Command, args []string) cenclierrors.CencliError
	// PostRun executes after the main command logic.
	// Not required to implement.
	PostRun(cmd *cobra.Command, args []string) cenclierrors.CencliError
	// HelpFunc allows the command to customize the help function.
	// Be careful with this, as it will override the default help function,
	// which will contain all necessary information by default.
	// Not required to implement.
	HelpFunc(cmd *cobra.Command, examples []string)
	// UsageFunc defines the usage function for the command.
	// Not required to implement. Allows customization of the usage output for the command.
	UsageFunc(cmd *cobra.Command, examples []string)
	// Init will run before the underlying cobra command is initialized.
	// This can be useful for binding persistent flags, etc.
	// You should NOT modify the underlying cobra command in this function.
	// If you need to, the Command interface should be expanded to accomplish
	// what you are trying to do.
	// Not required to implement.
	Init() error
	// Flags returns the underlying flag set for the command.
	// Used for modifying a command's flags.
	// Should not be implemented.
	Flags() *pflag.FlagSet
	// PersistentFlags returns the underlying persistent flag set for the command.
	// Used for modifying persistent flags on the command.
	// Should not be implemented.
	PersistentFlags() *pflag.FlagSet
	// contains filtered or unexported methods
}

Command is an interface that all CLI commands must implement. This allows new Commands to not have to worry about cobra specifics. Everything that implements Command MUST embed BaseCommand.

type Context

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

Context is the set of dependencies that are injected into each command.

func NewCommandContext

func NewCommandContext(
	cfg *config.Config,
	st store.Store,
	opts ...ContextOpts,
) *Context

func (*Context) AggregateService

func (c *Context) AggregateService() (aggregate.Service, cenclierrors.CencliError)

AggregateService attempts to provide a AggregateService to the caller. If it is not already set and is unable to be instantiated, it will return an error.

func (*Context) CenseyeService

func (c *Context) CenseyeService() (censeye.Service, cenclierrors.CencliError)

CenseyeService attempts to provide a CenseyeService to the caller. If it is not already set and is unable to be instantiated, it will return an error.

func (*Context) Config

func (c *Context) Config() *config.Config

func (*Context) HistoryService

func (c *Context) HistoryService() (history.Service, cenclierrors.CencliError)

HistoryService attempts to provide a HistoryService to the caller. If it is not already set and is unable to be instantiated, it will return an error.

func (*Context) Logger

func (c *Context) Logger(cmdName string) *slog.Logger

Logger returns a logger pre-populated with the command name field.

func (*Context) PrintAppResponseMeta

func (c *Context) PrintAppResponseMeta(meta *responsemeta.ResponseMeta)

PrintAppResponseMeta renders application-level response metadata to stderr. If the quiet flag is set, this is a no-op. If the debug flag is set, this will also print the headers.

func (*Context) PrintData

func (c *Context) PrintData(data any) cenclierrors.CencliError

PrintData renders data according to the configured output format.

func (*Context) PrintDataWithTemplate

func (c *Context) PrintDataWithTemplate(entity config.TemplateEntity, data any) cenclierrors.CencliError

PrintDataWithTemplate renders data through a template and writes the result to stdout.

func (*Context) PrintYAML

func (c *Context) PrintYAML(data any) cenclierrors.CencliError

PrintYAML renders data as YAML.

func (*Context) SearchService

func (c *Context) SearchService() (search.Service, cenclierrors.CencliError)

SearchService attempts to provide a SearchService to the caller. If it is not already set and is unable to be instantiated, it will return an error.

func (*Context) SetCensysClient

func (c *Context) SetCensysClient(cli client.Client)

SetClient sets the Context's client so that it can be used to initialize services.

func (*Context) SetLogger

func (c *Context) SetLogger(l *slog.Logger)

SetLogger sets the logger used by commands created with this context.

func (*Context) Store

func (c *Context) Store() store.Store

func (*Context) ViewService

func (c *Context) ViewService() (view.Service, cenclierrors.CencliError)

ViewService attempts to provide a ViewService to the caller. If it is not already set and is unable to be instantiated, it will return an error.

func (*Context) WithProgress

func (c *Context) WithProgress(
	ctx context.Context,
	logger *slog.Logger,
	initialMessage string,
	fn func(context.Context) cenclierrors.CencliError,
) cenclierrors.CencliError

WithProgress executes fn with progress reporting enabled. Progress events from the application layer are displayed via spinner (if enabled) and logged at debug level with the provided logger.

Parameters:

  • ctx: The context to enhance with progress reporting
  • logger: Logger that will receive progress events (inherits command context fields)
  • initialMessage: Message to display when progress starts (e.g. "Fetching data...")
  • fn: Function to execute with progress-enabled context

The function ensures the progress display is properly stopped even if fn panics or returns early.

type ContextOpts

type ContextOpts func(*Context)

ContextOpts are functional options for configuring Context

func WithAggregateService

func WithAggregateService(svc aggregate.Service) ContextOpts

WithAggregateService injects an instantiated AggregateService to the Context. This should only be used in tests, as in the application, the AggregateService will be instantiated on demand.

func WithCenseyeService

func WithCenseyeService(svc censeye.Service) ContextOpts

WithCenseyeService injects an instantiated CenseyeService to the Context. This should only be used in tests; in the app the service is instantiated on demand.

func WithHistoryService

func WithHistoryService(svc history.Service) ContextOpts

WithHistoryService injects an instantiated HistoryService to the Context. This should only be used in tests; in the app the service is instantiated on demand.

func WithSearchService

func WithSearchService(svc search.Service) ContextOpts

WithSearchService injects an instantiated SearchService to the Context. This should only be used in tests, as in the application, the SearchService will be instantiated on demand.

func WithViewService

func WithViewService(svc view.Service) ContextOpts

WithViewService injects an instantiated ViewService to the Context. This should only be used in tests, as in the application, the ViewService will be instantiated on demand.

type PositionalArgs

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

Essentially the same as cobra.PositionalArgs, but with its own error type.

func ExactArgs

func ExactArgs(n int) PositionalArgs

func RangeArgs

func RangeArgs(min, max int) PositionalArgs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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