userconfig

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Overview

Package userconfig manages the per-user CLI configuration: which provider the bare `m` command uses, the model id, and provider-specific connection details. API keys are intentionally excluded from this file — they live in the OS keychain (see keychain.go) so the YAML is safe to back up or sync.

Index

Constants

This section is empty.

Variables

View Source
var ErrSecretToolMissing = errors.New("secret-tool not found: install libsecret-tools (apt) or libsecret (dnf/pacman)")

ErrSecretToolMissing signals that the libsecret CLI (`secret-tool`) is not installed. The wizard catches this on Linux and offers to install it via the system package manager. Defined here (not in keychain_linux.go) so callers on every OS can reference it for cross-platform error matching with errors.Is.

Functions

func DeleteAPIKey

func DeleteAPIKey(provider Provider) error

DeleteAPIKey removes a stored API key. Idempotent: missing entries are not an error.

func Exists

func Exists() bool

Exists reports whether a config file is present. It does not validate it.

func GetAPIKey

func GetAPIKey(provider Provider) (string, error)

GetAPIKey retrieves a previously stored API key. Returns ErrKeyNotFound when no entry exists for that provider.

func GetAPIKeyByName added in v0.0.35

func GetAPIKeyByName(name string) (string, error)

GetAPIKeyByName retrieves a secret by arbitrary name.

func GetAPIKeyWithFallback

func GetAPIKeyWithFallback(provider Provider) (string, error)

GetAPIKeyWithFallback tries the keychain first, then falls back to the corresponding environment variable. This allows users without keychain tooling to simply export the API key in their shell.

Returns the key string if found in either location, or an error if neither exists. For providers that don't require an API key (e.g., local Ollama), returns empty string and no error.

func IsKeyNotFound

func IsKeyNotFound(err error) bool

IsKeyNotFound reports whether err indicates a missing keychain entry.

func IsNotExist

func IsNotExist(err error) bool

IsNotExist reports whether err indicates the config file is missing.

func Path

func Path() (string, error)

Path returns the absolute path of the config file (~/.config/m/config.yaml on Linux/macOS).

func Save

func Save(cfg *Config) error

Save writes the config atomically with mode 0600. Atomic write avoids a half-truncated file if the process is killed mid-write.

func SaveAPIKey

func SaveAPIKey(provider Provider, key string) error

SaveAPIKey stores an API key for a provider in the OS keychain. macOS uses the `security` CLI (always available); Linux uses `secret-tool` from libsecret-tools (must be installed — see keychain_linux.go).

func SaveAPIKeyByName added in v0.0.35

func SaveAPIKeyByName(name, key string) error

SaveAPIKeyByName stores a secret by arbitrary name (for MCP tokens etc).

func SaveState

func SaveState(st *State) error

SaveState writes state atomically with mode 0644 (not secret).

Types

type Config

type Config struct {
	Provider Provider `yaml:"provider"`
	Model    string   `yaml:"model"`
	// BaseURL is set for LiteLLM (the proxy endpoint) and may be set for
	// custom Ollama hosts. Empty for Anthropic / vanilla OpenAI.
	BaseURL string `yaml:"base_url,omitempty"`
	// DefaultAgent is an optional path to a custom .md agent file used by
	// bare `m` instead of the embedded default. Empty means use the builtin.
	DefaultAgent string `yaml:"default_agent,omitempty"`
}

Config is the on-disk schema. Fields are minimal on purpose: anything secret stays out (see keychain.go), and anything derivable from the model string is not duplicated.

func Load

func Load() (*Config, error)

Load reads and parses the config file. Returns fs.ErrNotExist when the file is missing so callers can distinguish "first run" from corruption.

type Provider

type Provider string

Provider identifies the LLM backend the default chat uses.

const (
	ProviderOllama    Provider = "ollama"
	ProviderAnthropic Provider = "anthropic"
	ProviderOpenAI    Provider = "openai"
	ProviderGemini    Provider = "gemini"
	ProviderAlibaba   Provider = "alibaba"
	ProviderLiteLLM   Provider = "litellm"
)

type State

type State struct {
	LastSeenVersion string `yaml:"last_seen_version,omitempty"`
}

State is the on-disk record of what version's release notes the user has already been shown. Kept separate from Config because it's machine-managed, not user-edited: the wizard never writes it, and nothing user-visible references it.

func LoadState

func LoadState() (*State, error)

LoadState returns the persisted state. Returns fs.ErrNotExist on first run (the file doesn't exist yet); callers should treat that as "everything is unseen" rather than an error.

Jump to

Keyboard shortcuts

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