Back to godoc.org
github.com/turbinelabs/cli/command

package command

v0.0.0 (f808c62)
Latest Go to latest
Published: Oct 25, 2018 | License: Apache-2.0 | Module: github.com/turbinelabs/cli

Overview

The command package provides an abstraction for a command-line application sub-command, a means to execute code when that sub-command is invoked, a means to report success/failure status of said code, and generic implementations of help and version sub-commands.

Index

Constants

const (
	//CmdErrCodeNoError is the CmdErrCode returns when there is no error
	CmdErrCodeNoError CmdErrCode = 0

	// CmdErrCodeError is the CmdErrCode returned for a generic error
	CmdErrCodeError = 1

	// CmdErrCodeBadInput is the CmdErrorCode returned for bad input
	CmdErrCodeBadInput = 2 // Bad Input Error
)

type Cmd

type Cmd struct {
	Name        string       // Name of the Command and the string to use to invoke it
	Summary     string       // One-sentence summary of what the Command does
	Usage       string       // Usage options/arguments
	Description string       // Detailed description of command
	Flags       flag.FlagSet // Set of flags associated with this Cmd, which typically configure the Runner
	Runner      Runner       // The code to run when this Cmd is invoked
}

A Cmd represents a named sub-command for a command-line application.

func (*Cmd) BadInput

func (c *Cmd) BadInput(args ...interface{}) CmdErr

BadInput produces a Cmd-scoped CmdErr with an exit code of 2, based on the given args, which are passed to fmt.Sprint.

func (*Cmd) BadInputf

func (c *Cmd) BadInputf(format string, args ...interface{}) CmdErr

BadInputf produces a Cmd-scoped CmdErr with an exit code of 2, based on the given format string and args, which are passed to fmt.Sprintf.

func (*Cmd) Error

func (c *Cmd) Error(args ...interface{}) CmdErr

Error produces a Cmd-scoped CmdErr with an exit code of 1, based on the given args, which are passed to fmt.Sprint.

func (*Cmd) Errorf

func (c *Cmd) Errorf(format string, args ...interface{}) CmdErr

Errorf produces a Cmd-scoped CmdErr with an exit code of 1, based on the given format string and args, which are passed to fmt.Sprintf.

func (*Cmd) Run

func (c *Cmd) Run() CmdErr

Run invokes the Runner associated with this Cmd, passing the args remaining after flags are parsed out. Names of Flags that have been marked as required by wrapping their Usage strings in turbinelabs/nonstdlib/flag.Required() and for which no value has been set will be returned to the caller in a Cmd.BadInput.

type CmdErr

type CmdErr struct {
	Cmd     *Cmd       // The Cmd that produced the exit status. Can be nil for global errors
	Code    CmdErrCode // The exit code
	Message string     // Additional information if the Code is non-zero
}

CmdErr represents the exit status of a Cmd.

func NoError

func NoError() CmdErr

NoError returns the singleton unscoped CmdErr with an exit code of 0

func (CmdErr) IsError

func (err CmdErr) IsError() bool

IsError returns true if the exit code is non-zero

type CmdErrCode

type CmdErrCode uint32

CmdErrCode is the exit code for the application

type MockRunner

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

MockRunner is a mock of Runner interface

func NewMockRunner

func NewMockRunner(ctrl *gomock.Controller) *MockRunner

NewMockRunner creates a new mock instance

func (*MockRunner) EXPECT

func (m *MockRunner) EXPECT() *MockRunnerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockRunner) Run

func (m *MockRunner) Run(cmd *Cmd, args []string) CmdErr

Run mocks base method

type MockRunnerMockRecorder

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

MockRunnerMockRecorder is the mock recorder for MockRunner

func (*MockRunnerMockRecorder) Run

func (mr *MockRunnerMockRecorder) Run(cmd, args interface{}) *gomock.Call

Run indicates an expected call of Run

type Runner

type Runner interface {
	// Execute code associated with a Cmd with the given arguments,
	// return exit status. The Cmd is provided here to avoid a circular
	// dependency between the Runner and the Cmd.
	Run(cmd *Cmd, args []string) CmdErr
}

A Runner represents the executable code associated with a Cmd. Typically the struct implementing Runner will include whatever state is needed, configured by the Flags in the associated Cmd.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier