dotenv

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package dotenv loads KEY=VALUE pairs from .env files into a map. It is deliberately minimal: yottacode uses .env exclusively for API keys, so the parser only needs to cover what dev tooling already produces — quoted values, comments, optional `export` prefix.

The format follows the de-facto subset shared across Docker, direnv, and godotenv:

# comment line
KEY=value
KEY="quoted value"      # double quotes preserve as-is
KEY='single quoted'     # single quotes preserve as-is, no escapes
export KEY=value        # leading export is silently dropped
KEY=value # trailing comment after a space

Multi-line values, escape sequences inside double quotes, and variable interpolation are NOT supported. The moment we need any of those we'll reach for github.com/joho/godotenv; today we don't.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(m map[string]string) []string

Apply populates os.Environ with values from m, but never overrides a variable that's already set. The OS environment wins so CI/CD secrets take precedence over a checked-in .env. Returns the list of keys actually applied (for diagnostics).

func DefaultPath

func DefaultPath() (string, error)

DefaultPath returns ~/.yottacode/.env — the home-scoped, yottacode- managed env file where API keys land. Save/SetKey/DeleteKey use it as their default; callers needing a per-repo .env pass a path directly.

func DeleteKey

func DeleteKey(path, name string) (bool, error)

DeleteKey removes name from path and writes back. Returns (true, nil) when the key was present and the file was rewritten, (false, nil) when the file or key is missing (a no-op, not an error — the caller's typical question is "is the key gone?" and the answer is yes either way), or (false, err) on a real I/O failure. Used by `provider remove` to clean up the matching API key from ~/.yottacode/.env so removing a profile actually removes its credential.

func Load

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

Load parses the file at path and returns its KEY=VALUE pairs. A missing file is not an error — the caller can chain Load calls and treat absent files as empty layers.

func LoadAndApply

func LoadAndApply(paths ...string) ([]string, error)

LoadAndApply is the common-case helper: load a chain of files and merge into the live environment without overriding existing values.

func LoadChain

func LoadChain(paths ...string) (map[string]string, error)

LoadChain loads multiple .env files in order. Later files override earlier ones. Missing files are skipped silently. Callers pass the chain in "lowest precedence first" order — typically (user-global, project-local).

func Save

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

Save writes kv atomically to path at mode 0600. Keys are sorted so diffs read stably across writes; values are double-quoted so they survive any whitespace/special chars. A "managed" header tells users this file is rewritten by yottacode commands. Parent dirs are created at 0700 if missing. Empty kv writes just the header (a blank but valid yottacode-managed file).

func SetKey

func SetKey(path, name, value string) error

SetKey is the merge-and-write convenience: load existing entries from path (missing file = empty layer), set name=value, write back. Used by `provider add` flows that want to persist one new key without disturbing the rest.

Types

This section is empty.

Jump to

Keyboard shortcuts

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