commands

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: Apache-2.0, UNKNOWN not legal advice Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HelpExec

func HelpExec(_ context.Context, _ []string) error

HelpExec is a standard exec method for displaying help information about a command

func WriteNopCloser

func WriteNopCloser(w io.Writer) io.WriteCloser

Types

type Command

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

Command is a simple wrapper for gnoland commands.

func NewCommand

func NewCommand(
	meta Metadata,
	config Config,
	exec ExecMethod,
) *Command

func (*Command) AddSubCommands

func (c *Command) AddSubCommands(cmds ...*Command)

AddSubCommands adds a variable number of subcommands and registers common flags using the flagset

func (*Command) Execute

func (c *Command) Execute(ctx context.Context, args []string)

Execute is a helper function for command entry. It wraps ParseAndRun and handles the flag.ErrHelp error, ensuring that every command with -h or --help won't show an error message: 'error parsing commandline arguments: flag: help requested'

Additionally, any error of type [ErrExitCode] will be handled by exiting with the given status code.

func (*Command) Parse

func (c *Command) Parse(args []string) error

Parse the commandline arguments for this command and all sub-commands recursively, defining flags along the way. If Parse returns without an error, the terminal command has been successfully identified, and may be invoked by calling Run.

If the terminal command identified by Parse doesn't define an Exec function, then Parse will return NoExecError.

Forked from peterbourgon/ff/ffcli

func (*Command) ParseAndRun

func (c *Command) ParseAndRun(ctx context.Context, args []string) error

ParseAndRun is a helper function that calls Parse and then Run in a single invocation. It's useful for simple command trees that don't need two-phase setup.

Forked from peterbourgon/ff/ffcli

func (*Command) Run

func (c *Command) Run(ctx context.Context) (err error)

Run selects the terminal command in a command tree previously identified by a successful call to Parse, and calls that command's Exec function with the appropriate subset of commandline args.

If the terminal command previously identified by Parse doesn't define an Exec function, then Run will return an error.

Forked from peterbourgon/ff/ffcli

type Config

type Config interface {
	// RegisterFlags registers the specific flags to the flagset
	RegisterFlags(*flag.FlagSet)
}

Config defines the command config interface that holds flag values and execution logic

type EmptyConfig

type EmptyConfig struct{}

EmptyConfig is an empty command configuration that should be substituted in commands that require one

func NewEmptyConfig

func NewEmptyConfig() *EmptyConfig

NewEmptyConfig creates a new instance of the empty configuration

func (*EmptyConfig) RegisterFlags

func (ec *EmptyConfig) RegisterFlags(_ *flag.FlagSet)

RegisterFlags ignores flag set registration

type ExecMethod

type ExecMethod func(ctx context.Context, args []string) error

ExecMethod executes the command using the specified config

type ExitCodeError

type ExitCodeError int

ExitCodeError is an error to terminate the program without printing any error, but passing in the given exit code to os.Exit.

Command.ParseAndRun will return any ExitCodeError encountered, but Command.Execute will handle it and return an appropriate error message.

func (ExitCodeError) Error

func (e ExitCodeError) Error() string

type IO

type IO interface {
	// getters
	In() io.Reader
	Out() io.WriteCloser
	Err() io.WriteCloser

	// setters and helpers
	SetIn(in io.Reader)
	SetOut(out io.WriteCloser)
	SetErr(err io.WriteCloser)
	Println(args ...interface{})
	Printf(format string, args ...interface{})
	Printfln(format string, args ...interface{})
	ErrPrintln(args ...interface{})
	ErrPrintfln(format string, args ...interface{})
	GetCheckPassword(prompts [2]string, insecure bool) (string, error)
	GetConfirmation(prompt string) (bool, error)
	GetPassword(prompt string, insecure bool) (string, error)
	GetString(prompt string) (string, error)
}

IO holds settable command input, output and error buffers

func NewDefaultIO

func NewDefaultIO() IO

NewDefaultIO returns a default command io that utilizes standard input / output / error

func NewTestIO

func NewTestIO() IO

NewTestIO returns a test command io that only sets standard input (to avoid panics)

type IOImpl

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

func (*IOImpl) Err

func (io *IOImpl) Err() io.WriteCloser

func (*IOImpl) ErrPrintfln

func (io *IOImpl) ErrPrintfln(format string, args ...interface{})

ErrPrintfln prints a formatted string terminated by a newline to cmd.Err(Buf)

func (*IOImpl) ErrPrintln

func (io *IOImpl) ErrPrintln(args ...interface{})

ErrPrintln prints a line terminated by a newline to cmd.Err(Buf)

func (*IOImpl) GetCheckPassword

func (io *IOImpl) GetCheckPassword(
	prompts [2]string,
	insecure bool,
) (string, error)

GetCheckPassword will prompt for a password twice to verify they match (for creating a new password). It enforces the password length. Only parses password once if input is piped in.

func (*IOImpl) GetConfirmation

func (io *IOImpl) GetConfirmation(prompt string) (bool, error)

GetConfirmation will request user give the confirmation from stdin. "y", "Y", "yes", "YES", and "Yes" all count as confirmations. If the input is not recognized, it returns false and a nil error.

func (*IOImpl) GetPassword

func (io *IOImpl) GetPassword(
	prompt string,
	insecure bool,
) (string, error)

GetPassword fetches the password using the provided prompt, if any

func (*IOImpl) GetString

func (io *IOImpl) GetString(prompt string) (string, error)

GetString simply returns the trimmed string output of a given reader.

func (*IOImpl) In

func (io *IOImpl) In() io.Reader

func (*IOImpl) Out

func (io *IOImpl) Out() io.WriteCloser

func (*IOImpl) Printf

func (io *IOImpl) Printf(format string, args ...interface{})

Printf prints a formatted string without trailing newline

func (*IOImpl) Printfln

func (io *IOImpl) Printfln(format string, args ...interface{})

Printfln prints a formatted string terminated by a newline

func (*IOImpl) Println

func (io *IOImpl) Println(args ...interface{})

Println prints a line terminated by a newline

func (*IOImpl) SetErr

func (io *IOImpl) SetErr(err io.WriteCloser)

SetErr sets the error writer for the command io

func (*IOImpl) SetIn

func (io *IOImpl) SetIn(in io.Reader)

SetIn sets the input reader for the command io

func (*IOImpl) SetOut

func (io *IOImpl) SetOut(out io.WriteCloser)

SetOut sets the output writer for the command io

type Metadata

type Metadata struct {
	Name       string
	ShortUsage string
	ShortHelp  string
	LongHelp   string
	Options    []ff.Option
}

Metadata contains basic help information about a command

type StringArr

type StringArr []string

StringArr defines the custom flag type that represents an array of string values

func (*StringArr) Set

func (s *StringArr) Set(value string) error

Set is a required output method for the flag. This is where our custom type manipulation actually happens

func (*StringArr) String

func (s *StringArr) String() string

String is a required output method for the flag

Jump to

Keyboard shortcuts

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