envfile

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package envfile loads .env-style files, prompts interactively for missing required values, and writes them back. Designed for per-tool config that each binary keeps next to itself (e.g. bin/projects/<name>/.env).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(path string) (map[string]string, error)

Load parses a .env file. Missing files return an empty map and no error. Lines are KEY=VALUE; lines starting with `#` and blank lines are ignored. Surrounding double or single quotes are stripped from values.

func MigrateOnce

func MigrateOnce(currentPath, legacyPath string)

MigrateOnce copies legacyPath → currentPath on first run after the canonical location moves (e.g. when `.env` shifts under XDG). Best-effort: silently does nothing if currentPath already exists, legacyPath is missing, or any I/O step fails — the user will be prompted to refill the file through Resolve like a fresh install. On a successful copy a single-line notice is written to stderr so the move isn't invisible. The legacy file is left in place so downgrading to an older binary still finds its config.

func Resolve

func Resolve(path string, fields []Field) (map[string]string, error)

Resolve fetches values for every Field in priority order:

  1. existing entry in path
  2. process environment ($KEY)
  3. Default (silent fallback)
  4. interactive prompt (Required only) — saved back to path

All resolved values are returned. Stdin must be a TTY when prompting is needed; otherwise an error is returned listing the missing keys.

func Save

func Save(path string, vals map[string]string) error

Save writes the map to path in stable KEY=VALUE form, creating parent directories as needed. Values containing spaces or `#` are quoted.

Types

type Field

type Field struct {
	Key         string
	Prompt      string             // shown to user when interactive
	Description string             // short help shown above the prompt
	Default     string             // used silently if no value found anywhere
	Required    bool               // missing required → prompt (or error if non-TTY)
	Validator   func(string) error // returns error if value is invalid
}

Field describes a single environment variable a tool needs.

Jump to

Keyboard shortcuts

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