output

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OutputKindJSON      = OutputKind("json")
	OutputKindNull      = OutputKind("")
	OutputKindPlaintext = OutputKind("plaintext")
)
View Source
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.

View Source
var ErrInvalidOutputKind = errors.NewError("output is invalid. Use 'json' or 'plaintext'")
View Source
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.

View Source
var MultiplePlaintextOutputFn = func(r resource) string {
	return fmt.Sprintf("* %s", SingularPlaintextOutputFn(r))
}

MultiplePlaintextOutputFn converts the resource to plain text.

View Source
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

func CmdOutput(action string, outputKind string, input []byte) (string, error)

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

func CmdOutputError(outputKind string, err error) string

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

func NewCmdOutputError(err error, outputKind string) error

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

type Outputter interface {
	JSON() string
	String() string
}

Outputter defines the different ways a command's response can be formatted based on user input.

type OutputterFn

type OutputterFn interface {
	New() (Outputter, error)
}

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

Jump to

Keyboard shortcuts

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