cli

package
v0.44.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2022 License: Apache-2.0 Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultEnvSeparator defines how to join env var names.
	DefaultEnvSeparator = "_"
	// DefaultCommandDelimiters defines which delimiters should be replaced.
	DefaultCommandDelimiters = []string{" ", "-"}
)
View Source
var HelpTemplate = `
{{if or .Cmd.Runnable .Cmd.HasSubCommands}}{{.Cmd.UsageString}}{{end}}`

HelpTemplate is the custom help template for the command.

View Source
var UsageTemplate = `` /* 1056-byte string literal not displayed */

UsageTemplate is the custom usage template of the command. Changes in comparison to cobra's default template:

  1. Usage section a. `[flags]` is placed before the arguments. b. All arguments are put in the usage in their proper order. c. Where applicable, the argument name is replaced with its placeholder.
  2. Commands section a. For the root command (`secrethub`) the commands are grouped into `Management commands` and `Commands`
  3. Flags section a. Flag's type was removed. b. The help text for flags is well divided into its own column, thus making the visibility of the flags better. c. At the end of a flag's help text, the name of its environment variable is displayed between brackets. d. The section is hidden if the only flag is `--help`.
  4. Arguments section (created by us)

Functions

func ApplyTemplate added in v0.42.0

func ApplyTemplate(w io.Writer, text string, data interface{}) error

ApplyTemplate executes the given template text on data, writing the result to `w`.

func ArgumentArrRegister added in v0.42.0

func ArgumentArrRegister(params Argument, args []string) error

func ArgumentRegister added in v0.42.0

func ArgumentRegister(params []Argument, args []string) error

func PrettyJSON

func PrettyJSON(data interface{}) (string, error)

PrettyJSON returns a 4-space indented JSON text. Can be useful for printing out structs.

Types

type App

type App struct {
	Root *CommandClause
	// contains filtered or unexported fields
}

App represents a command-line application that wraps the cobra library and adds functionality for verifying environment variables used for configuring the cli.

func NewApp

func NewApp(name, help string) *App

NewApp defines a new command-line application.

func (*App) CheckStrictEnv added in v0.34.0

func (a *App) CheckStrictEnv() error

CheckStrictEnv checks that every environment variable that starts with the app name is recognized by the application.

func (*App) Command

func (a *App) Command(name, help string) *CommandClause

Command defines a new top-level command with the given name and help text.

func (*App) ExtraEnvVarFunc added in v0.24.0

func (a *App) ExtraEnvVarFunc(f func(key string) bool) *App

ExtraEnvVarFunc takes a function that determines additional environment variables recognized by the application.

func (*App) PersistentFlags added in v0.42.0

func (a *App) PersistentFlags() *FlagSet

PersistentFlags returns a flag set that allows configuring global persistent flags (that work on all commands of the CLI).

func (*App) PrintEnv

func (a *App) PrintEnv(w io.Writer, verbose bool, osEnv func() []string) error

PrintEnv reads all environment variables starting with the app name and writes a table with the keys and their status: set, empty, unrecognized. The value of environment variables are not printed out for security reasons. The list is limited to variables that are actually set in the environment. Setting verbose to true will also include all known variables that are not set.

func (*App) Version

func (a *App) Version(version string) *App

Version adds a flag for displaying the application version number.

type ArgValue added in v0.42.0

type ArgValue interface {
	Set(string) error
}

type Argument added in v0.42.0

type Argument struct {
	Value       ArgValue
	Name        string
	Required    bool
	Placeholder string
	Description string
	Hidden      bool
}

type ByteValue added in v0.42.0

type ByteValue []byte

func (*ByteValue) Set added in v0.42.0

func (s *ByteValue) Set(replacer string) error

type CommandClause

type CommandClause struct {
	Cmd *cobra.Command

	App  *App
	Args []Argument
	// contains filtered or unexported fields
}

CommandClause represents a command clause in a command-line application.

func (*CommandClause) AddPersistentPreRunE added in v0.42.0

func (c *CommandClause) AddPersistentPreRunE(f func(*cobra.Command, []string) error)

AddPersistentPreRunE adds an extra function to execute before the command or any of its children is executed. The added function is executed after all functions previously registered as PersistentPreRunE.

func (*CommandClause) AddPreRunE added in v0.42.0

func (c *CommandClause) AddPreRunE(f func(*cobra.Command, []string) error)

AddPreRunE adds an extra function to execute before the command is executed. The added function is executed after all functions previously registered as PreRunE.

func (*CommandClause) Alias added in v0.42.0

func (c *CommandClause) Alias(alias string)

func (*CommandClause) BindAction added in v0.42.0

func (c *CommandClause) BindAction(fn func() error)

func (*CommandClause) BindArguments added in v0.42.0

func (c *CommandClause) BindArguments(params []Argument)

BindArguments binds a list of arguments that all parse 1 value.

func (*CommandClause) BindArgumentsArr added in v0.42.0

func (c *CommandClause) BindArgumentsArr(param Argument)

BindArgumentsArr binds a single argument that can parse 1 or more values.

func (*CommandClause) Command

func (c *CommandClause) Command(name, help string) *CommandClause

Command adds a new subcommand to this command.

func (*CommandClause) Flag

func (c *CommandClause) Flag(name string) *Flag

Flag defines a new flag with the given long name and help text, adding an environment variable default configurable by APP_COMMAND_FLAG_NAME. The help text is suffixed with the default value of the flag.

