config

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

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

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

func Save

func Save(cfg *Config) error

Save saves the config to the default config path (0600 file, 0755 dir).

func SaveTo

func SaveTo(cfg *Config, path string) error

SaveTo saves the config to the specified path.

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

func Load() (*Config, error)

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

func LoadFrom(path string) (*Config, error)

LoadFrom loads config from the specified path. A missing file returns an empty Config without error.

func (*Config) DeleteHostAPIKey

func (c *Config) DeleteHostAPIKey(host string) error

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

func (c *Config) DeleteHostOAuth(host string) error

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

func (c *Config) ResolveHost(flagValue string) Resolved

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

func (c *Config) ResolveToken(host string) (Resolved, error)

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

func (c *Config) SetHostAPIKey(host, key string) error

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.

type Resolved

type Resolved struct {
	Value  string
	Source string
}

Resolved is a value together with its source label.

Jump to

Keyboard shortcuts

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