environment

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package environment manages environment variable collections and their origin tracking. It provides functionality for overlaying multiple environment sources and exporting them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	// Key is the environment variable name.
	Key string
	// Old is the previous value of the variable.
	Old string
	// New is the current value of the variable.
	New string
}

Change represents a single environment variable that has changed between two environments.

type Diff

type Diff struct {
	// Added contains variables present in B but not in A.
	Added env.Env
	// Removed contains variables present in A but not in B.
	Removed env.Env
	// Changed contains variables present in both with different values.
	Changed []Change
}

Diff represents the differences between two environments.

func Diffs

func Diffs(first, second env.Env) Diff

Diffs computes a structured diff of two environments.

func (Diff) Equal

func (d Diff) Equal() bool

Equal reports whether there are no differences.

func (Diff) Render

func (d Diff) Render(aName, bName string) error

Render prints a git-like unified diff. Lines: + added, - removed, ~ changed ("old -> new"). aName/bName are labels (e.g., "env1", "env2").

type Environment

type Environment struct {
	// Name is the name of the profile.
	Name string
	// Output is the output file for the profile.
	Output file.File

	// Env is the environment variables for the profile.
	Env env.Env
	// Origin tracks the source of each environment variable.
	Origin Origin
}

Environment represents the resolved environment variables for a named profile.

func New

func New(name, output string) Environment

New returns a new environment for the given profile, with default values for the output file.

func (*Environment) OverlayDotEnv

func (e *Environment) OverlayDotEnv(path, profile string) error

OverlayDotEnv overlays the environment variables from a .env file.

func (*Environment) OverlayOther

func (e *Environment) OverlayOther(other Environment)

OverlayOther overlays the environment variables from another environment.

func (*Environment) UpdateOrigin

func (e *Environment) UpdateOrigin(profile string, env env.Env)

UpdateOrigin updates the origin of the environment variables.

func (*Environment) Write

func (e *Environment) Write() error

Write saves the environment variables to a dotenv file.

type Formatter

type Formatter struct {
	// WithOrigin indicates whether to include the origin of the variable.
	WithOrigin bool
	// WithKey indicates whether to include the key in the output.
	WithKey bool
	// Prefix is a string to prepend to the variable value.
	Prefix string
	// Padding is the width of the output field.
	Padding int
}

Formatter holds the configuration for formatting environment variables.

func (Formatter) All

func (f Formatter) All(environment Environment) string

All formats all environment variables for output.

func (Formatter) Key

func (f Formatter) Key(key string, environment Environment) string

Key formats an environment variable for output.

type Heritage

type Heritage []string

Heritage represents the inheritance chain of an environment variable.

func (Heritage) String

func (h Heritage) String() string

String returns the string representation of the heritage chain.

type Origin

type Origin map[string]Heritage

Origin tracks environment variable keys to their sources.

func (*Origin) Add

func (o *Origin) Add(origin string, keys ...string)

Add adds a new origin for the given keys.

func (*Origin) Clear

func (o *Origin) Clear(keys ...string)

Clear removes all origins for the given keys.

Jump to

Keyboard shortcuts

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