cli

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2020 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthHandlers = make(map[string]AuthHandler)

AuthHandlers is the map of registered auth type names to handlers

View Source
var Cache *viper.Viper

Cache is used to store temporary data between runs.

Client makes HTTP requests and parses the responses.

View Source
var (
	ErrCannotUnmarshal = errors.New("Unable to unmarshal response")
)

HTTP Client Errors

View Source
var PreRun func(cmd *cobra.Command, args []string) error

PreRun is a function that will run after flags are parsed but before the command handler has been called.

Root command (entrypoint) of the CLI.

View Source
var Stderr io.Writer = os.Stderr

Stderr is a cross-platform, color-safe writer if colors are enabled, otherwise it defaults to `os.Stderr`.

View Source
var Stdout io.Writer = os.Stdout

Stdout is a cross-platform, color-safe writer if colors are enabled, otherwise it defaults to `os.Stdout`.

Functions

func AddFlag

func AddFlag(path, name, short, description string, defaultValue interface{})

AddFlag registers a new custom flag for the command path. Use the `RegisterBefore` and `RegisterAfter` functions to register a handler that can check the value of this flag.

func AddGlobalFlag

func AddGlobalFlag(name, short, description string, defaultValue interface{})

AddGlobalFlag will make a new global flag on the root command.

func DeepAssign

func DeepAssign(target, source map[string]interface{})

DeepAssign recursively merges a source map into the target.

func GetBody

func GetBody(mediaType string, args []string) (string, error)

GetBody returns the request body if one was passed either as shorthand arguments or via stdin.

func GetMatchValue

func GetMatchValue(ctx *context.Context, selector string, reqParams map[string]interface{}, decoded interface{}) (interface{}, error)

GetMatchValue returns a value for the given selector query.

func GetProfile

func GetProfile() map[string]string

GetProfile returns the current profile's configuration.

func HandleAfter

func HandleAfter(path string, params *viper.Viper, resp *gentleman.Response, data interface{}) interface{}

HandleAfter runs any regeistered post-request handlers for the given command.

func HandleBefore

func HandleBefore(path string, params *viper.Viper, r *gentleman.Request)

HandleBefore runs any registered pre-request handlers for the given command.

func Init

func Init(config *Config)

Init will set up the CLI.

func InitCredentials

func InitCredentials(options ...func(*CredentialsFile) error)

InitCredentials sets up the profile/auth commands. Must be called *after* you have called `cli.Init()`.

// Initialize an API key
cli.InitCredentials(cli.ProfileKeys("api-key"))

This is deprecated and you should use `cli.UseAuth` instead.

func InitCredentialsFile

func InitCredentialsFile()

InitCredentialsFile sets up the creds file and `profile` global parameter.

func LogMiddleware

func LogMiddleware(useColor bool)

LogMiddleware adds verbose log info to HTTP requests.

func Markdown

func Markdown(content string) string

Markdown renders terminal-friendly Markdown content.

func Match

func Match(test string, expected json.RawMessage, actual interface{}) (bool, error)

Match returns `true` if the expected value of the match type is found in the given response data.

func ProfileKeys

func ProfileKeys(keys ...string) func(*CredentialsFile) error

ProfileKeys lets you specify authentication profile keys to be used in the credentials file. This is deprecated and you should use `cli.UseAuth` instead.

func ProfileListKeys

func ProfileListKeys(keys ...string) func(*CredentialsFile) error

ProfileListKeys sets which keys will be shown in the table when calling the `auth list-profiles` command. This is deprecated and you should use `cli.UseAuth` instead.

func RegisterAfter

func RegisterAfter(path string, handler AfterHandlerFunc)

RegisterAfter registers a post-request handler for the given command path. The handler may modify the unmarshalled response.

func RegisterBefore

func RegisterBefore(path string, handler BeforeHandlerFunc)

RegisterBefore registers a pre-request handler for the given command path. The handler may modify the request before it gets sent over the wire.

func SetCustomFlags

func SetCustomFlags(cmd *cobra.Command)

SetCustomFlags sets up the command with additional registered flags.

func UnmarshalRequest

func UnmarshalRequest(ctx *context.Context, s interface{}) error

UnmarshalRequest body into a given structure `s`. Supports both JSON and YAML depending on the request's content-type header.

func UnmarshalResponse

func UnmarshalResponse(resp *gentleman.Response, s interface{}) error

UnmarshalResponse into a given structure `s`. Supports both JSON and YAML depending on the response's content-type header.

func UseAuth

func UseAuth(typeName string, handler AuthHandler)

UseAuth registers a new auth handler for a given type name. For backward- compatibility, the auth type name can be a blank string. It is recommended to always pass a value for the type name.

func UserAgentMiddleware

func UserAgentMiddleware()

UserAgentMiddleware sets the user-agent header on requests.

Types

type AfterHandlerFunc

type AfterHandlerFunc func(string, *viper.Viper, *gentleman.Response, interface{}) interface{}

AfterHandlerFunc is a function that runs after a request has been sent and the response is unmarshalled. It may modify the response. It must return the response data regardless of whether it was modified.

type AuthHandler

type AuthHandler interface {
	// ProfileKeys returns the key names for fields to store in the profile.
	ProfileKeys() []string

	// OnRequest gets run before the request goes out on the wire.
	OnRequest(log *zerolog.Logger, request *http.Request) error
}

AuthHandler describes a handler that can be called on a request to inject auth information and is agnostic to the type of auth.

type BeforeHandlerFunc

type BeforeHandlerFunc func(string, *viper.Viper, *gentleman.Request)

BeforeHandlerFunc is a function that runs before a command sends a request over the wire. It may modify the request.

type Config

type Config struct {
	AppName   string
	EnvPrefix string
	Version   string
	Caching   bool
}

Config is used to pass settings to the CLI.

type ConsoleWriter

type ConsoleWriter struct {
	Out     io.Writer
	NoColor bool
}

ConsoleWriter reads a JSON object per write operation and outputs an optionally colored human readable version on the Out writer. This has been modified from the ConsoleWriter that ships with zerolog.

func (ConsoleWriter) Write

func (w ConsoleWriter) Write(p []byte) (n int, err error)

type CredentialsFile

type CredentialsFile struct {
	*viper.Viper
	// contains filtered or unexported fields
}

CredentialsFile holds credential-related information.

var Creds *CredentialsFile

Creds represents a configuration file storing credential-related information. Use this only after `InitCredentials` has been called.

type DefaultFormatter

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

DefaultFormatter can apply JMESPath queries and can output prettified JSON and YAML output. If Stdout is a TTY, then colorized/indented output is provided unless the `--raw` flag is specified. The default formatter uses the `query` and `output-format` configuration values to perform JMESPath queries and set JSON (default) or YAML output.

func NewDefaultFormatter

func NewDefaultFormatter(tty bool) *DefaultFormatter

NewDefaultFormatter creates a new formatted with autodetected TTY capabilities.

func (*DefaultFormatter) Format

func (f *DefaultFormatter) Format(data interface{}) error

Format will filter, prettify, colorize and output the data.

type ResponseFormatter

type ResponseFormatter interface {
	Format(interface{}) error
}

ResponseFormatter will filter, prettify, and print out the results of a call.

var Formatter ResponseFormatter

Formatter is the currently configured response output formatter.

Jump to

Keyboard shortcuts

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