func (*CommandClause) Flags added in v0.42.0

func (c *CommandClause) Flags() *FlagSet

func (*CommandClause) HelpLong added in v0.42.0

func (c *CommandClause) HelpLong(helpLong string)

func (*CommandClause) Hidden

func (c *CommandClause) Hidden() *CommandClause

Hidden hides the command in help texts.

type Flag

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

Flag represents a command-line flag.

func (*Flag) Changed added in v0.42.0

func (f *Flag) Changed() bool

func (*Flag) Envar

func (f *Flag) Envar(name string) *Flag

Envar overrides the environment variable name that configures the default value for a flag.

func (*Flag) HasEnvarValue added in v0.42.0

func (f *Flag) HasEnvarValue() bool

func (*Flag) Hidden

func (f *Flag) Hidden() *Flag

func (*Flag) NoEnvar

func (f *Flag) NoEnvar() *Flag

type FlagSet added in v0.42.0

type FlagSet struct {
	*pflag.FlagSet
	// contains filtered or unexported fields
}

func (*FlagSet) Bool added in v0.42.0

func (f *FlagSet) Bool(name string, def bool, usage string) *Flag

func (*FlagSet) BoolP added in v0.42.0

func (f *FlagSet) BoolP(name, shorthand string, def bool, usage string) *Flag

func (*FlagSet) BoolVar added in v0.42.0

func (f *FlagSet) BoolVar(reference *bool, name string, def bool, usage string) *Flag

func (*FlagSet) BoolVarP added in v0.42.0

func (f *FlagSet) BoolVarP(reference *bool, name, shorthand string, def bool, usage string) *Flag

func (*FlagSet) DurationVar added in v0.42.0

func (f *FlagSet) DurationVar(reference *time.Duration, name string, def time.Duration, usage string) *Flag

func (*FlagSet) DurationVarP added in v0.42.0

func (f *FlagSet) DurationVarP(reference *time.Duration, name, shorthand string, def time.Duration, usage string) *Flag

func (*FlagSet) IntVar added in v0.42.0

func (f *FlagSet) IntVar(reference *int, name string, def int, usage string) *Flag

func (*FlagSet) IntVarP added in v0.42.0

func (f *FlagSet) IntVarP(reference *int, name, shorthand string, def int, usage string) *Flag

func (*FlagSet) StringVar added in v0.42.0

func (f *FlagSet) StringVar(reference *string, name string, def string, usage string) *Flag

func (*FlagSet) StringVarP added in v0.42.0

func (f *FlagSet) StringVarP(reference *string, name, shorthand string, def string, usage string) *Flag

func (*FlagSet) Var added in v0.42.0

func (f *FlagSet) Var(reference pflag.Value, name string, usage string) *Flag

func (*FlagSet) VarP added in v0.42.0

func (f *FlagSet) VarP(reference pflag.Value, name string, shorthand string, usage string) *Flag

func (*FlagSet) VarPF added in v0.42.0

func (f *FlagSet) VarPF(reference pflag.Value, name string, shorthand string, usage string) *Flag

type Logger

type Logger interface {
	// Debugf logs a message when debug mode is enabled.
	Debugf(format string, args ...interface{})
	// Warningf logs a message when debug mode is enabled.
	Warningf(format string, args ...interface{})
	// EnableDebug turns printing debug messages on.
	EnableDebug()
}

Logger can be used to log debug and warning messages.

func NewLogger

func NewLogger() Logger

NewLogger returns a logger with the given format, module and loglevel.

type Registerer added in v0.42.0

type Registerer interface {
	Command(cmd string, help string) *CommandClause
}

Registerer allows others to register commands on it.

type StringListValue added in v0.42.0

type StringListValue []string

func (*StringListValue) Set added in v0.42.0

func (s *StringListValue) Set(replacer string) error

type StringValue added in v0.42.0

type StringValue struct {
	Value string
}

func (*StringValue) Set added in v0.42.0

func (s *StringValue) Set(replacer string) error

type URLValue added in v0.42.0

type URLValue struct {
	*url.URL
}

func (*URLValue) Set added in v0.42.0

func (s *URLValue) Set(replacer string) error

func (*URLValue) String added in v0.42.0

func (s *URLValue) String() string

Directories

Path Synopsis
Package clip provides functionality to read from and write to the clipboard.
Package clip provides functionality to read from and write to the clipboard.
fakeclip
Package fakeclip provides fake implementations of the clip.Clipper interface to be used for testing.
Package fakeclip provides fake implementations of the clip.Clipper interface to be used for testing.
Package cloneproc provides functionality to spawn a detached clone of the current process.
Package cloneproc provides functionality to spawn a detached clone of the current process.
Package filemode provides a wrapper around os.FileMode so that it can be parsed from a CLI flag.
Package filemode provides a wrapper around os.FileMode so that it can be parsed from a CLI flag.
Package mlock allows for locking memory, providing implementations for different operating systems.
Package mlock allows for locking memory, providing implementations for different operating systems.
Package progress provides a printer that writes dots at a configured interval.
Package progress provides a printer that writes dots at a configured interval.
fakeprogress
Package fakeprogress provides an implementation of the progress.Printer interface to be used in tests.
Package fakeprogress provides an implementation of the progress.Printer interface to be used in tests.
ui
Package ui provides a simple way to interact with the user through the terminal, i.e.
Package ui provides a simple way to interact with the user through the terminal, i.e.

Jump to

Keyboard shortcuts

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