config

package
v0.0.0-...-1096d29 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package config holds twoctl's per-environment credentials and base URLs.

Each named environment is a "context" with its own base URL and its own API key. Contexts live in ~/.config/twoctl/config.yaml; API keys live in the OS keychain under service=twoctl, account=ctx:<name>.

One context is "current" at any time. Commands resolve their key + URL in this order:

  1. --api-key flag (key) and --url / --env flags (URL)
  2. TWO_API_KEY env var
  3. current context's keychain entry and base URL

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAPIKey = errors.New("no API key found - set TWO_API_KEY, pass --api-key, or run `twoctl auth login`")

ErrNoAPIKey is returned when no key can be located for the active context.

View Source
var ErrNoContext = errors.New("no context configured - run `twoctl config set-context <name> --url <url>` or pass --env / --url")

ErrNoContext is returned when no context is configured and no fallback flags (--url / --env) were supplied.

Functions

func DeleteContext

func DeleteContext(name string) error

DeleteContext removes a context and its keychain entry.

func DeleteKey

func DeleteKey(ctxName string) error

DeleteKey removes the keychain entry for the given context. A missing entry is not an error.

func HasStoredKey

func HasStoredKey(ctxName string) bool

HasStoredKey reports whether the keychain currently holds a key for the given context.

func SaveFile

func SaveFile(f *File) error

SaveFile atomically writes the config file. The config directory is chmod'd to 0o700 and the file itself to 0o600 so other users on shared hosts can't read context URLs (or any future cached state).

Callers that mutate config (SetContext / UseContext / DeleteContext) wrap the load-modify-write cycle in withLock(); SaveFile itself is the final atomic step (CreateTemp + Rename).

func SetContext

func SetContext(name, baseURL, apiKey string) error

SetContext inserts or replaces a context. If apiKey is non-empty it is stored in the keychain under that context's account. The full read- modify-write cycle holds an exclusive flock on config.yaml.lock so two concurrent twoctl runs don't lose each other's updates.

func StoreKey

func StoreKey(ctxName, apiKey string) error

StoreKey writes apiKey to the keychain for the given context.

func UseContext

func UseContext(name string) error

UseContext switches the current context. The context must already exist. Held under config.yaml.lock for the read-modify-write window.

Types

type Context

type Context struct {
	Name    string `yaml:"name"`
	BaseURL string `yaml:"base-url"`
}

Context holds the per-environment settings for one named context.

func ListContexts

func ListContexts() ([]Context, string, error)

ListContexts returns contexts sorted by name. The active context's name is also returned for marking in UI output.

type File

type File struct {
	CurrentContext string             `yaml:"current-context"`
	Contexts       map[string]Context `yaml:"contexts"`
}

File is the on-disk shape of ~/.config/twoctl/config.yaml.

func LoadFile

func LoadFile() (*File, error)

LoadFile reads the config file. A missing file is not an error; a fresh File is returned instead.

type Resolved

type Resolved struct {
	APIKey      string
	BaseURL     string
	ContextName string
	Source      string // human-readable provenance, used by `whoami`
}

Resolved captures the materialised configuration for a single invocation.

func Resolve

func Resolve(flagKey, envFlag, rawURL string) (*Resolved, error)

Resolve picks a context (optionally overridden by --env / --url) and pairs it with an API key (optionally overridden by --api-key or TWO_API_KEY).

envFlag may be a known context name, a built-in alias (sandbox/prod/...), or any user-defined name; rawURL is a direct URL override.

Jump to

Keyboard shortcuts

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