Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit 19b5c2b

Browse files
committed
1 parent e1c9261 commit 19b5c2b

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

godotenv.go

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
/*
2-
A go port of the ruby dotenv library (https://github.com/bkeepers/dotenv)
3-
4-
Examples/readme can be found on the github page at https://github.com/joho/godotenv
5-
6-
The TL;DR is that you make a .env file that looks something like
7-
8-
SOME_ENV_VAR=somevalue
9-
10-
and then in your go code you can call
11-
12-
godotenv.Load()
13-
14-
and all the env vars declared in .env will be avaiable through os.Getenv("SOME_ENV_VAR")
15-
*/
1+
// Package godotenv
2+
// A go port of the ruby dotenv library (https://github.com/bkeepers/dotenv)
3+
//
4+
// Examples/readme can be found on the github page at https://github.com/joho/godotenv
5+
//
6+
// The TL;DR is that you make a .env file that looks something like
7+
//
8+
// SOME_ENV_VAR=somevalue
9+
//
10+
// and then in your go code you can call
11+
//
12+
// godotenv.Load()
13+
//
14+
// and all the env vars declared in .env will be avaiable through os.Getenv("SOME_ENV_VAR")
1615
package godotenv
1716

1817
import (
@@ -23,17 +22,17 @@ import (
2322
"strings"
2423
)
2524

26-
/*
27-
Call this function as close as possible to the start of your program (ideally in main)
28-
29-
If you call Load without any args it will default to loading .env in the current path
30-
31-
You can otherwise tell it which files to load (there can be more than one) like
32-
33-
godotenv.Load("fileone", "filetwo")
34-
35-
It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults
36-
*/
25+
// Load will read your env file(s) and load them into ENV for this process.
26+
//
27+
// Call this function as close as possible to the start of your program (ideally in main)
28+
//
29+
// If you call Load without any args it will default to loading .env in the current path
30+
//
31+
// You can otherwise tell it which files to load (there can be more than one) like
32+
//
33+
// godotenv.Load("fileone", "filetwo")
34+
//
35+
// It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults
3736
func Load(filenames ...string) (err error) {
3837
filenames = filenamesOrDefault(filenames)
3938

@@ -46,10 +45,8 @@ func Load(filenames ...string) (err error) {
4645
return
4746
}
4847

49-
/*
50-
Read all env (with same file loading semantics as Load) but return values as
51-
a map rather than automatically writing values into env
52-
*/
48+
// Read all env (with same file loading semantics as Load) but return values as
49+
// a map rather than automatically writing values into env
5350
func Read(filenames ...string) (envMap map[string]string, err error) {
5451
filenames = filenamesOrDefault(filenames)
5552
envMap = make(map[string]string)
@@ -70,15 +67,13 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
7067
return
7168
}
7269

73-
/*
74-
Loads env vars from the specified filenames (empty map falls back to default)
75-
then executes the cmd specified.
76-
77-
Simply hooks up os.Stdin/err/out to the command and calls Run()
78-
79-
If you want more fine grained control over your command it's recommended
80-
that you use `Load()` or `Read()` and the `os/exec` package yourself.
81-
*/
70+
// Exec loads env vars from the specified filenames (empty map falls back to default)
71+
// then executes the cmd specified.
72+
//
73+
// Simply hooks up os.Stdin/err/out to the command and calls Run()
74+
//
75+
// If you want more fine grained control over your command it's recommended
76+
// that you use `Load()` or `Read()` and the `os/exec` package yourself.
8277
func Exec(filenames []string, cmd string, cmdArgs []string) error {
8378
Load(filenames...)
8479

@@ -97,10 +92,10 @@ func filenamesOrDefault(filenames []string) []string {
9792
}
9893
}
9994

100-
func loadFile(filename string) (err error) {
95+
func loadFile(filename string) error {
10196
envMap, err := readFile(filename)
10297
if err != nil {
103-
return
98+
return err
10499
}
105100

106101
for key, value := range envMap {
@@ -109,7 +104,7 @@ func loadFile(filename string) (err error) {
109104
}
110105
}
111106

112-
return
107+
return nil
113108
}
114109

115110
func readFile(filename string) (envMap map[string]string, err error) {
@@ -149,7 +144,7 @@ func parseLine(line string) (key string, value string, err error) {
149144
if strings.Contains(line, "#") {
150145
segmentsBetweenHashes := strings.Split(line, "#")
151146
quotesAreOpen := false
152-
segmentsToKeep := make([]string, 0)
147+
var segmentsToKeep []string
153148
for _, segment := range segmentsBetweenHashes {
154149
if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 {
155150
if quotesAreOpen {

0 commit comments

Comments
 (0)