cmd

package
v0.0.0-...-9f6392c Latest Latest
Warning

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

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

Documentation

Overview

Package cmd defines a testable CLI-like interface in order for the true CLI binary to be simple.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeAlreadySet is returned from a timeFlag parsing if the value has already been set.
	ErrTimeAlreadySet = errors.New("time flag has already been set")
)

Functions

func ComposeInitContext

func ComposeInitContext(ctx0 context.Context, cmps ...CommandComponent) (ctx context.Context, err error)

ComposeInitContext returns the sequenced context initialization of all provided components' InitContext functions, or the first error encountered.

func EndorseSet

func EndorseSet(ctx context.Context, setter func(ec *endorse.Context)) error

EndorseSet calls the given setter function on the context's endorse.Context if it exists, or returns the missing context error.

func EndorseSetE

func EndorseSetE(ctx context.Context, setter func(ec *endorse.Context) error) error

EndorseSetE returns the setter's result given the context's endorse.Context if it exists, or returns the missing context error.

func MakeApp

func MakeApp(ctx context.Context, app *AppComponents) *cobra.Command

MakeApp returns an initialized cobra root command for a CLI tool that includes all expected subcommands.

func MustBeNonempty

func MustBeNonempty(name string, value *string) error

MustBeNonempty returns an error if the given flag is empty.

Types

type AppComponents

type AppComponents struct {
	// Endorse abstracts any extra endorsement setup to do on top of the base before performing an
	// endorsement.
	Endorse CommandComponent
	// Global provides flags, validation, and context for every command. Its context must be
	// initialized explicitly.
	Global CommandComponent
	// SignatureRandom is the source of randomness used for certificate signature salting.
	SignatureRandom io.Reader
	// Bootstrap abstracts any extra setup to do on top of the base before performing key
	// bootstrapping.
	Bootstrap CommandComponent
	// Rotate abstracts any extra setup to do on top of the base before performing key rotation.
	Rotate CommandComponent
	// Wipeout abstracts any extra setup to do on top of the base before destroying all keys and
	// key certificates.
	Wipeout CommandComponent
}

AppComponents contains implementations of application interfaces needed to instantiate the entire GCE TCB CLI tool.

type BootstrapCommand

type BootstrapCommand struct{}

BootstrapCommand is the core bootstrap command component.

func (*BootstrapCommand) AddFlags

func (b *BootstrapCommand) AddFlags(cmd *cobra.Command)

AddFlags adds any implementation-specific flags for this command component.

func (*BootstrapCommand) InitContext

func (b *BootstrapCommand) InitContext(ctx context.Context) (context.Context, error)

InitContext extends the given context with whatever else the component needs before execution.

func (*BootstrapCommand) PersistentPreRunE

func (b *BootstrapCommand) PersistentPreRunE(cmd *cobra.Command, _ []string) error

PersistentPreRunE returns an error if the results of the parsed flags constitute an error.

type CommandComponent

type CommandComponent interface {
	// InitContext extends the given context with whatever else the component needs before execution.
	// This is separate from PersistentPreRunE to allow all flag validation code to run before
	// performing these potentially expensive initialization actions.
	InitContext(ctx context.Context) (context.Context, error)
	// AddFlags adds any implementation-specific flags for this command component.
	AddFlags(cmd *cobra.Command)
	// PersistentPreRunE returns an error if the results of the parsed flags constitute an error.
	PersistentPreRunE(cmd *cobra.Command, args []string) error
}

CommandComponent represents any setup that must happen before running a command.

func EndorseSetter

func EndorseSetter(setter func(ec *endorse.Context)) CommandComponent

EndorseSetter returns a CommandComponent that runs the given initialization function with the context's endorse.Context if present.

func EndorseSetterE

func EndorseSetterE(setter func(ec *endorse.Context) error) CommandComponent

EndorseSetterE returns a CommandComponent that whose InitContext returns the setter's result when given the context's endorse.Context. Returns the missing context error if not present.

type ComposedComponent

type ComposedComponent struct {
	Components []CommandComponent
}

ComposedComponent dispatches to the encapsulated components in sequence for each of their functions.

func Compose

func Compose(cmps ...CommandComponent) *ComposedComponent

Compose returns a component that composes all the given components in the order given.

func (*ComposedComponent) AddFlags

func (c *ComposedComponent) AddFlags(cmd *cobra.Command)

AddFlags adds any implementation-specific flags for the held command components.

func (*ComposedComponent) InitContext

func (c *ComposedComponent) InitContext(ctx context.Context) (context.Context, error)

InitContext extends the given context with whatever else the held components need before execution.

func (*ComposedComponent) PersistentPreRunE

func (c *ComposedComponent) PersistentPreRunE(cmd *cobra.Command, args []string) error

PersistentPreRunE returns an error if the results of the parsed flags constitute an error.

type PartialComponent

type PartialComponent struct {
	// InitContext extends the given context with whatever else the component needs before execution.
	FInitContext func(ctx context.Context) (context.Context, error)
	// AddFlags adds any implementation-specific flags for this command component.
	FAddFlags func(cmd *cobra.Command)
	// PersistentPreRunE returns an error if the results of the parsed flags constitute an error.
	FPersistentPreRunE func(cmd *cobra.Command, args []string) error
}

PartialComponent implements a CommandComponent with the provided functions. Missing fields have reasonable default behavior.

func (*PartialComponent) AddFlags

func (p *PartialComponent) AddFlags(cmd *cobra.Command)

AddFlags adds any implementation-specific flags for this command component.

func (*PartialComponent) InitContext

func (p *PartialComponent) InitContext(ctx context.Context) (context.Context, error)

InitContext extends the given context with whatever else the component needs before execution.

func (*PartialComponent) PersistentPreRunE

func (p *PartialComponent) PersistentPreRunE(cmd *cobra.Command, args []string) error

PersistentPreRunE returns an error if the results of the parsed flags constitute an error.

type RotateCommand

type RotateCommand struct{}

RotateCommand provides the core CommandComponent for executing a key rotation. This must be defined in cmd instead of on top of rotate.SignerKeyContext since rotate cannot depend on cmd to do so.

func (*RotateCommand) AddFlags

func (r *RotateCommand) AddFlags(cmd *cobra.Command)

AddFlags adds core flags for populating a rotate.SigningKeyContext.

func (*RotateCommand) InitContext

func (r *RotateCommand) InitContext(ctx context.Context) (context.Context, error)

InitContext extends the given context with whatever else the component needs before execution.

func (*RotateCommand) PersistentPreRunE

func (r *RotateCommand) PersistentPreRunE(cmd *cobra.Command, _ []string) error

PersistentPreRunE returns an error if the results of the parsed flags constitute an error.

type RunFn

type RunFn func(*cobra.Command, []string) error

RunFn is the type of a cobra error-returning runner function.

func ComposeRun

func ComposeRun(cmp CommandComponent, run func(context.Context) error) RunFn

ComposeRun will return run called with a command's context that has been extended by cmp's InitContext if cmp is non-nil.

Directories

Path Synopsis
Package output provides operations for command implementations to write information of various kinds.
Package output provides operations for command implementations to write information of various kinds.

Jump to

Keyboard shortcuts

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