cli

package
v0.0.0-...-488a668 Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: BSD-3-Clause Imports: 13 Imported by: 5

Documentation

Overview

Package cli contains utilities for "Command Line Interface" applications.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadPipedInput

func ReadPipedInput(maxLength int) ([]byte, error)

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

func ReadSecure(prompt string) ([]byte, error)

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

func SetupCommandParams(c *cobra.Command, params []Param, vp *viper.Viper) error

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

func SignalsHandler(list []os.Signal) chan os.Signal

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

func (c *Config) FileUsed() string

FileUsed returns the full path of the configuration file used to load the settings.

func (*Config) Get

func (c *Config) Get(key string) interface{}

Get the value registered for `key`, if any.

func (*Config) Internals

func (c *Config) Internals() *viper.Viper

Internals expose the private viper instance used by the configuration manager; use with care.

func (*Config) IsSet

func (c *Config) IsSet(key string) bool

IsSet returns true if a value is available for `key`.

func (*Config) Read

func (c *Config) Read(src io.Reader) error

Read configuration values from the provided `src` element.

func (*Config) ReadFile

func (c *Config) ReadFile(ignoreNotFound bool) error

ReadFile will try to load configuration values from the local filesystem; optionally ignore the error produced when no configuration file was found.

func (*Config) Set

func (c *Config) Set(key string, value interface{})

Set the provided `key` to `value`.

func (*Config) Unmarshal

func (c *Config) Unmarshal(receiver interface{}, key string) error

Unmarshal will load configuration values into `receiver`; which must be a pointer. A `key` value can be provided to load a specific subsection of the settings available.

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.

func (*Spinner) Start

func (s *Spinner) Start()

Start the spinner indicator.

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop the spinner indicator.

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.

Jump to

Keyboard shortcuts

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