cmd

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2025 License: MIT Imports: 13 Imported by: 2

Documentation

Index

Constants

View Source
const (
	GroupBase  = "group-base"
	GroupOther = "group-other"
)

Variables

This section is empty.

Functions

func GenDocsCommand added in v0.8.1

func GenDocsCommand(ec *ecctx.ExecutionContext) *cobra.Command

Types

type InitFunc

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

type NoopRunner added in v0.8.2

type NoopRunner[T any] struct{}

func (*NoopRunner[T]) Complete added in v0.8.2

func (o *NoopRunner[T]) Complete(ctx context.Context, runnerArg T)

func (*NoopRunner[T]) Run added in v0.8.2

func (o *NoopRunner[T]) Run(ctx context.Context, runnerArg T) error

func (*NoopRunner[T]) Validate added in v0.8.2

func (o *NoopRunner[T]) Validate(ctx context.Context, runnerArg T) error

type RootCommandBuilder added in v0.8.1

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

func NewRootCommand

func NewRootCommand(ec *ecctx.ExecutionContext) *RootCommandBuilder

NewRootCommand creates a new root command Use this function to create a new root command that is used to add subcommands

It supports the following features:

- Adding global flags to the command - Automatically reads configuration from the configuration file - Automatically reads configuration from environment variables - Automatically binds the configuration to the command's flags - Automatically sets the log level based on the configuration - Automatically sets the log format based on the configuration

It uses ec.AppName as the base name for the configuration file and environment variables initFunc is a function that is called before the command is executed It can be used to add more context or do other initializations

func (*RootCommandBuilder) Build added in v0.8.1

func (b *RootCommandBuilder) Build() *cobra.Command

Build builds the root command

func (*RootCommandBuilder) WithInitFunc added in v0.8.1

func (b *RootCommandBuilder) WithInitFunc(fn InitFunc) *RootCommandBuilder

WithInitFunc adds an init function to the root command This sets the PersistentPreRunE function of the command

func (*RootCommandBuilder) WithPersistentFlags added in v0.8.1

func (b *RootCommandBuilder) WithPersistentFlags(flags *pflag.FlagSet) *RootCommandBuilder

WithPreRunFunc adds persistent flags to the root command

type SubCommandBuilder added in v0.8.1

type SubCommandBuilder[T any] struct {
	// contains filtered or unexported fields
}

SubCommandBuilder is a builder for a subcommand

func NewSubCommand

func NewSubCommand[T any](
	name string,
	runner SubCommandRunner[T],
	runnerArg T,
) *SubCommandBuilder[T]

NewSubCommand creates a new subcommand args can be used to pass arguments to the runner

func (*SubCommandBuilder[T]) Build added in v0.8.1

func (b *SubCommandBuilder[T]) Build() *cobra.Command

Build builds the subcommand

func (*SubCommandBuilder[T]) WithExample added in v0.8.1

func (b *SubCommandBuilder[T]) WithExample(example string) *SubCommandBuilder[T]

WithExample sets the example of the subcommand

func (*SubCommandBuilder[T]) WithGroupID added in v0.8.1

func (b *SubCommandBuilder[T]) WithGroupID(group string) *SubCommandBuilder[T]

WithGroupID sets the group id of the subcommand

func (*SubCommandBuilder[T]) WithLongDesc added in v0.8.1

func (b *SubCommandBuilder[T]) WithLongDesc(desc string) *SubCommandBuilder[T]

WithLongDesc sets the long description of the subcommand

func (*SubCommandBuilder[T]) WithNoArgs added in v0.8.2

func (b *SubCommandBuilder[T]) WithNoArgs() *SubCommandBuilder[T]

WithExample sets the example of the subcommand

func (*SubCommandBuilder[T]) WithShortDesc added in v0.8.1

func (b *SubCommandBuilder[T]) WithShortDesc(desc string) *SubCommandBuilder[T]

WithShortDesc sets the short description of the subcommand

type SubCommandRunner

type SubCommandRunner[T any] interface {
	// Complete performs any setup or completion of arguments
	Complete(ctx context.Context, arg T)

	// Validate checks if the arguments are valid
	// Returns error if validation fails
	Validate(ctx context.Context, arg T) error

	// Run executes the command with the given arguments
	// Returns error if execution fails
	Run(ctx context.Context, arg T) error
}

SubCommandRunner is an interface for a subcommand runner

type TestRunner added in v0.8.1

type TestRunner[T any] struct {
	CompleteFunc func(T)
	ValidateFunc func(T) error
	RunFunc      func(T) error
}

TestRunner is a test runner for commands

func (*TestRunner[T]) Complete added in v0.8.1

func (tr *TestRunner[T]) Complete(ctx context.Context, runnerArg T)

func (*TestRunner[T]) Run added in v0.8.1

func (tr *TestRunner[T]) Run(ctx context.Context, runnerArg T) error

func (*TestRunner[T]) Validate added in v0.8.1

func (tr *TestRunner[T]) Validate(ctx context.Context, runnerArg T) error

Jump to

Keyboard shortcuts

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