cobra

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatQuiet      format = "quiet"
	FormatTable      format = "table"
	FormatJSON       format = "json"
	FormatJSONPretty format = "json-pretty"

	FlagQuiet  = "quiet"
	FlagFormat = "format"

	None = "<none>"
)

Variables

View Source
var (
	// ErrNilDependency is returned if a dependency is missing.
	ErrNilDependency = errors.New("a dependency was expected to be defined but is nil. Please open an issue with the stack trace")
	// ErrNoPrintButFail is returned to detect a failure state that was already reported to the user in some way
	ErrNoPrintButFail = errors.New("this error should never be printed")
)

Functions

func AskForConfirmation

func AskForConfirmation(s string, stdin io.Reader, stdout io.Writer) bool

asks for confirmation with the question string s and reads the answer pass nil to use os.Stdin and os.Stdout

func CheckResponse

func CheckResponse(err error, expectedStatusCode int, response *http.Response)

CheckResponse fatals if err is nil or the response.StatusCode does not match the expectedStatusCode

func EnvVarExamplesHelpMessage

func EnvVarExamplesHelpMessage(name string) string

EnvVarExamplesHelpMessage returns a string containing documentation on how to use environment variables.

func ExactArgs

func ExactArgs(cmd *cobra.Command, args []string, l int)

ExactArgs fatals if args does not equal l.

func Exec

func Exec(_ *testing.T, cmd *cobra.Command, stdIn io.Reader, args ...string) (string, string, error)

Exec runs the provided cobra command with the given reader as STD_IN and the given args. Returns STD_OUT, STD_ERR and the error from the execution.

func ExecExpectedErr

func ExecExpectedErr(t *testing.T, cmd *cobra.Command, args ...string) string

ExecExpectedErr is a helper that assumes a failing run from Exec returning ErrNoPrintButFail Returns STD_ERR.

func ExecNoErr

func ExecNoErr(t *testing.T, cmd *cobra.Command, args ...string) string

ExecNoErr is a helper that assumes a successful run from Exec. Returns STD_OUT.

func ExpectDependency

func ExpectDependency(dependencies ...interface{})

ExpectDependency expects every dependency to be not nil or it fatals.

func FailSilently

func FailSilently(cmd *cobra.Command) error

FailSilently is supposed to be used within a commands RunE function. It silences cobras error handling and returns the ErrNoPrintButFail error.

func Fatalf

func Fatalf(message string, args ...interface{})

Fatalf prints to os.Stderr and exists with code 1.

func FormatResponse

func FormatResponse(o interface{}) string

FormatResponse takes an object and prints a json.MarshalIdent version of it or fatals.

func Generate

func Generate(cmd *cobra.Command, args []string) error

Generate generates markdown documentation for a cobra command and its children.

func MinArgs

func MinArgs(cmd *cobra.Command, args []string, min int)

MinArgs fatals if args does not satisfy min.

func Must

func Must(err error, message string, args ...interface{})

Must fatals with the optional message if err is not nil.

func MustGetBool

func MustGetBool(cmd *cobra.Command, name string) bool

MustGetBool returns a bool flag or fatals if an error occurs.

func MustGetDuration

func MustGetDuration(cmd *cobra.Command, name string) time.Duration

MustGetDuration returns a time.Duration flag or fatals if an error occurs.

func MustGetInt

func MustGetInt(cmd *cobra.Command, name string) int

MustGetInt returns a int flag or fatals if an error occurs.

func MustGetString

func MustGetString(cmd *cobra.Command, name string) string

MustGetString returns a string flag or fatals if an error occurs.

func MustGetStringSlice

func MustGetStringSlice(cmd *cobra.Command, name string) []string

MustGetStringSlice returns a []string flag or fatals if an error occurs.

func PrintCollection

func PrintCollection(cmd *cobra.Command, collection OutputCollection)

func PrintErrors

func PrintErrors(cmd *cobra.Command, errs map[string]error)

func PrintRow

func PrintRow(cmd *cobra.Command, row OutputEntry)

func RangeArgs

func RangeArgs(cmd *cobra.Command, args []string, r []int)

RangeArgs fatals if args does not satisfy any of the lengths set in r.

func RegisterFormatFlags

func RegisterFormatFlags(flags *pflag.FlagSet)

func Version

func Version(gitTag, gitHash, buildTime *string) *cobra.Command

Version returns a *cobra.Command that handles the `version` command.

Types

type OutputCollection

type OutputCollection interface {
	OutputHeader
	Table() [][]string
	Interface() interface{}
	Len() int
}

type OutputEntry

type OutputEntry interface {
	OutputHeader
	Fields() []string
	Interface() interface{}
}

type OutputHeader

type OutputHeader interface {
	Header() []string
}

Jump to

Keyboard shortcuts

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