Documentation ¶
Index ¶
- Variables
- func CmdOutput(action string, outputKind string, input []byte) (string, error)
- func CmdOutputError(outputKind string, err error) string
- func CmdOutputSingular(outputKind string, input []byte, fn PlaintextOutputFn) (string, error)
- func NewCmdOutputError(err error, outputKind string) error
- type MultipleOutputter
- type OutputKind
- type Outputter
- type OutputterFn
- type PlaintextOutputFn
- type SingularOutputter
Constants ¶
This section is empty.
Variables ¶
var ( OutputKindJSON = OutputKind("json") OutputKindNull = OutputKind("") OutputKindPlaintext = OutputKind("plaintext") )
var ConfigPlaintextOutputFn = func(r resource) string { keys := make([]string, 0) for k := range r { keys = append(keys, k) } sort.Strings(keys) lst := make([]string, 0) for _, k := range keys { lst = append(lst, fmt.Sprintf("%s: %v", k, r[k])) } return strings.Join(lst, "\n") }
ConfigPlaintextOutputFn converts the resource to plain text specifically for data from the config file.
var ErrInvalidOutputKind = errors.NewError("output is invalid. Use 'json' or 'plaintext'")
var ErrorPlaintextOutputFn = func(r resource) string { switch { case r["code"] == nil && (r["message"] == "" || r["message"] == nil): return "unknown error occurred" case r["code"] == nil: return r["message"].(string) case r["message"] == "": return fmt.Sprintf("an error occurred (code: %s)", r["code"]) default: return fmt.Sprintf("%s (code: %s)", r["message"], r["code"]) } }
ErrorPlaintextOutputFn converts the resource to plain text specifically for data from the error file. An error response could have a code and message or just a message. It's also possible that there isn't either property.
var MultiplePlaintextOutputFn = func(r resource) string { return fmt.Sprintf("* %s", SingularPlaintextOutputFn(r)) }
MultiplePlaintextOutputFn converts the resource to plain text.
var SingularPlaintextOutputFn = func(r resource) string { email := r["email"] id := r["_id"] key := r["key"] name := r["name"] switch { case name != nil && key != nil: return fmt.Sprintf("%s (%s)", name.(string), key.(string)) case email != nil && id != nil: return fmt.Sprintf("%s (%s)", email.(string), id.(string)) case name != nil && id != nil: return fmt.Sprintf("%s (%s)", name.(string), id.(string)) case key != nil: return key.(string) case email != nil: return email.(string) case id != nil: return id.(string) default: return "cannot read resource" } }
SingularPlaintextOutputFn converts the resource to plain text based on its name and key.
Functions ¶
func CmdOutput ¶
CmdOutput returns a response from a resource action formatted based on the output flag along with an optional message based on the action.
func CmdOutputError ¶
CmdOutputError returns a response from a resource action error.
func CmdOutputSingular ¶
func CmdOutputSingular(outputKind string, input []byte, fn PlaintextOutputFn) (string, error)
CmdOutputSingular builds a command response based on the flag the user provided and the shape of the input. The expected shape is a single JSON object.
func NewCmdOutputError ¶ added in v1.2.0
NewCmdOutputError builds error output based on the error and output kind.
Types ¶
type MultipleOutputter ¶
type MultipleOutputter struct {
// contains filtered or unexported fields
}
func (MultipleOutputter) JSON ¶
func (o MultipleOutputter) JSON() string
func (MultipleOutputter) String ¶
func (o MultipleOutputter) String() string
type OutputKind ¶
type OutputKind string
func NewOutputKind ¶
func NewOutputKind(s string) (OutputKind, error)
func (OutputKind) String ¶
func (o OutputKind) String() string
type Outputter ¶
Outputter defines the different ways a command's response can be formatted based on user input.
type OutputterFn ¶
OutputterFn is a factory to build the right outputter. By adding an layer of abstraction, it lets us push back the error handling from where a caller provides the input to where the caller builds the outputter.
type PlaintextOutputFn ¶
type PlaintextOutputFn func(resource) string
PlaintextOutputFn represents the various ways to output a resource or resources.
type SingularOutputter ¶
type SingularOutputter struct {
// contains filtered or unexported fields
}
func (SingularOutputter) JSON ¶
func (o SingularOutputter) JSON() string
func (SingularOutputter) String ¶
func (o SingularOutputter) String() string