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")
16
15
package godotenv
17
16
18
17
import (
@@ -23,17 +22,17 @@ import (
23
22
"strings"
24
23
)
25
24
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
37
36
func Load (filenames ... string ) (err error ) {
38
37
filenames = filenamesOrDefault (filenames )
39
38
@@ -46,10 +45,8 @@ func Load(filenames ...string) (err error) {
46
45
return
47
46
}
48
47
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
53
50
func Read (filenames ... string ) (envMap map [string ]string , err error ) {
54
51
filenames = filenamesOrDefault (filenames )
55
52
envMap = make (map [string ]string )
@@ -70,15 +67,13 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
70
67
return
71
68
}
72
69
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.
82
77
func Exec (filenames []string , cmd string , cmdArgs []string ) error {
83
78
Load (filenames ... )
84
79
@@ -97,10 +92,10 @@ func filenamesOrDefault(filenames []string) []string {
97
92
}
98
93
}
99
94
100
- func loadFile (filename string ) ( err error ) {
95
+ func loadFile (filename string ) error {
101
96
envMap , err := readFile (filename )
102
97
if err != nil {
103
- return
98
+ return err
104
99
}
105
100
106
101
for key , value := range envMap {
@@ -109,7 +104,7 @@ func loadFile(filename string) (err error) {
109
104
}
110
105
}
111
106
112
- return
107
+ return nil
113
108
}
114
109
115
110
func readFile (filename string ) (envMap map [string ]string , err error ) {
@@ -149,7 +144,7 @@ func parseLine(line string) (key string, value string, err error) {
149
144
if strings .Contains (line , "#" ) {
150
145
segmentsBetweenHashes := strings .Split (line , "#" )
151
146
quotesAreOpen := false
152
- segmentsToKeep := make ( []string , 0 )
147
+ var segmentsToKeep []string
153
148
for _ , segment := range segmentsBetweenHashes {
154
149
if strings .Count (segment , "\" " ) == 1 || strings .Count (segment , "'" ) == 1 {
155
150
if quotesAreOpen {
0 commit comments