Output & generation

  1. Marshal — struct back to .env
  2. Usage — generate --help
  3. Example — generate .env.example

Marshal — struct back to .env

Marshal renders a struct into .env bytes (sorted KEY=value lines, values quoted and escaped when needed). MarshalMap returns the flat map[string]string instead. Prefixes from nested structs are applied, so the output round-trips through Unmarshal.

data, _ := oneenv.Marshal(cfg)
os.Stdout.Write(data)
// DB_HOST=localhost
// DB_PORT=5432
// NAME="app one"
// TAGS=a,b

m, _ := oneenv.MarshalMap(cfg)   // map[string]string

Usage — generate --help

Usage[T] writes a table of the variables a struct consumes — key, type, whether it’s required, its default, and the desc tag — ideal for a --help flag.

oneenv.Usage[Config](os.Stdout)
KEY   TYPE           REQUIRED  DEFAULT  DESCRIPTION
PORT  int            no        8080     listen port
HOST  string         yes                bind host

Example — generate .env.example

Example[T] writes a ready-to-fill .env.example for the variables a struct consumes: each key with its default (empty when none), preceded by the desc tag, the type, and whether it is required. Secret defaults are never written.

oneenv.Example[Config](os.Stdout)
# listen port
# type: int
PORT=8080

# bind host
# type: string, required
HOST=

The CLI can also produce one from your existing .env files — keys are kept, values stripped:

oneenv -example            # writes .env.example next to you
oneenv -f .env -example -o -   # print to stdout