cli

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: MIT Imports: 16 Imported by: 2

Documentation

Index

Constants

View Source
const StatusErr = 1
View Source
const StatusOk = 0

Variables

View Source
var CommandLocked = errors.New("command is locked, skipping execution")

Functions

func Bootstrap

func Bootstrap(
	args []string,
	availableCommands *CommandsRegistry,
	outputWriter io.Writer,
	processExit func(code int),
)

Bootstrap Will bootstrap everything needed for the user CLI request. Will process the user input and run the requested command. By default, will output to os.Stdout if nil is provided for the io.Writer argument.

Types

type Command

type Command interface {
	Id() string
	Description() string
	Exec(stdWriter io.Writer) error
	DefineFlags(flagSet *flag.FlagSet)
	ValidateFlags() error
}

Command interface defines the methods that a command must implement

type CommandWithoutFlags

type CommandWithoutFlags struct{}

func (*CommandWithoutFlags) DefineFlags

func (*CommandWithoutFlags) DefineFlags(*flag.FlagSet)

func (*CommandWithoutFlags) ValidateFlags

func (*CommandWithoutFlags) ValidateFlags() error

type CommandsRegistry

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

CommandsRegistry holds all registered commands

func NewCommandsRegistry

func NewCommandsRegistry() *CommandsRegistry

func (*CommandsRegistry) Command

func (registry *CommandsRegistry) Command(id string) (Command, bool)

Command returns a command by its ID

func (*CommandsRegistry) Commands

func (registry *CommandsRegistry) Commands() map[string]Command

Commands returns a copy of all registered commands

func (*CommandsRegistry) Register

func (registry *CommandsRegistry) Register(cmd Command) error

Register adds a command to the registry

type FsLockableCommand

type FsLockableCommand struct {
	// The command that needs to be locked
	Command Command
	// contains filtered or unexported fields
}

FsLockableCommand is a helper struct that implements the locking mechanism for commands that need to run exclusively (preventing concurrent execution).

func NewLockableCommand

func NewLockableCommand(
	cmd Command,
	lockFileDirPath string,
) *FsLockableCommand

NewLockableCommand creates a new FsLockableCommand for the given command. The lock file will be created with the Command.Id() in its name.

func NewLockableCommandWithLockName

func NewLockableCommandWithLockName(
	cmd Command,
	lockFileDirPath string,
	lockName string,
) *FsLockableCommand

NewLockableCommandWithLockName creates a new FsLockableCommand for the given command. The lock file will be created with the given lockName in its name.

func (*FsLockableCommand) DefineFlags

func (l *FsLockableCommand) DefineFlags(flagSet *flag.FlagSet)

DefineFlags delegates to the wrapped command.

func (*FsLockableCommand) Description

func (l *FsLockableCommand) Description() string

Description returns the description of the wrapped command.

func (*FsLockableCommand) Exec

func (l *FsLockableCommand) Exec(stdWriter io.Writer) error

Exec acquires the lock, executes the wrapped command, and then releases the lock.

func (*FsLockableCommand) Id

func (l *FsLockableCommand) Id() string

Id returns the ID of the wrapped command.

func (*FsLockableCommand) Lock

func (l *FsLockableCommand) Lock() (bool, error)

Lock acquires both the in-memory mutex and the file lock. If the lock cannot be acquired, it returns an error.

func (*FsLockableCommand) Unlock

func (l *FsLockableCommand) Unlock() error

Unlock releases both the in-memory mutex and the file lock.

func (*FsLockableCommand) ValidateFlags

func (l *FsLockableCommand) ValidateFlags() error

ValidateFlags delegates to the wrapped command.

type HelpCommand

type HelpCommand struct {
	CommandWithoutFlags
	// contains filtered or unexported fields
}

func NewHelpCommand

func NewHelpCommand(availableCommands []Command) *HelpCommand

func (*HelpCommand) Description

func (c *HelpCommand) Description() string

func (*HelpCommand) Exec

func (c *HelpCommand) Exec(baseWriter io.Writer) error

func (*HelpCommand) Id

func (c *HelpCommand) Id() string

type LockableCommand

type LockableCommand interface {
	Command
	Lock() (bool, error)
	Unlock() error
}

Jump to

Keyboard shortcuts

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