cmdutil

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 21 Imported by: ? 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 commands share: the dependency Factory, argument validators and the --json/--jq/--template flags.

Index

Constants

View Source
const (
	HostFromFlag    = "flag"    // --host
	HostFromURL     = "url"     // an issue URL argument
	HostFromEnv     = "env"     // $YTRACK_HOST
	HostFromPath    = "path"    // a hosts.yml path glob matching the working directory
	HostFromDefault = "default" // default_host in hosts.yml
)

Sources of the host a command talks to, as HostResolution reports them.

Variables

This section is empty.

Functions

func AddJSONFlags

func AddJSONFlags(cmd *cobra.Command, target **output.Exporter, fields []string, opts ...JSONOption)

AddJSONFlags adds --json <fields>, --jq and --template to cmd. After flag parsing, *target is nil unless --json was given, in which case it holds an exporter limited to the requested fields. Requesting a field outside fields, or --jq/--template without --json, is a usage error.

func ExactArgs

func ExactArgs(n int, msg string) cobra.PositionalArgs

ExactArgs is cobra.ExactArgs returning a usage error with msg.

func GroupRunE

func GroupRunE(cmd *cobra.Command, args []string) error

GroupRunE makes a command group print its help when called bare and fail with a usage error on an unknown subcommand, instead of exiting 0.

func MinimumArgs added in v0.2.0

func MinimumArgs(n int, msg string) cobra.PositionalArgs

MinimumArgs is cobra.MinimumNArgs returning a usage error with msg.

func MutuallyExclusive

func MutuallyExclusive(message string, conditions ...bool) error

MutuallyExclusive returns a usage error when more than one condition holds.

func NoArgs

func NoArgs(cmd *cobra.Command, args []string) error

NoArgs rejects positional arguments with a usage error.

Types

type Factory

type Factory struct {
	IOStreams  *iostreams.IOStreams
	Prompter   prompter.Prompter
	Browser    browser.Browser
	AppVersion string
	Config     func() (*config.Config, error)
	// RoundTripper replaces the HTTP transport when non-nil (tests).
	RoundTripper http.RoundTripper
	// Now returns the current time; nil means time.Now (tests set it).
	Now func() time.Time
	// WorkDir returns the folder host paths are matched against; nil means
	// the process's working directory (tests set it).
	WorkDir func() (string, error)

	// Bound to the root command's persistent flags.
	HostFlag string
	Debug    bool
	// contains filtered or unexported fields
}

Factory carries the dependencies commands use instead of globals, so tests can swap IO, config, browser, prompts and HTTP.

func (*Factory) APIClient

func (f *Factory) APIClient() (*transport.Client, hosts.Host, error)

APIClient resolves the host and token and returns a client for them. It fails with an auth error (exit 4) when either is missing.

func (*Factory) Clock

func (f *Factory) Clock() time.Time

Clock returns the current time from Now, or time.Now.

func (*Factory) CredentialStore

func (f *Factory) CredentialStore(cfg *config.Config) auth.Store

CredentialStore returns the token store for the config directory.

func (*Factory) DebugEnabled

func (f *Factory) DebugEnabled() bool

DebugEnabled reports whether --debug or $YTRACK_DEBUG asks for HTTP traces.

func (*Factory) HostResolution added in v0.4.0

func (f *Factory) HostResolution(cfg *config.Config) (hosts.Host, Resolution, error)

HostResolution picks the host to talk to and says why: --host, then the host of an issue URL argument, then $YTRACK_HOST, then the logged-in host whose path glob matches the working directory, then the default host from hosts.yml. A known host keeps its stored URL (scheme and path prefix); an unknown one is normalized from the input.

func (*Factory) NewClient

func (f *Factory) NewClient(host hosts.Host, token string) *transport.Client

NewClient returns an API client for host using token. It warns when the host is reached over plain http and is not loopback.

func (*Factory) ResolveHost

func (f *Factory) ResolveHost(cfg *config.Config) (hosts.Host, error)

ResolveHost picks the host to talk to (see HostResolution).

func (*Factory) ResolveToken

func (f *Factory) ResolveToken(cfg *config.Config, host hosts.Host) (token, source string, err error)

ResolveToken returns the token for host and where it came from: $YTRACK_TOKEN wins over stored credentials. It returns "" when there is none. It warns when YTRACK_TOKEN goes to a host that is not in hosts.yml, unless the user named that host with --host or $YTRACK_HOST (the usual CI setup).

func (*Factory) UseURLHost

func (f *Factory) UseURLHost(serviceURL string)

UseURLHost makes serviceURL, taken from an issue URL argument, the host to talk to when neither --host nor $YTRACK_HOST names one.

func (*Factory) Warnf

func (f *Factory) Warnf(format string, args ...any)

Warnf prints a "! " warning to stderr once per distinct message.

type JSONOption

type JSONOption func(*jsonOptions)

JSONOption adjusts AddJSONFlags.

func WithoutJQShorthand

func WithoutJQShorthand() JSONOption

WithoutJQShorthand leaves --jq without its -q shorthand, for commands where -q means something else (issue list uses it for --query).

type Resolution added in v0.4.0

type Resolution struct {
	// Source is one of the HostFrom* values.
	Source string
	// Glob is the matching path glob when Source is HostFromPath.
	Glob string
}

Resolution says why a host was picked.

Jump to

Keyboard shortcuts

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