Skip to content

Commit

Permalink
up: dotenv - update some get value method logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 15, 2022
1 parent d22fc9c commit 052e9b2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
22 changes: 22 additions & 0 deletions dotenv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,36 @@ Package `dotenv` that supports importing data from files (eg `.env`) to ENV

## Usage

### Load Env

```go
err := dotenv.Load("./", ".env")
// Or use
// err := dotenv.LoadExists("./", ".env")
```

Load from string-map:

```go
err := dotenv.LoadFromMap(map[string]string{
"ENV_KEY": "value",
"LOG_LEVEL": "info",
})
```

### Read Env

```go
val := dotenv.Get("ENV_KEY")
// Or use
// val := os.Getenv("ENV_KEY")

// get int value
intVal := dotenv.Int("LOG_LEVEL")

// get bool value
blVal := dotenv.Bool("OPEN_DEBUG")

// with default value
val := dotenv.Get("ENV_KEY", "default value")
```
32 changes: 19 additions & 13 deletions dotenv/dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,8 @@ func LoadFromMap(kv map[string]string) (err error) {

// Get get os ENV value by name
func Get(name string, defVal ...string) (val string) {
if UpperEnvKey {
name = strings.ToUpper(name)
}
if val = loadedData[name]; val != "" {
return
}

// NOTICE: if is windows OS, os.Getenv() Key is not case-sensitive
if val = os.Getenv(name); val != "" {
return
if val, ok := getVal(name); ok {
return val
}

if len(defVal) > 0 {
Expand All @@ -142,7 +134,7 @@ func Get(name string, defVal ...string) (val string) {

// Bool get a bool value by key
func Bool(name string, defVal ...bool) (val bool) {
if str := Get(name); str != "" {
if str, ok := getVal(name); ok {
val, err := strconv.ParseBool(str)
if err == nil {
return val
Expand All @@ -157,7 +149,7 @@ func Bool(name string, defVal ...bool) (val bool) {

// Int get a int value by key
func Int(name string, defVal ...int) (val int) {
if str := Get(name); str != "" {
if str, ok := getVal(name); ok {
val, err := strconv.ParseInt(str, 10, 0)
if err == nil {
return int(val)
Expand All @@ -170,6 +162,20 @@ func Int(name string, defVal ...int) (val int) {
return
}

func getVal(name string) (val string, ok bool) {
if UpperEnvKey {
name = strings.ToUpper(name)
}

// cached
if val = loadedData[name]; val != "" {
return
}

// NOTICE: if is windows OS, os.Getenv() Key is not case-sensitive
return os.LookupEnv(name)
}

// load and parse .env file data to os ENV
func loadFile(file string) (err error) {
fd, err := os.Open(file)
Expand All @@ -184,7 +190,7 @@ func loadFile(file string) (err error) {
defer fd.Close()

// parse file contents
p := parser.NewSimpled(parser.NoDefSection)
p := parser.NewLite()
if _, err = p.ParseFrom(bufio.NewScanner(fd)); err != nil {
return
}
Expand Down

0 comments on commit 052e9b2

Please sign in to comment.