Skip to content

Commit

Permalink
wrap godotenv for load/overload for now; include documentation on usage
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaqx committed Jun 12, 2024
1 parent f8d72b3 commit 00e68f7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ import (
)

func main() {
// TODO
if err := env.Load(); err != nil {
fmt.Printf("failed to load environment variables %v\n", err)
}

port := env.GetWithFallback("PORT", "8080")
fmt.Printf("Port: %s\n", port)
}
```

### Roadmap

- [ ] `.env` loading
- [ ] Load environment variables into structs with tags
- [ ] Type casting of environment variable values
40 changes: 40 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package env

import (
"os"

"github.com/joho/godotenv"
)

// Set sets an environment variable.
Expand All @@ -28,3 +30,41 @@ func GetWithFallback(key string, fallback string) string {
}
return fallback
}

// Load will read your env file(s) and load them into ENV for this process.
//
// Call this function as close as possible to the start of your program
// (ideally in main).
//
// If you call Load without any args it will default to loading .env in the
// current path.
//
// You can otherwise tell it which files to load (there can be more than one)
// like:
//
// env.Load("fileone", "filetwo")
//
// > [!IMPORTANT] This __WILL NOT__ override an env variable that already
// > exists. Consider the .env file to set dev vars or sensible defaults.
func Load(filenames ...string) (err error) {
return godotenv.Load(filenames...)
}

// Overload will read your env file(s) and load them into ENV for this process.
//
// Call this function as close as possible to the start of your program
// (ideally in main).
//
// If you call Load without any args it will default to loading .env in the
// current path.
//
// You can otherwise tell it which files to load (there can be more than one)
// like:
//
// env.Overload("fileone", "filetwo")
//
// > [!IMPORTANT] This __WILL__ override an env variable that already
// > exists. Consider the .env file to forcefully set all vars.
func Overload(filenames ...string) (err error) {
return godotenv.Overload(filenames...)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/syntaqx/env

go 1.22.3

require github.com/joho/godotenv v1.5.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

0 comments on commit 00e68f7

Please sign in to comment.