Documentation ¶
Overview ¶
Package cli contains utilities for "Command Line Interface" applications.
Index ¶
- func ReadPipedInput(maxLength int) ([]byte, error)
- func ReadSecure(prompt string) ([]byte, error)
- func SetupCommandParams(c *cobra.Command, params []Param, vp *viper.Viper) error
- func SignalsHandler(list []os.Signal) chan os.Signal
- type Config
- func (c *Config) FileUsed() string
- func (c *Config) Get(key string) interface{}
- func (c *Config) Internals() *viper.Viper
- func (c *Config) IsSet(key string) bool
- func (c *Config) Read(src io.Reader) error
- func (c *Config) ReadFile(ignoreNotFound bool) error
- func (c *Config) Set(key string, value interface{})
- func (c *Config) Unmarshal(receiver interface{}, key string) error
- type ConfigOptions
- type Param
- type Spinner
- type SpinnerOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadPipedInput ¶
ReadPipedInput retrieves contents passed-in from standard input up to the provided maximum number of bytes.
Example ¶
// Read a maximum of 32 bytes from standard input input, err := ReadPipedInput(32) if len(input) > 0 && err != nil { // Handle error panic(err) } log.Printf("data received: %s", input)
Output:
func ReadSecure ¶
ReadSecure will interactively prompt the user to enter a value. The value provided won't be displayed on the screen.
Example ¶
// Interactively prompt the user to enter a value. The value provided won't be // displayed on the screen. password, err := ReadSecure("Enter your password: ") if err != nil { // Handle error panic(err) } log.Printf("you entered: %s", password)
Output:
func SetupCommandParams ¶
SetupCommandParams will properly configure the command with the provided parameter list.
Example ¶
Register a list of parameters to a sample command.
sampleCmd := &cobra.Command{} parameters := []Param{ { Name: "name-of-parameter", Usage: "describe the parameter use or intent", FlagKey: "cmd.parameter.name", ByDefault: 9090, }, { Name: "bool-flag", Usage: "parameters support several basic types", FlagKey: "cmd.parameter.flag", ByDefault: false, }, } if err := SetupCommandParams(sampleCmd, parameters, viper.GetViper()); err != nil { panic(err) }
Output:
func SignalsHandler ¶
SignalsHandler returns a properly configured OS signals handler channel.
Example ¶
// Register the signals to look for and wait for one s := <-SignalsHandler([]os.Signal{ syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, }) log.Printf("signal received: %s", s)
Output:
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config provides a simple interface to manage application settings using Viper.
func ConfigHandler ¶
func ConfigHandler(app string, opts *ConfigOptions) *Config
ConfigHandler returns a new configuration management instance configured for the provided `app` identifier. Optional locations can be provided to specify valid paths to look for a config file.
func (*Config) FileUsed ¶
FileUsed returns the full path of the configuration file used to load the settings.
func (*Config) Internals ¶
Internals expose the private viper instance used by the configuration manager; use with care.
func (*Config) ReadFile ¶
ReadFile will try to load configuration values from the local filesystem; optionally ignore the error produced when no configuration file was found.
type ConfigOptions ¶
type ConfigOptions struct { // Configuration file name (without extension). Defaults to `config`. FileName string // Configuration file extension. This will be used internally to automatically // decode its contents accordingly. Defaults to `yaml` FileType string // Additional locations to look for the configuration file. Locations []string }
ConfigOptions adjust the internal behavior of the configuration handler.
type Param ¶
type Param struct { // Name of the parameter, will be displayed to the user when inspecting the // help information for the command. Name string // Brief and clear description of the parameter usage or intent, will be // displayed to the user when inspecting the help information for the command. Usage string // Internal code for the parameter. This should match the structure of a // configuration file when used and can be useful to add 'namespaces' for // configuration settings. This key should be used when reading configuration // options using viper. For example: // root.child.parameter FlagKey string // Default value to use for the parameter, the type of the default value will // determine the expected type for the parameter. Supported types are: // int, int32, int64 uint32, uint64, string, bool, []string ByDefault interface{} // If provided the parameter can be provided using a shorthand letter that can // be used after a single dash. Must be unique. Short string // Parameters are optional by default. If instead you wish your command to report // an error when a parameter has not been set, mark it as required. Required bool }
Param represents an individual CLI parameter.
type Spinner ¶
type Spinner struct {
// contains filtered or unexported fields
}
Spinner indicator.
func NewSpinner ¶
func NewSpinner(opts ...SpinnerOption) *Spinner
NewSpinner creates a new spinner indicator instance.
type SpinnerOption ¶
type SpinnerOption = func(s *Spinner)
SpinnerOption provide a functional style mechanism to adjust the settings when creating a new spinner instance.
func WithSpinnerColor ¶
func WithSpinnerColor(color string) SpinnerOption
WithSpinnerColor adjust the color used for the spinner indicator. Supported values are: "green", "blue", "yellow" and "red".
Directories ¶
Path | Synopsis |
---|---|
Package loader provide a helper mechanism to work with settings for complex applications.
|
Package loader provide a helper mechanism to work with settings for complex applications. |
http
Package http provides a configuration loader component suitable to use with `http.Server` instances.
|
Package http provides a configuration loader component suitable to use with `http.Server` instances. |
internal
Package internal includes private utilities not intended for public consumption.
|
Package internal includes private utilities not intended for public consumption. |
otel
Package otel provides a configuration loader component suitable to use with `sdk.Instrumentation` instances.
|
Package otel provides a configuration loader component suitable to use with `sdk.Instrumentation` instances. |
rpc
Package rpc provides a configuration loader component suitable to use with `rpc.Server` instances.
|
Package rpc provides a configuration loader component suitable to use with `rpc.Server` instances. |
Package shell provides an interactive client for CLI-based applications.
|
Package shell provides an interactive client for CLI-based applications. |