handler

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2020 License: MPL-2.0, MPL-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version added in v0.1.1

func Version() string

Types

type DefaultSession added in v0.1.1

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

DefaultSession can be embedded in all mixins and sub-command handlers and provide a default implementation of the Session interface which uses os.Stdout, os.Stderr, and os.Stdin by default.

func (*DefaultSession) Err added in v0.1.1

func (s *DefaultSession) Err() io.Writer

func (*DefaultSession) ExitOnErr added in v0.1.1

func (s *DefaultSession) ExitOnErr(err error, msg string, code int)

func (*DefaultSession) ExitOnErrShort added in v0.1.1

func (s *DefaultSession) ExitOnErrShort(err error, msg string, code int)

func (*DefaultSession) ExitOnErrsShort added in v0.1.1

func (s *DefaultSession) ExitOnErrsShort(errs []error, code int)

func (*DefaultSession) Exitf added in v0.1.1

func (s *DefaultSession) Exitf(code int, format string, a ...interface{})

func (*DefaultSession) In added in v0.1.1

func (s *DefaultSession) In() io.Reader

func (*DefaultSession) OnSignal added in v0.1.1

func (s *DefaultSession) OnSignal(sig os.Signal, f func(os.Signal)) (cancel func())

func (*DefaultSession) Out added in v0.1.1

func (s *DefaultSession) Out() io.Writer

func (*DefaultSession) SetErr added in v0.1.1

func (s *DefaultSession) SetErr(w io.Writer)

func (*DefaultSession) SetIn added in v0.1.1

func (s *DefaultSession) SetIn(r io.Reader)

func (*DefaultSession) SetOut added in v0.1.1

func (s *DefaultSession) SetOut(w io.Writer)

type Input added in v0.1.1

type Input struct {
	// Args holds all unbound arguments except for the first "--" if present.
	Args []string

	// ArgsBeforeDash holds the subset of Args located before the first "--" if present,
	// or the same elements as Args if absent.
	ArgsBeforeDash []string

	// ArgsAfterDash holds the subset of Args located after the first "--" if present.
	ArgsAfterDash []string
}

Input defines the framework-agnostic user input passed to handler's Run methods.

type Mixin

type Mixin interface {
	// BindCobraFlags gives each mixin the opportunity to define its own flags.
	BindCobraFlags(cmd *cobra.Command) (requiredFlags []string)

	// Name should identify the mixin for use in error/log messages.
	//
	// Ideally it is short, e.g. like a package name.
	Name() string
}

type PostRun

type PostRun interface {
	PostRun(ctx context.Context)
}

PostRun is optionally implemented by handlers/mixins to perform tasks after Handler.Run finishes.

type PreRun

type PreRun interface {
	PreRun(ctx context.Context, args []string) error
}

PreRun is optionally implemented by handlers/mixins to perform tasks after flags are parsed but before Handler.Run.

If it returns an error, Handler.Run and Handler.PostRun do not execute.

type Session added in v0.1.1

type Session interface {
	// Err returns the standard error destination.
	Err() io.Writer

	// In returns the standard input source.
	In() io.Reader

	// OnSignal starts a goroutine which runs the provided function every time the signal
	// is received. It returns a function to end the goroutine.
	OnSignal(s os.Signal, do func(os.Signal)) (cancel func())

	// Out returns the standard output destination.
	Out() io.Writer

	// SetErr assigns the standard error destination.
	SetErr(io.Writer)

	// SetIn assigns the standard input source.
	SetIn(io.Reader)

	// SetOut assigns the standard error destination.
	SetOut(io.Writer)

	// ExitOnError exits the program if the error is non-nil and prints the error in full format (%+v).
	ExitOnErr(err error, msg string, code int)

	// ExitOnErrorShort exits the program if the error is non-nil and prints the error's string value.
	ExitOnErrShort(err error, msg string, code int)

	// ExitOnErrsShort exits the program if the slice is non-empty and prints each error's string value.
	ExitOnErrsShort(errs []error, code int)

	// Exitf exits the program with a formatted message.
	Exitf(code int, format string, a ...interface{})
}

Session defines CLI handler behaviors which are useful to replace with test doubles.

Behaviors related to terminal I/O are based on the interface approach in docker's CLI:

https://github.com/docker/cli/blob/f1b116179f2a95d0ea45780dfb8be51c2825b9c0/cli/command/cli.go
https://www.apache.org/licenses/LICENSE-2.0.html

Directories

Path Synopsis
mixin

Jump to

Keyboard shortcuts

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