Documentation
¶
Overview ¶
Package clictx carries the global per-invocation state (flags, cancellable context) attached to every cobra command, so subcommand packages can read it without importing the root cli package and creating an import cycle.
Index ¶
- Constants
- Variables
- func Attach(cmd *cobra.Command, ctx context.Context, flags *Flags)
- func CredentialsDir() (string, error)
- func UseProfileStore(res *resolve.Result) error
- type Flags
- func (g *Flags) ApplyPreferences() error
- func (g *Flags) AuthClient(cfg *config.Config, orgID string) (*api.AuthClient, error)
- func (g *Flags) CIToken(cfg *config.Config) (*auth.CIExchanger, error)
- func (g *Flags) CaptureChanged(cmd *cobra.Command)
- func (g *Flags) Config() (*config.Config, error)
- func (g *Flags) ConfigFile() (*config.File, error)
- func (g *Flags) DownloadClient() (*http.Client, error)
- func (g *Flags) HTTPClient() (*http.Client, error)
- func (g *Flags) HasOrgFlag() bool
- func (g *Flags) Org(ctx context.Context, cfg *config.Config) (api.Organization, error)
- func (g *Flags) OrgFlag() string
- func (g *Flags) RequireSession(cfg *config.Config) error
- func (g *Flags) Resolve() (*resolve.Result, error)
- func (g *Flags) ResolveAuth(cfg *config.Config) string
- func (g *Flags) ResolveWebApp(cfg *config.Config) string
- func (g *Flags) SetActiveOrg(o config.Organization) error
- func (g *Flags) StaticToken() (string, error)
- func (g *Flags) TokenSource(cfg *config.Config, audience, orgID string) (*auth.TokenSource, error)
- func (g *Flags) Transport() (transport.Config, error)
- func (g *Flags) UpdateProfile(fn func(p *config.Profile) error) error
Constants ¶
const ( FlagReadTimeout = "cli-read-timeout" FlagConnectTimeout = "cli-connect-timeout" FlagMaxAttempts = "max-attempts" )
Flag names shared with the root command.
Variables ¶
var ErrNeedsUserLogin = clierr.Usage(errors.New("this command needs a user sign-in (`norcube login`); a CI token (NORCUBE_TOKEN) can only call Norcube services. `norcube auth status` shows the token's organization"))
ErrNeedsUserLogin is returned by commands that act as a person (whoami, org, sessions, configure) when the invocation authenticates with a CI token, which belongs to an organization, not to a user.
Functions ¶
func Attach ¶
Attach installs flags on cmd's context. Call from the root command's PersistentPreRunE so children inherit it.
func CredentialsDir ¶ added in v0.16.0
CredentialsDir is where the file credential store keeps one file per profile: credentials/ next to the config file.
func UseProfileStore ¶ added in v0.16.0
UseProfileStore points the profile's credentials at the store it asked for: the file store for credential_store = "file", and also for "keyring" when no keyring is available (with a one-time notice).
Types ¶
type Flags ¶
type Flags struct {
Profile string
AuthOverride string
WebAppOverride string
OrgOverride string
EndpointURLs []string
TokenFile string
// Output is the resolved output format once the root command's
// PersistentPreRunE has run (-o > NORCUBE_OUTPUT > profile > defaults).
Output string
NoPager bool
// Query is the --query JMESPath expression applied to the result.
Query string
// HTTP behavior. Each wins over its NORCUBE_* variable only when the
// flag was given on the command line (see CaptureChanged).
// Debug traces HTTP requests to stderr (--debug, NORCUBE_DEBUG=1).
Debug bool
// DebugOut receives the trace; nil means stderr.
DebugOut io.Writer
ReadTimeoutSecs int
ConnectTimeoutSecs int
MaxAttempts int
// contains filtered or unexported fields
}
Flags are the values of the persistent flags declared on the root command, plus the configuration resolved from them.
func Get ¶
Get returns the Flags attached by Attach. Returns a zero Flags struct if nothing is attached, so callers don't need to nil-check.
func (*Flags) ApplyPreferences ¶ added in v0.16.0
ApplyPreferences settles the output format and paging before a command runs. A config file that can't be read doesn't stop commands that don't need it (config edit, config path, upgrade): only an explicitly invalid output format is an error here.
func (*Flags) AuthClient ¶ added in v0.16.0
AuthClient returns a Norcube Auth client authenticated for orgID, using the shared HTTP client.
func (*Flags) CIToken ¶ added in v0.18.0
CIToken returns the exchanger for the CI token given by NORCUBE_TOKEN or --token-file, or nil when the invocation doesn't use one (a profile session, or a plain access token). A value that starts like a CI token but fails the local checksum is a usage error, so a truncated paste is caught before any request. One exchanger serves the whole invocation, so each service's access token is fetched once.
func (*Flags) CaptureChanged ¶ added in v0.16.0
CaptureChanged records which HTTP flags were set explicitly, so their zero values ("no timeout") can be told apart from "not given". Call it once the flags are parsed.
func (*Flags) ConfigFile ¶ added in v0.16.0
ConfigFile returns the loaded config file.
func (*Flags) DownloadClient ¶ added in v0.16.0
DownloadClient returns the shared client for streaming downloads.
func (*Flags) HTTPClient ¶ added in v0.16.0
HTTPClient returns the shared client for API requests of this invocation.
func (*Flags) HasOrgFlag ¶
HasOrgFlag reports whether the organization was given for this command (--org or NORCUBE_ORG), as opposed to coming from the profile. A project's .langsync.json only applies when it wasn't.
func (*Flags) Org ¶ added in v0.16.0
Org returns the organization this invocation targets: --org or NORCUBE_ORG (an id or a slug, resolved against the organizations the user belongs to) wins over the profile's organization. The returned ID is empty when none is set. An unknown slug is a usage error.
func (*Flags) OrgFlag ¶ added in v0.18.0
OrgFlag is the organization given by --org or NORCUBE_ORG ("" when neither is set).
func (*Flags) RequireSession ¶ added in v0.16.0
RequireSession returns auth.ErrNotLoggedIn when this invocation has no credentials at all: no --token-file or NORCUBE_TOKEN and no session in the profile's credential store. A session that exists but was revoked is only discovered when a token is minted.
func (*Flags) Resolve ¶ added in v0.16.0
Resolve loads the config file (migrating an old one) and resolves the profile, endpoints, organization, credentials and output of this invocation. The result is computed once.
func (*Flags) ResolveAuth ¶
ResolveAuth returns the auth-service base URL of this invocation.
func (*Flags) ResolveWebApp ¶
ResolveWebApp returns the web-app base URL of this invocation.
func (*Flags) SetActiveOrg ¶ added in v0.16.0
func (g *Flags) SetActiveOrg(o config.Organization) error
SetActiveOrg makes o the selected profile's organization.
func (*Flags) StaticToken ¶ added in v0.16.0
StaticToken returns the access token given by --token-file or NORCUBE_TOKEN ("" when the profile's credential store is used).
func (*Flags) TokenSource ¶ added in v0.16.0
TokenSource returns a token source for (audience, org) of the selected profile that uses the shared HTTP client.