go.bigb.es/cacher/internal/config

go.bigb.es/cacher @ v0.1.2 — 9dc739e5 2 months ago

import "go.bigb.es/cacher/internal/config"

Overview

Package config loads cacher configuration with precedence: flag > env (CACHER_*) > config file (~/.config/cacher/config.toml) > defaults.

Functions

f func ApplyEnv

src
func ApplyEnv(cfg *Config) error

ApplyEnv overlays CACHER_<UPPER> environment variables onto cfg. Unset/empty variables are ignored (do not zero out file values).

f func DefaultPath

src
func DefaultPath() (string, error)

DefaultPath returns ~/.config/cacher/config.toml on every platform. We deliberately do not use os.UserConfigDir (which returns ~/Library/Application Support on macOS) because cacher is a CI tool that needs the same path on a developer's mac and on a Linux build runner.

f func ExpandPath

src
func ExpandPath(p string) string

ExpandPath expands a leading ~ in a path against $HOME.

f func Save

src
func Save(path string, cfg Config) error

Save writes the config to path (creating parent dirs). Empty path = DefaultPath.

Types

T type Config

src
type Config struct {
	Endpoint   string `toml:"endpoint"`
	Region     string `toml:"region"`
	Bucket     string `toml:"bucket"`
	Prefix     string `toml:"prefix"`
	ArchSuffix bool   `toml:"arch_suffix"`

	// KeyFile / SecretFile point at files that contain the raw credential
	// material (whitespace is stripped on load). Used in CI where secrets
	// are mounted as files. Env vars CACHER_S3_KEY_ID / CACHER_S3_SECRET
	// take priority for local-dev runs.
	KeyFile    string `toml:"key_file"`
	SecretFile string `toml:"secret_file"`
}

Config is the persisted cacher configuration. The TOML file under ~/.config/cacher/config.toml holds the same fields. Every field is overridable via CACHER_<UPPER> environment variables.

f func Defaults

src

Defaults returns built-in defaults. They are overlaid by file → env → flags.

f func Load

src
func Load(path string) (Config, error)

Load reads the config file at path (empty path = DefaultPath). A missing file is not an error — it returns Defaults(). Env vars and flag overlays must be applied by the caller afterwards.

m func (Config) Credentials

src
func (c Config) Credentials() (keyID, secret string, err error)

Credentials resolves the S3 access key + secret with precedence:

  1. CACHER_S3_KEY_ID / CACHER_S3_SECRET env vars
  2. files at cfg.KeyFile / cfg.SecretFile

Whitespace (including trailing newlines) is trimmed from file contents.

m func (Config) Validate

src
func (c Config) Validate() error

Validate ensures the minimum viable config for an S3 operation.

Source files