gh

package
v2.100.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package gh provides types that represent the domain of the CLI application.

For example, the CLI expects to be able to get and set user configuration in order to perform its functionality, so the Config interface is defined here, though the concrete implementation lives elsewhere. Though the current implementation of config writes to certain files on disk, that is an implementation detail compared to the contract laid out in the interface here.

Currently this package is in an early state but we could imagine other domain concepts living here for interacting with git or GitHub.

Index

Constants

This section is empty.

Variables

TokenTypes lists every recognised credential. TokenTypeUnknown is absent because its empty value prefixes every string, so matching against it would claim any token.

Functions

This section is empty.

Types

type AliasConfig

type AliasConfig interface {
	// Get retrieves the expansion for a specified alias.
	Get(alias string) (expansion string, err error)

	// Add adds a new alias with the specified expansion.
	Add(alias, expansion string)

	// Delete removes an alias.
	Delete(alias string) error

	// All returns a map of all aliases to their corresponding expansions.
	All() map[string]string
}

AliasConfig defines an interface for managing command aliases.

type AuthConfig

type AuthConfig interface {
	// HasActiveToken returns true when a token for the hostname is present.
	HasActiveToken(hostname string) bool

	// ActiveToken will retrieve the active auth token for the given hostname, searching environment variables,
	// general configuration, and finally encrypted storage.
	ActiveToken(hostname string) (token string, source string)

	// ActiveTokenType reports what kind of credential the active token for the
	// hostname is, so a caller can decide whether it will do without handling
	// the token itself.
	ActiveTokenType(hostname string) TokenType

	// HasEnvToken returns true when a token has been specified in an environment variable, else returns false.
	HasEnvToken() bool

	// TokenFromKeyring will retrieve the auth token for the given hostname, only searching in encrypted storage.
	TokenFromKeyring(hostname string) (token string, err error)

	// TokenFromKeyringForUser will retrieve the auth token for the given hostname and username, only searching
	// in encrypted storage.
	//
	// An empty username will return an error because the potential to return the currently active token under
	// surprising cases is just too high to risk compared to the utility of having the function being smart.
	TokenFromKeyringForUser(hostname, username string) (token string, err error)

	// ActiveUser will retrieve the username for the active user at the given hostname.
	//
	// This will not be accurate if the oauth token is set from an environment variable.
	ActiveUser(hostname string) (username string, err error)

	// Hosts retrieves a list of known hosts.
	Hosts() []string

	// APIHostForHost returns the api_host configured for host, reporting false
	// when the host has no api_host set. See config.AuthConfig.APIHostForHost.
	APIHostForHost(host string) (apiHost string, found bool)

	// HostForAPIHost returns the known host whose api_host is the given hostname,
	// reporting false when no host claims it. See config.AuthConfig.HostForAPIHost.
	HostForAPIHost(apiHost string) (host string, found bool)

	// DefaultHost retrieves the default host.
	DefaultHost() (host string, source string)

	// Login will set user, git protocol, and auth token for the given hostname.
	//
	// If the encrypt option is specified it will first try to store the auth token
	// in encrypted storage and will fall back to the general insecure configuration.
	Login(hostname, username, token, gitProtocol string, secureStorage bool) (insecureStorageUsed bool, err error)

	// SwitchUser switches the active user for a given hostname.
	SwitchUser(hostname, user string) error

	// Logout will remove user, git protocol, and auth token for the given hostname.
	// It will remove the auth token from the encrypted storage if it exists there.
	Logout(hostname, username string) error

	// UsersForHost retrieves a list of users configured for a specific host.
	UsersForHost(hostname string) []string

	// TokenForUser retrieves the authentication token and its source for a specified user and hostname.
	TokenForUser(hostname, user string) (token string, source string, err error)

	// SetActiveToken will override any token resolution and return the given token and source for all calls to
	// ActiveToken.
	// Use for testing purposes only.
	SetActiveToken(token, source string)

	// SetHosts will override any hosts resolution and return the given hosts for all calls to Hosts.
	// Use for testing purposes only.
	SetHosts(hosts []string)

	// SetDefaultHost will override any host resolution and return the given host and source for all calls to
	// DefaultHost.
	// Use for testing purposes only.
	SetDefaultHost(host, source string)
}

AuthConfig is used for interacting with some persistent configuration for gh, with knowledge on how to access encrypted storage when necessary. Behavior is scoped to authentication specific tasks.

