import "github.com/juju/cmd/v4"
var DefaultFormatters = map[string]Formatter{
"smart": FormatSmart,
"yaml": FormatYaml,
"json": FormatJson,
}
DefaultFormatters holds the formatters that can be specified with the --format flag.
var ErrNoPath = errors.New("path not set")
var ErrSilent = errors.New("cmd: error out silently")
ErrSilent can be returned from Run to signal that Main should exit with code 1 without producing error output.
var FormatJson = json.Marshal
FormatJson marshals value to a json-formatted []byte.
func CheckEmpty(args []string) error
CheckEmpty is a utility function that returns an error if args is not empty.
func FormatSmart(value interface{}) ([]byte, error)
FormatSmart marshals value into a []byte according to the following rules:
* string: untouched
* bool: converted to `True` or `False` (to match pyjuju)
* int or float: converted to sensible strings
* []string: joined by `\n`s into a single string
* anything else: delegate to FormatYaml
func FormatYaml(value interface{}) ([]byte, error)
FormatYaml marshals value to a yaml-formatted []byte, unless value is nil.
func IsErrSilent(err error) bool
IsErrSilent returns whether the error should be logged from cmd.Main.
func IsRcPassthroughError(err error) bool
IsRcPassthroughError returns whether the error is an RcPassthroughError.
func Main(c Command, ctx *Context, args []string) int
Main runs the given Command in the supplied Context with the given arguments, which should not include the command name. It returns a code suitable for passing to os.Exit.
func NewCommandLogWriter(name string, out, err io.Writer) loggo.Writer
NewCommandLogWriter creates a loggo writer for registration by the callers of a command. This way the logged output can also be displayed otherwise, e.g. on the screen.
func NewRcPassthroughError(code int) error
NewRcPassthroughError creates an error that will have the code used at the return code from the cmd.Main function rather than the default of 1 if there is an error.
func ParseAliasFile(aliasFilename string) map[string][]string
 ParseAliasFile will read the specified file and convert the content to a map of names to the command line arguments they relate to. The function will always return a valid map, even if it is empty.
func ZeroOrOneArgs(args []string) (string, error)
ZeroOrOneArgs checks to see that there are zero or one args, and returns the value of the arg if provided, or the empty string if not.
type AppendStringsValue []string
AppendStringsValue implements gnuflag.Value for a value that can be set multiple times, and it appends each value to the slice.
func NewAppendStringsValue(target *[]string) *AppendStringsValue
NewAppendStringsValue is used to create the type passed into the gnuflag.FlagSet Var function. f.Var(cmd.NewAppendStringsValue(&someMember), "name", "help")
func (v *AppendStringsValue) Set(s string) error
Implements gnuflag.Value Set.
func (v *AppendStringsValue) String() string
Implements gnuflag.Value String.
type Command interface {
// IsSuperCommand returns true if the command is a super command.
IsSuperCommand() bool
// Info returns information about the Command.
Info() *Info
// SetFlags adds command specific flags to the flag set.
SetFlags(f *gnuflag.FlagSet)
// Init initializes the Command before running.
Init(args []string) error
// Run will execute the Command as directed by the options and positional
// arguments passed to Init.
Run(ctx *Context) error
// AllowInterspersedFlags returns whether the command allows flag
// arguments to be interspersed with non-flag arguments.
AllowInterspersedFlags() bool
}
Command is implemented by types that interpret command-line arguments.
type CommandBase struct{}
CommandBase provides the default implementation for SetFlags, Init, and Help.
func (c *CommandBase) AllowInterspersedFlags() bool
AllowInterspersedFlags returns true by default. Some subcommands may want to override this.
func (c *CommandBase) Init(args []string) error
Init in the simplest case makes sure there are no args.
func (c *CommandBase) IsSuperCommand() bool
IsSuperCommand implements Command.IsSuperCommand
func (c *CommandBase) SetFlags(f *gnuflag.FlagSet)
SetFlags does nothing in the simplest case.
type Context struct {
Dir string
Env map[string]string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
// contains filtered or unexported fields
}
Context represents the run context of a Command. Command implementations should interpret file names relative to Dir (see AbsPath below), and print output and errors to Stdout and Stderr respectively.
func DefaultContext() (*Context, error)
DefaultContext returns a Context suitable for use in non-hosted situations.
func (ctx *Context) AbsPath(path string) string
AbsPath returns an absolute representation of path, with relative paths interpreted as relative to ctx.Dir.
func (ctx *Context) GetStderr() io.Writer
GetStderr satisfies environs.BootstrapContext
func (ctx *Context) GetStdin() io.Reader
GetStdin satisfies environs.BootstrapContext
func (ctx *Context) GetStdout() io.Writer
GetStdout satisfies environs.BootstrapContext
func (ctx *Context) Getenv(key string) string
Getenv looks up an environment variable in the context. It mirrors os.Getenv. An empty string is returned if the key is not set.
func (ctx *Context) Infof(format string, params ...interface{})
Infof will write the formatted string to Stderr if quiet is false, but if quiet is true the message is logged.
func (ctx *Context) InterruptNotify(c chan<- os.Signal)
InterruptNotify satisfies environs.BootstrapContext