Documentation
¶
Overview ¶
Package command provides CLI command implementations and helpers.
Index ¶
- func RootCommandToCobra(root Command) (*cobra.Command, cenclierrors.CencliError)
- type ArgCountError
- type BaseCommand
- func (b *BaseCommand) AddSubCommands(cmds ...Command) error
- func (b *BaseCommand) DefaultOutputType() OutputType
- func (b *BaseCommand) Examples() []string
- func (b *BaseCommand) Flags() *pflag.FlagSet
- func (b *BaseCommand) HelpFunc(cmd *cobra.Command, examples []string)
- func (b *BaseCommand) InheritedFlags() *pflag.FlagSet
- func (b *BaseCommand) Init() error
- func (b *BaseCommand) Long() string
- func (b *BaseCommand) PersistentFlags() *pflag.FlagSet
- func (b *BaseCommand) PostRun(cmd *cobra.Command, args []string) cenclierrors.CencliError
- func (b *BaseCommand) RenderShort() cenclierrors.CencliError
- func (b *BaseCommand) RenderTemplate() cenclierrors.CencliError
- func (b *BaseCommand) SupportedOutputTypes() []OutputType
- func (b *BaseCommand) SupportsStreaming() bool
- func (b *BaseCommand) UsageFunc(cmd *cobra.Command, examples []string)
- type Command
- type Context
- func (c *Context) AggregateService() (aggregate.Service, cenclierrors.CencliError)
- func (c *Context) CenseyeService() (censeye.Service, cenclierrors.CencliError)
- func (c *Context) Config() *config.Config
- func (c *Context) CreditsService() (credits.Service, cenclierrors.CencliError)
- func (c *Context) GetStoredOrgID(ctx context.Context) (mo.Option[identifiers.OrganizationID], cenclierrors.CencliError)
- func (c *Context) HasOrgID() bool
- func (c *Context) HistoryService() (history.Service, cenclierrors.CencliError)
- func (c *Context) Logger(cmdName string) *slog.Logger
- func (c *Context) OrganizationsService() (organizations.Service, cenclierrors.CencliError)
- func (c *Context) PrintAppResponseMeta(meta *responsemeta.ResponseMeta)
- func (c *Context) PrintData(cmd Command, data any) cenclierrors.CencliError
- func (c *Context) PrintDataWithTemplate(entity config.TemplateEntity, data any) cenclierrors.CencliError
- func (c *Context) PrintYAML(data any) cenclierrors.CencliError
- func (c *Context) SearchService() (search.Service, cenclierrors.CencliError)
- func (c *Context) SetCensysClient(cli client.Client)
- func (c *Context) SetLogger(l *slog.Logger)
- func (c *Context) Store() store.Store
- func (c *Context) ViewService() (view.Service, cenclierrors.CencliError)
- func (c *Context) WithProgress(ctx context.Context, logger *slog.Logger, initialMessage string, ...) cenclierrors.CencliError
- func (c *Context) WithStreamingOutput(ctx context.Context, logger *slog.Logger) (context.Context, func(error))
- type ContextOpts
- func WithAggregateService(svc aggregate.Service) ContextOpts
- func WithCenseyeService(svc censeye.Service) ContextOpts
- func WithCreditsService(svc credits.Service) ContextOpts
- func WithHistoryService(svc history.Service) ContextOpts
- func WithOrganizationsService(svc organizations.Service) ContextOpts
- func WithSearchService(svc search.Service) ContextOpts
- func WithViewService(svc view.Service) ContextOpts
- type OutputSupport
- type OutputType
- type PositionalArgs
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) DefaultOutputType ¶ added in v0.1.0
func (b *BaseCommand) DefaultOutputType() OutputType
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) InheritedFlags ¶ added in v0.1.0
func (b *BaseCommand) InheritedFlags() *pflag.FlagSet
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) RenderShort ¶ added in v0.1.0
func (b *BaseCommand) RenderShort() cenclierrors.CencliError
func (*BaseCommand) RenderTemplate ¶ added in v0.1.0
func (b *BaseCommand) RenderTemplate() cenclierrors.CencliError
func (*BaseCommand) SupportedOutputTypes ¶ added in v0.1.0
func (b *BaseCommand) SupportedOutputTypes() []OutputType
func (*BaseCommand) SupportsStreaming ¶ added in v0.1.0
func (b *BaseCommand) SupportsStreaming() bool
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
// InheritedFlags returns the underlying inherited flag set for the command.
// Used for modifying inherited flags on the command.
// Should not be implemented.
InheritedFlags() *pflag.FlagSet
// DefaultOutputType returns the default output type for this command.
DefaultOutputType() OutputType
// SupportedOutputTypes returns the output types this command supports.
// OutputTypeData includes json, yaml, and tree formats (buffered output).
SupportedOutputTypes() []OutputType
// SupportsStreaming returns true if this command supports streaming output mode.
// Commands that return true must use WithStreamingOutput in their Run implementation.
SupportsStreaming() bool
// RenderShort renders the command output in short format.
RenderShort() cenclierrors.CencliError
// RenderTemplate renders the command output using a template.
RenderTemplate() cenclierrors.CencliError
// 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 (*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) CreditsService ¶ added in v0.1.0
func (c *Context) CreditsService() (credits.Service, cenclierrors.CencliError)
CreditsService attempts to provide a CreditsService to the caller. If it is not already set and is unable to be instantiated, it will return an error.
func (*Context) GetStoredOrgID ¶ added in v0.1.0
func (c *Context) GetStoredOrgID(ctx context.Context) (mo.Option[identifiers.OrganizationID], cenclierrors.CencliError)
GetStoredOrgID retrieves the stored organization ID from the store. Returns the org ID if found, or None if not configured.
func (*Context) HasOrgID ¶ added in v0.1.0
HasOrgID returns true if the context has a configured organization ID.
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) OrganizationsService ¶ added in v0.1.0
func (c *Context) OrganizationsService() (organizations.Service, cenclierrors.CencliError)
OrganizationsService attempts to provide an OrganizationsService to the caller. If it is not already set and is unable to be instantiated, it will return an error.
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(cmd Command, data any) cenclierrors.CencliError
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 ¶
SetClient sets the Context's client so that it can be used to initialize services.
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.
func (*Context) WithStreamingOutput ¶ added in v0.1.0
func (c *Context) WithStreamingOutput( ctx context.Context, logger *slog.Logger, ) (context.Context, func(error))
WithStreamingOutput sets up streaming output infrastructure when streaming mode is enabled. For non-streaming mode, this is a no-op.
Returns a context with a streaming emitter attached (if streaming) and a stop function that must be called to properly clean up resources. The stop function should be deferred immediately after calling WithStreamingOutput.
Example usage:
ctx, stopStreaming := c.WithStreamingOutput(cmd.Context(), logger) defer stopStreaming(nil)
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 WithCreditsService ¶ added in v0.1.0
func WithCreditsService(svc credits.Service) ContextOpts
WithCreditsService injects an instantiated CreditsService to the Context. This should only be used in tests, as in the application, the CreditsService will be 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 WithOrganizationsService ¶ added in v0.1.0
func WithOrganizationsService(svc organizations.Service) ContextOpts
WithOrganizationsService injects an instantiated OrganizationsService to the Context. This should only be used in tests, as in the application, the OrganizationsService will be 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 OutputSupport ¶ added in v0.1.0
type OutputSupport struct {
Default OutputType
}
OutputSupport defines what output formats a command supports and its default.
type OutputType ¶ added in v0.1.0
type OutputType int
OutputType is the type of output a command supports.
const ( // OutputTypeData is the output type for commands that output buffered raw data (json, yaml, tree) OutputTypeData OutputType = iota // OutputTypeShort is the output type for commands that output a short view (i.e. a custom rendering) OutputTypeShort // OutputTypeTemplate is the output type for commands that output a template view (i.e. a handlebars template) OutputTypeTemplate )
type PositionalArgs ¶
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