type Config

type Config interface {
	// GetOrDefault provides primitive access for fetching configuration values, optionally scoped by host.
	GetOrDefault(hostname string, key string) o.Option[ConfigEntry]
	// Set provides primitive access for setting configuration values, optionally scoped by host.
	Set(hostname string, key string, value string)

	// AccessibleColors returns the configured accessible_colors setting, optionally scoped by host.
	AccessibleColors(hostname string) ConfigEntry
	// AccessiblePrompter returns the configured accessible_prompter setting, optionally scoped by host.
	AccessiblePrompter(hostname string) ConfigEntry
	// Browser returns the configured browser, optionally scoped by host.
	Browser(hostname string) ConfigEntry
	// ColorLabels returns the configured color_label setting, optionally scoped by host.
	ColorLabels(hostname string) ConfigEntry
	// Editor returns the configured editor, optionally scoped by host.
	Editor(hostname string) ConfigEntry
	// GitProtocol returns the configured git protocol, optionally scoped by host.
	GitProtocol(hostname string) ConfigEntry
	// HTTPUnixSocket returns the configured HTTP unix socket, optionally scoped by host.
	HTTPUnixSocket(hostname string) ConfigEntry
	// Pager returns the configured Pager, optionally scoped by host.
	Pager(hostname string) ConfigEntry
	// Prompt returns the configured prompt, optionally scoped by host.
	Prompt(hostname string) ConfigEntry
	// PreferEditorPrompt returns the configured editor-based prompt, optionally scoped by host.
	PreferEditorPrompt(hostname string) ConfigEntry
	// Spinner returns the configured spinner setting, optionally scoped by host.
	Spinner(hostname string) ConfigEntry
	// Telemetry returns the configured telemetry setting, ignoring host scoping since telemetry is a global setting.
	Telemetry() ConfigEntry

	// Aliases provides persistent storage and modification of command aliases.
	Aliases() AliasConfig

	// Authentication provides persistent storage and modification of authentication configuration.
	Authentication() AuthConfig

	// CacheDir returns the directory where the cacheable artifacts can be persisted.
	CacheDir() string

	// Migrate applies a migration to the configuration.
	Migrate(Migration) error

	// Version returns the current schema version of the configuration.
	Version() o.Option[string]

	// Write persists modifications to the configuration.
	Write() error
}

A Config implements persistent storage and modification of application configuration.

type ConfigEntry

type ConfigEntry struct {
	Value  string
	Source ConfigSource
}

type ConfigSource

type ConfigSource string
const (
	ConfigDefaultProvided ConfigSource = "default"
	ConfigUserProvided    ConfigSource = "user"
)

type Migration

type Migration interface {
	// PreVersion is the required config version for this to be applied
	PreVersion() string
	// PostVersion is the config version that must be applied after migration
	PostVersion() string
	// Do is expected to apply any necessary changes to the config in place
	Do(*ghConfig.Config) error
}

Migration is the interface that config migrations must implement.

Migrations will receive a copy of the config, and should modify that copy as necessary. After migration has completed, the modified config contents will be used.

The calling code is expected to verify that the current version of the config matches the PreVersion of the migration before calling Do, and will set the config version to the PostVersion after the migration has completed successfully.

type ProjectsV1Support added in v2.71.0

type ProjectsV1Support interface {
	// contains filtered or unexported methods
}

ProjectsV1Support provides type safety and readability around whether or not Projects v1 is supported by the targeted host.

It is a sealed type to ensure that consumers must use the exported ProjectsV1Supported and ProjectsV1Unsupported variables to get an instance of the type.

var (
	ProjectsV1Supported   ProjectsV1Support = projectsV1Supported{}
	ProjectsV1Unsupported ProjectsV1Support = projectsV1Unsupported{}
)

type TokenType added in v2.99.0

type TokenType string

TokenType is the kind of credential a token is, and its value is the prefix that identifies it. The zero value covers a token gh does not recognise, including an empty one.

See the token formats GitHub documents.

const (
	TokenTypeUnknown        TokenType = ""
	TokenTypeOAuth          TokenType = "gho_"
	TokenTypePersonalAccess TokenType = "ghp_"
	TokenTypeFineGrainedPAT TokenType = "github_pat_"
	TokenTypeUserToServer   TokenType = "ghu_"
	TokenTypeServerToServer TokenType = "ghs_"
	TokenTypeRefresh        TokenType = "ghr_"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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