gcloud

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package gcloud is the sole interface for gcloud CLI configuration state. It reads and writes the config files directly; the only time it shells out to the gcloud binary is the interactive re-login offered after a switch.

Index

Constants

This section is empty.

Variables

View Source
var ErrExists = errors.New("configuration already exists")

ErrExists is returned when renaming onto a configuration that already exists.

View Source
var ErrInvalidName = errors.New("invalid configuration name")

ErrInvalidName is returned when a configuration name violates gcloud's naming rule (a lowercase letter followed by lowercase letters, digits, or hyphens).

View Source
var ErrNoConfigurations = errors.New("no gcloud configurations found")

ErrNoConfigurations is returned when the config root has no configurations.

View Source
var ErrNoPrevious = errors.New("no previous configuration")

ErrNoPrevious is returned when no previous configuration has been recorded.

View Source
var ErrNotConfigured = errors.New("gcloud not configured — run 'gcloud init'")

ErrNotConfigured is returned when the gcloud config root does not exist.

View Source
var ErrNotFound = errors.New("configuration not found")

ErrNotFound is returned when a named configuration does not exist.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client operates on a gcloud config root and gctx's own state file.

func New

func New(root, statePath, kubeCachePath string) *Client

New returns a Client rooted at the gcloud config directory. statePath is the file where gctx records the previously active configuration. kubeCachePath is the gke-gcloud-auth-plugin token cache, invalidated on every switch.

func (*Client) ClearLoginConfig added in v0.1.1

func (c *Client) ClearLoginConfig(name string) error

ClearLoginConfig removes the [auth] login_config_file property from a configuration, dropping the [auth] section when it becomes empty.

func (*Client) Current

func (c *Client) Current() (string, error)

Current returns the name of the active configuration. When the active_config pointer is absent, gcloud treats "default" as active.

func (*Client) List

func (c *Client) List() ([]Config, error)

List returns all configurations, sorted by name.

func (*Client) LoginConfigWarning added in v0.1.1

func (c *Client) LoginConfigWarning(name string) string

LoginConfigWarning returns a hint when the named configuration is a workforce account missing its login_config_file, or "" when there is nothing to warn.

func (*Client) LoginRequired

func (c *Client) LoginRequired(ctx context.Context, configs []Config) []bool

LoginRequired reports, per configuration (by position), whether its stored credentials are missing or can no longer produce an access token — i.e. a re-login is needed. Configurations without an account are never flagged. Checks perform live token refreshes and run concurrently, bounded by authCheckTimeout.

func (*Client) Previous

func (c *Client) Previous() (string, error)

Previous returns the last active configuration recorded before the most recent switch, or ErrNoPrevious when none exists.

func (*Client) Rename

func (c *Client) Rename(oldName, newName string) error

Rename renames a configuration, updating the active_config pointer and the recorded previous configuration when they refer to the old name.

func (*Client) SetLoginConfig added in v0.1.1

func (c *Client) SetLoginConfig(name, path string) error

SetLoginConfig persists the [auth] login_config_file property for a configuration, so its interactive login uses the workforce flow. The path must exist; it is stored as an absolute path.

func (*Client) Switch

func (c *Client) Switch(name string) error

Switch activates the named configuration and records the previously active one, unless the target is already active (a no-op).

func (*Client) VerifyAuth

func (c *Client) VerifyAuth(ctx context.Context, name string) *ReloginPlan

VerifyAuth checks whether the named configuration's credentials can still produce an access token, performing a live token refresh. It returns a ReloginPlan when a re-login is required, or nil when the credentials are valid or cannot be verified (best-effort).

type Config

type Config struct {
	Name    string
	Account string
	Project string
	Region  string
	// LoginConfigFile is the [auth] login_config_file property, set for
	// configurations that authenticate via a workforce/BeyondCorp login config.
	LoginConfigFile string
}

Config describes a single gcloud configuration.

func (Config) IsWorkforce added in v0.1.1

func (cfg Config) IsWorkforce() bool

IsWorkforce reports whether the configuration authenticates as a workforce identity federation principal.

func (Config) NeedsLoginConfig added in v0.1.1

func (cfg Config) NeedsLoginConfig() bool

NeedsLoginConfig reports a workforce configuration whose login_config_file is not persisted. Without it, gcloud (and gctx's re-login) silently fall back to the standard Google OAuth flow instead of the workforce login.

type ReloginPlan

type ReloginPlan struct {
	Config Config
}

ReloginPlan describes how to re-authenticate a configuration whose credentials have expired.

func (*ReloginPlan) Command

func (p *ReloginPlan) Command() string

Command returns the gcloud login command as it would be run.

func (*ReloginPlan) FixLoginConfigCommand

func (p *ReloginPlan) FixLoginConfigCommand() string

FixLoginConfigCommand returns the gcloud command to repoint the active configuration at a valid login config file.

func (*ReloginPlan) MissingLoginConfig

func (p *ReloginPlan) MissingLoginConfig() string

MissingLoginConfig returns the login_config_file path when the config points at one that does not exist — a dangling reference that would make the login command fail. It returns "" when there is no such problem.

func (*ReloginPlan) Run

func (p *ReloginPlan) Run() error

Run executes the login command, wiring the current process's stdio so the interactive login flow works.

Jump to

Keyboard shortcuts

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