Documentation
¶
Overview ¶
Package cmdutil holds what commands share: the dependency Factory, argument validators and the --json/--jq/--template flags.
Index ¶
- Constants
- func AddJSONFlags(cmd *cobra.Command, target **output.Exporter, fields []string, ...)
- func ExactArgs(n int, msg string) cobra.PositionalArgs
- func GroupRunE(cmd *cobra.Command, args []string) error
- func IssueProject(id string) string
- func MinimumArgs(n int, msg string) cobra.PositionalArgs
- func MutuallyExclusive(message string, conditions ...bool) error
- func NoArgs(cmd *cobra.Command, args []string) error
- type Factory
- func (f *Factory) APIClient() (*transport.Client, hosts.Host, error)
- func (f *Factory) Clock() time.Time
- func (f *Factory) CredentialStore(cfg *config.Config) auth.Store
- func (f *Factory) CurrentLogin(ctx context.Context, client *transport.Client, host hosts.Host) (string, error)
- func (f *Factory) DebugEnabled() bool
- func (f *Factory) HostResolution(cfg *config.Config) (hosts.Host, Resolution, error)
- func (f *Factory) NewClient(host hosts.Host, token string) *transport.Client
- func (f *Factory) ResolveHost(cfg *config.Config) (hosts.Host, error)
- func (f *Factory) ResolveToken(cfg *config.Config, host hosts.Host) (token, source string, err error)
- func (f *Factory) UseURLHost(serviceURL string)
- func (f *Factory) Warnf(format string, args ...any)
- func (f *Factory) WithIssueHost(ctx context.Context, id string, run func() error) error
- func (f *Factory) WithIssueHostForWrite(ctx context.Context, id string, run func() error) error
- type JSONOption
- type Resolution
Constants ¶
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. --jq or --template without --json exports every field, as `ytrack api --jq` needs no --json either. Requesting a field outside fields 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 ¶
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
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 ¶
MutuallyExclusive returns a usage error when more than one condition holds.
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 ¶
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) CredentialStore ¶
CredentialStore returns the token store for the config directory.
func (*Factory) CurrentLogin ¶ added in v0.6.2
func (f *Factory) CurrentLogin(ctx context.Context, client *transport.Client, host hosts.Host) (string, error)
CurrentLogin returns the login of the account client talks as on host. The login `auth login` saved in hosts.yml is the per-host cache while the stored token is in use; with $YTRACK_TOKEN, or without a saved login, it asks GET /api/users/me.
func (*Factory) DebugEnabled ¶
DebugEnabled reports whether --debug or $YTRACK_DEBUG asks for HTTP traces.
func (*Factory) HostResolution ¶ added in v0.4.0
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 ¶
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 ¶
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 ¶
UseURLHost makes serviceURL, taken from an issue URL argument, the host to talk to when neither --host nor $YTRACK_HOST names one.
func (*Factory) WithIssueHost ¶ added in v0.5.2
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. When the picked host lacks the project and two or more other hosts have it, the 404 ends with "; try --host <a> or --host <b>". Hosts whose `auth paths` fence them to other folders take no part: they are never probed, switched to or named. No other error ever suggests a host.
func (*Factory) WithIssueHostForWrite ¶ added in v0.5.2
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.