cmdutil

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 24 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
	HostFromKey     = "key"     // the logged-in host that has the issue key's project
	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 IssueProject added in v0.5.2

func IssueProject(id string) string

IssueProject returns the project short name of a readable issue ID (APP-123 gives APP), or "" for a database ID such as 2-1234.

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 that owns the issue key's project (see WithIssueHost), 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.

func (*Factory) WithIssueHost added in v0.5.2

func (f *Factory) WithIssueHost(ctx context.Context, id string, run func() error) error

WithIssueHost runs run, which talks to the host APIClient picks, for a command that reads issue id. When the folder rule or the default host picked the host (no --host, issue URL, $YTRACK_HOST or $YTRACK_TOKEN), more than one host is logged in, and the picked host does not have id's project while exactly one other logged-in host does, run talks to that host instead and one stderr line says so. The answer comes from the project cache when it knows; otherwise run first goes to the picked host and only a 404 triggers the project lookup (one paged read per host, cached for config.ProjectCacheTTL) and a single rerun.

func (*Factory) WithIssueHostForWrite added in v0.5.2

func (f *Factory) WithIssueHostForWrite(ctx context.Context, id string, run func() error) error

WithIssueHostForWrite is WithIssueHost for a command that changes issue id: it settles the host before run, looking the project up when the cache does not know, so no write request ever goes to a host that does not own the key.

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
	// Project is the issue key's project when Source is HostFromKey.
	Project string
}

Resolution says why a host was picked.

func (Resolution) Why added in v0.5.2

func (r Resolution) Why() string

Why completes "<host> chosen ..." for messages that explain the choice.

Jump to

Keyboard shortcuts

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