Documentation
¶
Overview ¶
Package config handles loading, saving, and resolving configuration values for the melange CLI. Credential resolution follows this order: environment > environment file > explicitly selected config storage > keyring > legacy config fallback.
Index ¶
- Constants
- func ConfigDir() string
- func Save(cfg *Config) error
- func SaveTo(cfg *Config, path string) error
- type Config
- func (c *Config) DeleteHostAPIKey(host string) error
- func (c *Config) DeleteHostOAuth(host string) error
- func (c *Config) ResolveAnyTokenWith(host string, lookupPAT func(string) (string, bool, error), ...) (Resolved, *OAuthCredentials, error)
- func (c *Config) ResolveHost(flagValue string) Resolved
- func (c *Config) ResolveOAuth(host string, lookup func(string) (*OAuthCredentials, bool, error)) (*OAuthCredentials, string, error)
- func (c *Config) ResolveToken(host string) (Resolved, error)
- func (c *Config) ResolveTokenWith(host string, lookup func(host string) (string, bool, error)) (Resolved, error)
- func (c *Config) SetHostAPIKey(host, key string) error
- func (c *Config) SetHostOAuth(host string, creds OAuthCredentials) error
- type HostEntry
- type OAuthCredentials
- type Resolved
Constants ¶
const ( EnvHost = "MELANGE_HOST" EnvAPIKey = "MELANGE_API_KEY" EnvAPIKeyFile = "MELANGE_API_KEY_FILE" DefaultHost = "https://api.zetic.ai" // CredentialStorageConfig marks a host whose token was explicitly stored // in the mode-0600 config file after the user opted into // --insecure-storage. Resolution skips the unavailable keyring only for // this explicit per-host selection. CredentialStorageConfig = "config" )
Env variable names — centralized so callers don't hard-code strings.
Variables ¶
This section is empty.
Functions ¶
func ConfigDir ¶
func ConfigDir() string
ConfigDir returns the platform-appropriate directory for the config file.
- Linux/macOS: ${XDG_CONFIG_HOME:-$HOME/.config}/melange
- Windows: %AppData%\melange
Types ¶
type Config ¶
type Config struct {
Host string `yaml:"host"`
DefaultRepo string `yaml:"default_repo"`
Hosts map[string]HostEntry `yaml:"hosts"`
}
Config is the top-level config file schema.
func Load ¶
Load loads the config from the default config path. A missing file is not an error — it returns an empty Config. A corrupted YAML file returns an error.
func LoadFrom ¶
LoadFrom loads config from the specified path. A missing file returns an empty Config without error.
func (*Config) DeleteHostAPIKey ¶
DeleteHostAPIKey removes the API key for host from the config file and saves it. Removing an absent host is not an error.
func (*Config) DeleteHostOAuth ¶ added in v0.5.0
DeleteHostOAuth removes OAuth credentials for host.
func (*Config) ResolveAnyTokenWith ¶ added in v0.5.0
func (c *Config) ResolveAnyTokenWith(host string, lookupPAT func(string) (string, bool, error), lookupOAuth func(string) (*OAuthCredentials, bool, error)) (Resolved, *OAuthCredentials, error)
ResolveAnyTokenWith resolves the effective token with precedence: MELANGE_API_KEY > MELANGE_API_KEY_FILE (non-empty) > OAuth(fresh) > PAT keyring > PAT config. Empty MELANGE_API_KEY_FILE is treated as unset for OAuth path (falls through) but ResolveTokenWith preserves empty behavior.
func (*Config) ResolveHost ¶
ResolveHost returns the API host following the precedence chain:
flag > env:MELANGE_HOST > config.host > default
func (*Config) ResolveOAuth ¶ added in v0.5.0
func (c *Config) ResolveOAuth(host string, lookup func(string) (*OAuthCredentials, bool, error)) (*OAuthCredentials, string, error)
ResolveOAuth returns OAuth credentials for host, checking config vs keyring.
func (*Config) ResolveToken ¶
ResolveToken returns the API token for the given host without consulting a keyring. Call ResolveTokenWith for the full CLI credential resolution path. A set-but-unreadable MELANGE_API_KEY_FILE is a hard error.
func (*Config) ResolveTokenWith ¶
func (c *Config) ResolveTokenWith(host string, lookup func(host string) (string, bool, error)) (Resolved, error)
ResolveTokenWith is ResolveToken with an injected keyring lookup (nil = no keyring). The lookup is a plain func so this package does not import internal/keyring; commands pass keyring.Lookup. Precedence:
env:MELANGE_API_KEY > env:MELANGE_API_KEY_FILE > explicitly selected config storage > keyring > legacy config > empty
A keyring lookup failure remains a hard error unless this host explicitly selected config storage. This prevents an unavailable keyring from silently switching credentials.
func (*Config) SetHostAPIKey ¶
SetHostAPIKey stores an API key for host in the config file and saves it.
func (*Config) SetHostOAuth ¶ added in v0.5.0
func (c *Config) SetHostOAuth(host string, creds OAuthCredentials) error
SetHostOAuth stores OAuth credentials for host, clearing any PAT.
type HostEntry ¶
type HostEntry struct {
APIKey string `yaml:"api_key"`
Storage string `yaml:"storage,omitempty"`
OAuth *OAuthCredentials `yaml:"oauth,omitempty"`
}
HostEntry holds per-host credentials.
type OAuthCredentials ¶ added in v0.5.0
type OAuthCredentials struct {
AccessToken string `yaml:"access_token"`
RefreshToken string `yaml:"refresh_token"`
Expiry time.Time `yaml:"expiry,omitempty"`
ClientID string `yaml:"client_id"`
Scope string `yaml:"scope,omitempty"`
TokenType string `yaml:"token_type,omitempty"`
}
OAuthCredentials holds OAuth token pair for a host.