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
- Variables
- func AddJSONFlags(cmd *cobra.Command, exporter **Exporter, exportType any)
- func CommandPath(cmd *cobra.Command) string
- func CompleteArchivedInvoices(f *Factory) cobra.CompletionFunc
- func CompleteCustomerIDs(f *Factory) cobra.CompletionFunc
- func CompleteInputFile(exts ...string) cobra.CompletionFunc
- func CompleteTemplates(f *Factory) cobra.CompletionFunc
- func Confirm(ctx context.Context, ios *iostreams.IOStreams, question string) (bool, error)
- func DeprecateFlag(flags *pflag.FlagSet, name string)
- func DidYouMean(suggestions []string) string
- func FlagErrorFunc(cmd *cobra.Command, err error) error
- func FlagErrorf(command, format string, args ...any) error
- func HoldInterrupt(ctx context.Context) (release func())
- func OpenInEditor(ctx context.Context, ios *iostreams.IOStreams, ed *editor.Editor, ...) error
- func SupportPath(h invoice.Host, command string, kind invoice.SupportFile, ...) (string, error)
- func TemplatePath(h invoice.Host, command, flagValue, baseDir string) (string, error)
- func UnknownSubcommandError(cmd *cobra.Command, name string) error
- func WarnDeprecated(w io.Writer, old, new string)
- func WhyNoPrompt(ios *iostreams.IOStreams) string
- func WithInterruptHold(ctx context.Context, held *atomic.Int32) context.Context
- type ExecError
- type Exporter
- type Factory
- type FlagError
Constants ¶
const DeprecatedFlagAnnotation = "deprecated"
DeprecatedFlagAnnotation marks a flag that warns about itself when used, so the single-dash normaliser adds no warning of its own.
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 ¶
var CancelError = errors.New("CancelError")
CancelError means the user declined a prompt or interrupted it.
var SilentError = errors.New("SilentError")
SilentError means the command failed and has already reported why.
Functions ¶
func AddJSONFlags ¶
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 ¶
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 ¶
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 ¶
DeprecateFlag hides the flag name in flags and marks it deprecated. The command that owns it prints the warning.
func DidYouMean ¶
DidYouMean returns `; did you mean "a" or "b"?` for the suggested names, or "" when there are none.
func FlagErrorFunc ¶
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 ¶
FlagErrorf returns a *FlagError for command with a formatted message.
func HoldInterrupt ¶
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 ¶
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 ¶
UnknownSubcommandError is the usage error for an unknown subcommand of cmd, with the closest subcommands as a suggestion.
func WarnDeprecated ¶
WarnDeprecated prints the one-line warning that old is deprecated and new replaces it.
func WhyNoPrompt ¶
WhyNoPrompt returns why ios cannot prompt, or "" when it can.
Types ¶
type ExecError ¶
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.
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.
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 ¶
NewFactory returns a Factory whose adapters run programs with runner on the OS e.GOOS and read the editor settings with e.Getenv.