common

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package common contains general-purpose types and helpers. Ideally, it should remain as small as possible.

Index

Constants

View Source
const TimeFormat = "2006-01-02 15:04"

TimeFormat is a format string for time.Format that reflects what the Fastly web UI uses.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base struct {
	CmdClause *kingpin.CmdClause
	Globals   *config.Data
}

Base is stuff that should be included in every concrete command.

func (Base) Name

func (b Base) Name() string

Name implements the Command interface, and returns the FullCommand from the kingpin.Command that's used to select which command to actually run.

type Command

type Command interface {
	Name() string
	Exec(in io.Reader, out io.Writer) error
}

Command is an interface that abstracts over all of the concrete command structs. The Name method lets us select which command should be run, and the Exec method invokes whatever business logic the command should do.

func SelectCommand

func SelectCommand(name string, commands []Command) (Command, bool)

SelectCommand chooses the command matching name, if it exists.

type Globals

type Globals struct {
	Token   string
	Verbose bool
	Client  api.Interface
}

Globals are flags and other stuff that's useful to every command. Globals are passed to each concrete command's constructor as a pointer, and are populated after a call to Parse. A concrete command's Exec method can use any of the information in the globals.

type Optional

type Optional struct {
	WasSet bool
}

Optional models an optional type that consumers can use to assert whether the inner value has been set and is therefore valid for use.

func (*Optional) Set

Set implements kingpin.Action and is used as callback to set that the optional inner value is valid.

type OptionalBool

type OptionalBool struct {
	Optional
	Value bool
}

OptionalBool models an optional boolean flag value.

type OptionalInt added in v0.9.0

type OptionalInt struct {
	Optional
	Value int
}

OptionalInt models an optional int flag value.

type OptionalString

type OptionalString struct {
	Optional
	Value string
}

OptionalString models an optional string flag value.

type OptionalStringSlice

type OptionalStringSlice struct {
	Optional
	Value []string
}

OptionalStringSlice models an optional string slice flag value.

type OptionalUint

type OptionalUint struct {
	Optional
	Value uint
}

OptionalUint models an optional uint flag value.

type OptionalUint8 added in v0.9.0

type OptionalUint8 struct {
	Optional
	Value uint8
}

OptionalUint8 models an optional unit8 flag value.

type Registerer

type Registerer interface {
	Command(name, help string) *kingpin.CmdClause
}

Registerer abstracts over a kingpin.App and kingpin.CmdClause. We pass it to each concrete command struct's constructor as the "parent" into which the command should install itself.

type StreamingExec added in v0.18.0

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

StreamingExec models a generic command execution that consumers can use to execute commands and stream their output to an io.Writer. For example compute commands can use this to standardize the flow control for each compiler toolchain.

func NewStreamingExec added in v0.18.0

func NewStreamingExec(cmd string, args, env []string, verbose bool, out io.Writer) *StreamingExec

NewStreamingExec constructs a new StreamingExec instance.

func (StreamingExec) Exec added in v0.18.0

func (s StreamingExec) Exec() error

Exec executes the compiler command and pipes the child process stdout and stderr output to the supplied io.Writer, it waits for the command to exit cleanly or returns an error.

type SyncWriter

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

SyncWriter protects any io.Writer with a mutex.

func NewSyncWriter

func NewSyncWriter(w io.Writer) *SyncWriter

NewSyncWriter wraps an io.Writer with a mutex.

func (*SyncWriter) Write

func (w *SyncWriter) Write(p []byte) (int, error)

Write implements io.Writer with mutex protection.

type UndoFn added in v0.3.0

type UndoFn func() error

UndoFn is a function with no arguments which returns an error or nil.

type UndoStack added in v0.3.0

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

UndoStack models a simple undo stack which consumers can use to store undo stateful functions, such as a function to teardown API state if something goes wrong during procedural commands, for example deleting a Fastly service after it's been created.

func NewUndoStack added in v0.3.0

func NewUndoStack() *UndoStack

NewUndoStack constructs a new UndoStack.

func (*UndoStack) Len added in v0.3.0

func (s *UndoStack) Len() int

Len method returns the number of elements in the UndoStack.

func (*UndoStack) Pop added in v0.3.0

func (s *UndoStack) Pop() UndoFn

Pop method pops last added UndoFn element oof the stack and returns it. If stack is empty Pop() returns nil.

func (*UndoStack) Push added in v0.3.0

func (s *UndoStack) Push(elem UndoFn)

Push method pushes an Undoer element onto the UndoStack.

func (*UndoStack) RunIfError added in v0.3.0

func (s *UndoStack) RunIfError(w io.Writer, err error)

RunIfError unwinds the stack if a non-nil error is passed, by serially calling each UndoFn function state in FIFO order. If any UndoFn returns an error, it gets logged to the provided writer. Should be deferrerd, such as:

undoStack := common.NewUndoStack()
defer func() { undoStack.RunIfError(w, err) }()

type Undoer added in v0.28.0

type Undoer interface {
	Pop() UndoFn
	Push(elem UndoFn)
	Len() int
	RunIfError(w io.Writer, err error)
}

Undoer represents the API of an UndoStack.

Jump to

Keyboard shortcuts

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