clictx

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

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

View Source
const (
	FlagReadTimeout    = "cli-read-timeout"
	FlagConnectTimeout = "cli-connect-timeout"
	FlagMaxAttempts    = "max-attempts"
)

Flag names shared with the root command.

Variables

View Source
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

func Attach(cmd *cobra.Command, ctx context.Context, flags *Flags)

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

func CredentialsDir() (string, error)

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

func UseProfileStore(res *resolve.Result) error

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

func Get(cmd *cobra.Command) *Flags

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

func (g *Flags) ApplyPreferences() error

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

func (g *Flags) AuthClient(cfg *config.Config, orgID string) (*api.AuthClient, error)

AuthClient returns a Norcube Auth client authenticated for orgID, using the shared HTTP client.

func (*Flags) CIToken added in v0.18.0

func (g *Flags) CIToken(cfg *config.Config) (*auth.CIExchanger, error)

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

func (g *Flags) CaptureChanged(cmd *cobra.Command)

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) Config added in v0.16.0

func (g *Flags) Config() (*config.Config, error)

Config returns the configuration this command runs with.

func (*Flags) ConfigFile added in v0.16.0

func (g *Flags) ConfigFile() (*config.File, error)

ConfigFile returns the loaded config file.

func (*Flags) DownloadClient added in v0.16.0

func (g *Flags) DownloadClient() (*http.Client, error)

DownloadClient returns the shared client for streaming downloads.

func (*Flags) HTTPClient added in v0.16.0

func (g *Flags) HTTPClient() (*http.Client, error)

HTTPClient returns the shared client for API requests of this invocation.

func (*Flags) HasOrgFlag

func (g *Flags) HasOrgFlag() bool

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

func (g *Flags) Org(ctx context.Context, cfg *config.Config) (api.Organization, error)

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

func (g *Flags) OrgFlag() string

OrgFlag is the organization given by --org or NORCUBE_ORG ("" when neither is set).

func (*Flags) RequireSession added in v0.16.0

func (g *Flags) RequireSession(cfg *config.Config) error

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

func (g *Flags) Resolve() (*resolve.Result, error)

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

func (g *Flags) ResolveAuth(cfg *config.Config) string

ResolveAuth returns the auth-service base URL of this invocation.

func (*Flags) ResolveWebApp

func (g *Flags) ResolveWebApp(cfg *config.Config) string

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

func (g *Flags) StaticToken() (string, error)

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

func (g *Flags) TokenSource(cfg *config.Config, audience, orgID string) (*auth.TokenSource, error)

TokenSource returns a token source for (audience, org) of the selected profile that uses the shared HTTP client.

func (*Flags) Transport added in v0.16.0

func (g *Flags) Transport() (transport.Config, error)

Transport returns the HTTP settings: flag > NORCUBE_* env > [defaults] in the config file > built-in default.

func (*Flags) UpdateProfile added in v0.16.0

func (g *Flags) UpdateProfile(fn func(p *config.Profile) error) error

UpdateProfile changes the selected profile in the config file (creating the profile when it doesn't exist yet) and re-resolves.

Jump to

Keyboard shortcuts

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