dotenv
.env parser and editor that keeps comments, order and quoting, plus the dotenvctl CLI.
go get github.com/ubgo/dotenv
- Language
- Go
- Go version
- 1.22
- Core deps
- 0 direct
- Latest tag
- v0.1.3
- License
- Apache-2.0
- Go modules
- 2
Code from the README
// Example_reading shows what each read accessor returns for a file with a
// duplicated key and a commented-out setting: duplicates resolve to the last
// occurrence, and disabled lines are invisible to every accessor.
func Example_reading() {
f := dotenv.Parse("A=1\nB=2\nA=3\n# C=off\n")
v, ok := f.Get("A")
fmt.Println(v, ok) // last occurrence wins
fmt.Println(f.Has("C")) // disabled lines are not active keys
fmt.Println(f.Count("A")) // how many active A lines exist
fmt.Println(f.Keys()) // active keys, file order, deduplicated
fmt.Println(len(f.Pairs())) // active pairs, duplicates included
fmt.Println(f.Map()["A"]) // effective view
fmt.Println(len(f.Disabled())) // the commented-out setting is still known
// Output:
// 3 true
// false
// 2
// [A B]
// 3
// 3
// 1
}// Example_editing covers the upsert family: Set updates in place or appends,
// SetAfter places a new key next to an anchor, a missing anchor is an error
// rather than a silent append, and Unset comments a key out reversibly.
func Example_editing() {
f := dotenv.Parse("DB_HOST=localhost\nDB_PORT=5432\nOLD_KEY=x\n")
created := f.Set("API_KEY", "secret") // appends: the key was new
fmt.Println("created:", created)
if _, err := f.SetAfter("DB_PORT", "DB_PASSWORD", "s3cret"); err != nil {
fmt.Println(err)
return
}
_, err := f.SetAfter("NO_SUCH_KEY", "X", "1")
fmt.Println("missing anchor:", errors.Is(err, dotenv.ErrAnchorNotFound))
f.Unset("OLD_KEY", false) // false: comment it out; true would delete the line
fmt.Print(f.Render())
// Output:
// created: true
// missing anchor: true
// DB_HOST=localhost
// DB_PORT=5432
// DB_PASSWORD=s3cret
// # OLD_KEY=x
// API_KEY=secret
}// Example_disabledSettings shows that a commented-out setting is its own kind
// with a readable key and value, that it stays inactive, and that Restore turns
// it back on without inventing any bytes.
func Example_disabledSettings() {
f := dotenv.Parse("# DB_USER=admin\n# just a note\nDB_HOST=db\n")
for _, e := range f.Entries() {
fmt.Printf("%-13s %q\n", e.Kind, e.Key)
}
fmt.Println(f.Has("DB_USER"), f.Disabled()[0].Key)
f.Restore("DB_USER")
fmt.Print(f.Render())
// Output:
// disabled-pair "DB_USER"
// comment ""
// pair "DB_HOST"
// false DB_USER
// DB_USER=admin
// # just a note
// DB_HOST=db
}// Example_inheritedDeclarations reads a Compose-style name-only line. The
// package lists the name but never resolves it; the application layers its
// own source on top (a map stands in for os.LookupEnv here).
func Example_inheritedDeclarations() {
f := dotenv.Parse("DATABASE_URL=postgres://localhost/dev\nAWS_REGION\n")
host := map[string]string{"AWS_REGION": "eu-west-1"} // stands in for the process environment
env := f.Map()
fmt.Println(f.Inherited(), len(env))
for _, name := range f.Inherited() {
if v, ok := host[name]; ok { // the caller's decision, not the parser's
env[name] = v
}
}
fmt.Println(env["AWS_REGION"])
// Output:
// [AWS_REGION] 1
// eu-west-1
}From the dotenv README.
At a glance (from the README)
| Family | Semantics | Use for |
|---|---|---|
Set, SetAfter, SetBefore | upsert — guarantees one active entry; updates in place if the key exists | editing |
Append, InsertAfter, InsertBefore | literal — places exactly what you pass, duplicates included | generating |