cmdutil

package
v0.0.0-...-6221d3e Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cmdutil holds what every command shares: the Factory, and the error types commands return so that cli.Main can decide, in one place, what to print and which exit code to use.

Index

Constants

View Source
const DeprecatedFlagAnnotation = "deprecated"

DeprecatedFlagAnnotation marks a flag that warns about itself when used, so the single-dash normaliser adds no warning of its own.

View Source
const JSONFieldsAnnotation = "help:json-fields"

JSONFieldsAnnotation holds a command's --json fields, comma-separated. The help page lists them and the shell completes them.

Variables

View Source
var CancelError = errors.New("CancelError")

CancelError means the user declined a prompt or interrupted it.

View Source
var SilentError = errors.New("SilentError")

SilentError means the command failed and has already reported why.

Functions

func AddJSONFlags

func AddJSONFlags(cmd *cobra.Command, exporter **Exporter, exportType any)

AddJSONFlags adds --json to cmd. Its fields are the json tags of the fields of exportType, a struct. When --json is given, *exporter is set before cmd runs; it stays nil otherwise. An unknown field is a usage error that lists the fields; --json without a value lists them and fails.

func CommandPath

func CommandPath(cmd *cobra.Command) string

CommandPath is cmd's path without the root command, for example "template list", or "" for the root.

func CompleteArchivedInvoices

func CompleteArchivedInvoices(f *Factory) cobra.CompletionFunc

CompleteArchivedInvoices completes the FILENAME argument of `archive edit` with the files `archive list` shows.

func CompleteCustomerIDs

func CompleteCustomerIDs(f *Factory) cobra.CompletionFunc

CompleteCustomerIDs completes the CUSTOMER_ID argument from customers.yaml, the -c, --customers file or the one the command would find.

func CompleteInputFile

func CompleteInputFile(exts ...string) cobra.CompletionFunc

CompleteInputFile completes the optional positional input of a command that also takes -i, --input: files with one of exts, and nothing once the input is given.

func CompleteTemplates

func CompleteTemplates(f *Factory) cobra.CompletionFunc

CompleteTemplates completes -t, --template with the names `template list` shows, and with file names when no name matches.

func Confirm

func Confirm(ctx context.Context, ios *iostreams.IOStreams, question string) (bool, error)

Confirm asks question on stderr and reads a yes/no answer. Anything but y or yes, including end of input, is a no. It returns CancelError when ctx is cancelled before an answer arrives.

func DeprecateFlag

func DeprecateFlag(flags *pflag.FlagSet, name string)

DeprecateFlag hides the flag name in flags and marks it deprecated. The command that owns it prints the warning.

func DidYouMean

func DidYouMean(suggestions []string) string

DidYouMean returns `; did you mean "a" or "b"?` for the suggested names, or "" when there are none.

func FlagErrorFunc

func FlagErrorFunc(cmd *cobra.Command, err error) error

FlagErrorFunc turns a cobra flag-parsing error into a *FlagError for cmd. --json without a value is the exception: it lists the fields and exits 1. An unknown long flag gets the closest flag name as a suggestion. An error in a global flag points to the root help, which documents it.

func FlagErrorf

func FlagErrorf(command, format string, args ...any) error

FlagErrorf returns a *FlagError for command with a formatted message.

func HoldInterrupt

func HoldInterrupt(ctx context.Context) (release func())

HoldInterrupt makes the signalContext that ctx comes from ignore SIGINT until release is called. A terminal sends Ctrl-C to every program in the foreground, so an editor invox waits on gets it too and decides what it means, as with git. SIGTERM, which only invox receives, still cancels ctx.

func OpenInEditor

func OpenInEditor(ctx context.Context, ios *iostreams.IOStreams, ed *editor.Editor, command, path, nextStep string) error

OpenInEditor opens path in ed for command. Without a terminal, or with prompting disabled, it starts no editor and returns a *FlagError that ends with nextStep, what the user can do instead.

func SupportPath

func SupportPath(h invoice.Host, command string, kind invoice.SupportFile, flagValue, baseDir string) (string, error)

SupportPath returns the absolute path of the support file kind that command reads: flagValue made absolute against baseDir when it is set, else the file found from baseDir. Finding none is a usage error that names the ways to provide it.

func TemplatePath

func TemplatePath(h invoice.Host, command, flagValue, baseDir string) (string, error)

TemplatePath returns the absolute path of the template that command renders with: flagValue, a path or the name of a template in the catalog, when it is set, else the template found from baseDir.

func UnknownSubcommandError

func UnknownSubcommandError(cmd *cobra.Command, name string) error

UnknownSubcommandError is the usage error for an unknown subcommand of cmd, with the closest subcommands as a suggestion.

func WarnDeprecated

func WarnDeprecated(w io.Writer, old, new string)

WarnDeprecated prints the one-line warning that old is deprecated and new replaces it.

func WhyNoPrompt

func WhyNoPrompt(ios *iostreams.IOStreams) string

WhyNoPrompt returns why ios cannot prompt, or "" when it can.

func WithInterruptHold

func WithInterruptHold(ctx context.Context, held *atomic.Int32) context.Context

WithInterruptHold returns ctx carrying held, the count of HoldInterrupt calls not yet released. The signal handler ignores SIGINT while it is above zero.

Types

type ExecError

type ExecError struct {
	Program string
	Code    int
	Err     error
}

ExecError is the failure of an external program. Code is the program's exit code, or -1 when it did not exit normally. invox itself exits 1.

func (*ExecError) Error

func (e *ExecError) Error() string

func (*ExecError) Unwrap

func (e *ExecError) Unwrap() error

type Exporter

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

Exporter writes the --json output of a command: the fields it was asked for, of each value it is given.

func (*Exporter) Write

func (e *Exporter) Write(ios *iostreams.IOStreams, data any) error

Write encodes the requested fields of data, a struct or a slice of structs, as one JSON document on ios.Out: indented on a terminal, compact otherwise. A slice is always an array, [] when it is empty. The whole document is encoded before anything is written.

type Factory

type Factory struct {
	IOStreams *iostreams.IOStreams
	Env       env.Env
	Compiler  *tectonic.Compiler
	Editor    *editor.Editor
	Opener    *opener.Opener
	// Mailer is nil where Apple Mail is not available.
	Mailer *applemail.Composer
	// ConfigFile is the --config value as typed, "" when not given. Main
	// sets it before anything calls Host.
	ConfigFile string
	// contains filtered or unexported fields
}

Factory holds what commands use to reach outside invox: the standard streams, the process environment and the external programs.

func NewFactory

func NewFactory(ios *iostreams.IOStreams, runner run.Runner, e env.Env) *Factory

NewFactory returns a Factory whose adapters run programs with runner on the OS e.GOOS and read the editor settings with e.Getenv.

func (*Factory) Host

func (f *Factory) Host() invoice.Host

Host returns the user directories, resolved from Env on the first call so that a test can set up its environment after building the Factory.

type FlagError

type FlagError struct {
	Command string
	Err     error
}

FlagError is a usage error: a bad flag, argument or subcommand. Command is the command whose help explains the usage, for example "customer list", or empty for the root command.

func (*FlagError) Error

func (e *FlagError) Error() string

func (*FlagError) Unwrap

func (e *FlagError) Unwrap() error

Jump to

Keyboard shortcuts

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