cobracli

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: BSD-3-Clause Imports: 5 Imported by: 54

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorPrinterWithDebugHandler

func ErrorPrinterWithDebugHandler(debugVar *bool, debugErrTransform func(error) string) func(*cobra.Command, error)

ErrorPrinterWithDebugHandler returns an error handler that prints the provided error as "Error: <error.Error()>" unless "error.Error()" is empty, in which case nothing is printed. If the provided boolean variable pointer is non-nil and the value is true, then the error output is provided to the specified error transform function before being printed.

func Execute

func Execute(rootCmd *cobra.Command, params ...Param) int

Execute executes the provided root command configured with the provided parameters. Returns an integer that should be used as the exit code for the application. Typical usage is "os.Exit(cobracli.Execute(...))" in a main function.

func ExecuteWithDebugVarAndDefaultParams

func ExecuteWithDebugVarAndDefaultParams(rootCmd *cobra.Command, debugVar *bool, params ...Param) int

ExecuteWithDebugVarAndDefaultParams executes the provided root command using the parameters returned by DefaultParams and the provided params. If the provided debugVar pointer is non-nil, adds a "--debug" boolean flag as a persistent flag on the command (unless the command already has a flag with that name). If the "--debug" flag is added by this function, then invoking the command with the flag will set the value of the variable pointed to by debugVar. If that variable is true, then if the command exits with an error, full stack traces will be printed if available as part of the error.

func ExecuteWithDefaultParams

func ExecuteWithDefaultParams(rootCmd *cobra.Command, params ...Param) int

ExecuteWithDefaultParams executes the provided root command using the parameters returned by DefaultParams and the provided params. Also adds a "--debug" boolean flag as a persistent flag on the command (unless the command already has a flag with that name). If the "--debug" flag is added by this function, then invoking the command with the flag will make it such that, if the command exits with an error, full stack traces will be printed if available as part of the error.

func FlagErrorsUsageErrorConfigurer

func FlagErrorsUsageErrorConfigurer(command *cobra.Command)

FlagErrorsUsageErrorConfigurer configures the provided command such that, when it encounters an error processing a flag, the returned error includes the usage string for the command.

func PrintUsageOnRequiredFlagErrorHandlerDecorator

func PrintUsageOnRequiredFlagErrorHandlerDecorator(fn func(*cobra.Command, error)) func(*cobra.Command, error)

PrintUsageOnRequiredFlagErrorHandlerDecorator decorates the provided error handler to add functionality that prints the command usage if the error that occurred was due to a required flag not being specified. This handler first processes the error using the provided handler. Then, it examines the string returned by the Error() function of the error to determine if it matches the form of an error that indicates that a required flag was missing. If so, the usage string of the command is printed.

func RemoveHelpCommandConfigurer

func RemoveHelpCommandConfigurer(command *cobra.Command)

RemoveHelpCommandConfigurer removes the "help" subcommand from the provided command.

func SilenceErrorsConfigurer

func SilenceErrorsConfigurer(command *cobra.Command)

SilenceErrorsConfigurer configures the provided command to silence the default behavior of printing errors and printing command usage on errors.

func VersionCmd

func VersionCmd(appName, version string) *cobra.Command

VersionCmd returns a command that prints the version of the application with the given name and given version to the Stdout of the command.

Types

type Param

type Param interface {
	// contains filtered or unexported methods
}

func AddDebugPersistentFlagParam

func AddDebugPersistentFlagParam(debug *bool) Param

AddDebugPersistentFlagParam adds "--debug" as a boolean persistent flag that sets the value of the provided *bool.

func ConfigureCmdParam

func ConfigureCmdParam(configureCmd func(*cobra.Command)) Param

ConfigureCmdParam adds the provided configuration function to the executor. All of the configuration functions on the executor are applied to the root command before it is executed.

func DefaultParams

func DefaultParams(debugVar *bool) []Param

DefaultParams returns a slice of Params that configures Cobra CLI execution with specific opinionated default behavior:

  • Sets SilenceErrors and SilenceUsage to true, which disables Cobra's built-in error and usage behavior. This prevents the behavior where usage is printed on any error returned by the command.
  • Registers a custom flag usage error handler that appends Cobra's command usage string to the errors encountered while parsing flags. This makes it such that errors that occur due to invalid flags do print the usage.
  • Registers an error printer that prints top-level errors as "Error: <error.Error()>" unless <error.Error()> is the empty string, in which case no error is printed. If the "debugVar" pointer is non-nil and its underlying value is true, then <error.Error()> is printed as a full verbose stack trace if it is a pkg/errors error. This printer is also configured to print the usage output for a command if the command returns an error that indicates that a required flag was not provided.

func ErrorHandlerParam

func ErrorHandlerParam(handler func(*cobra.Command, error)) Param

ErrorHandlerParam sets the error handler for the command. If executing the root command returns an error, the command that was executed is provided to the error handler.

func ExitCodeExtractorParam

func ExitCodeExtractorParam(extractor func(error) int) Param

ExitCodeExtractorParam sets the exit code extractor function for the executor. If executing the root command returns an error, the error is provided to the function and the code returned by the extractor is used as the exit code.

func VersionCmdParam

func VersionCmdParam(version string) Param

VersionCmdParam configures a command so that it has a "version" subcommand that prints the value of the provided version. Is a noop if the provided version is empty.

func VersionFlagParam

func VersionFlagParam(version string) Param

VersionFlagParam configures a command so that its "Version" field has the provided value. If it is non-empty, this will add a top-level "--version" flag that prints the version for that command.

Jump to

Keyboard shortcuts

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