cli

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: MIT Imports: 54 Imported by: 0

Documentation

Overview

Package cli implements sq's CLI. The spf13/cobra library is used, with some notable modifications. Although cobra provides excellent functionality, it has some issues. Most prominently, its documentation suggests reliance upon package-level constructs for initializing the command tree (bad for testing).

Thus, this cmd package deviates from cobra's suggested usage pattern by eliminating all pkg-level constructs (which makes testing easier), and also replaces cobra's Command.RunE func signature with a signature that accepts as its first argument the RunContext type.

RunContext is similar to context.Context (and contains an instance of that), but also encapsulates injectable resources such as config and logging.

Update (Dec 2020): recent releases of cobra now support accessing Context from the cobra.Command. At some point it may make sense to revisit the way commands are constructed, to use this now-standard cobra mechanism.

The entry point to this pkg is the Execute function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute(ctx context.Context, stdin *os.File, stdout, stderr io.Writer, args []string) error

Execute builds a RunContext using ctx and default settings, and invokes ExecuteWith.

func ExecuteWith

func ExecuteWith(ctx context.Context, rc *RunContext, args []string) error

ExecuteWith invokes the cobra CLI framework, ultimately resulting in a command being executed. The caller must invoke rc.Close.

func WithRunContext added in v0.15.0

func WithRunContext(ctx context.Context, rc *RunContext) context.Context

WithRunContext returns ctx with rc added as a value.

Types

type RunContext

type RunContext struct {
	// Stdin typically is os.Stdin, but can be changed for testing.
	Stdin *os.File

	// Out is the output destination.
	// If nil, default to stdout.
	Out io.Writer

	// ErrOut is the error output destination.
	// If nil, default to stderr.
	ErrOut io.Writer

	// Cmd is the command instance provided by cobra for
	// the currently executing command. This field will
	// be set before the command's runFunc is invoked.
	Cmd *cobra.Command

	// Args is the arg slice supplied by cobra for
	// the currently executing command. This field will
	// be set before the command's runFunc is invoked.
	Args []string

	// Config is the run's config.
	Config *config.Config

	// ConfigStore is run's config store.
	ConfigStore config.Store

	// Log is the run's logger.
	Log lg.Log
	// contains filtered or unexported fields
}

RunContext is a container for injectable resources passed to all execX funcs. The Close method should be invoked when the RunContext is no longer needed.

func RunContextFrom added in v0.15.0

func RunContextFrom(ctx context.Context) *RunContext

RunContextFrom extracts the RunContext added to ctx via WithRunContext.

func (*RunContext) Close

func (rc *RunContext) Close() error

Close should be invoked to dispose of any open resources held by rc. If an error occurs during Close and rc.Log is not nil, that error is logged at WARN level before being returned.

Directories

Path Synopsis
Package buildinfo hosts build info variables populated via ldflags.
Package buildinfo hosts build info variables populated via ldflags.
Package config holds CLI configuration.
Package config holds CLI configuration.
Package output provides interfaces and implementations for outputting data and messages.
Package output provides interfaces and implementations for outputting data and messages.
csvw
Package csvw implements writers for CSV.
Package csvw implements writers for CSV.
htmlw
Package htmlw implements a RecordWriter for HTML.
Package htmlw implements a RecordWriter for HTML.
jsonw
Package jsonw implements output writers for JSON.
Package jsonw implements output writers for JSON.
markdownw
Package markdownw implements writers for Markdown.
Package markdownw implements writers for Markdown.
tablew
Package tablew implements text table output writers.
Package tablew implements text table output writers.
tablew/internal
Package tablewriter creates & generates text based table
Package tablewriter creates & generates text based table
xlsxw
Package xlsxw implements output writers for Microsoft Excel.
Package xlsxw implements output writers for Microsoft Excel.
xmlw
Package xmlw implements output writers for XML.
Package xmlw implements output writers for XML.

Jump to

Keyboard shortcuts

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