4
4
"context"
5
5
"fmt"
6
6
"os"
7
+ "slices"
7
8
"strconv"
8
9
"strings"
9
10
"sync"
@@ -13,40 +14,41 @@ import (
13
14
)
14
15
15
16
var (
17
+ defenv = ".env"
18
+ usrenv = ".env.local"
16
19
once sync.Once
17
20
opclient * onepassword.Client
18
21
)
19
22
23
+ // load global .env files
20
24
func init () {
21
- loadEnvFiles ("" )
25
+ // .env (default)
26
+ godotenv .Load (defenv )
27
+
28
+ // .env.local # local user specific (usually git ignored)
29
+ godotenv .Overload (usrenv )
22
30
}
23
31
24
32
func Environ (dir string , env ... string ) []string {
25
- loadEnvFiles (dir )
26
-
27
- environ := append (os .Environ (), env ... )
28
- for i , e := range environ {
29
- pair := strings .SplitN (e , "=" , 2 )
30
- environ [i ] = pair [0 ] + "=" + op (pair [1 ])
31
- }
33
+ osenv := os .Environ ()
34
+ dotenv := dotenvFiles (dir )
32
35
33
- return environ
36
+ return slices . Concat ( osenv , dotenv , env )
34
37
}
35
38
36
- func loadEnvFiles (dir string ) {
37
- defenv := ".env"
38
- usrenv := ".env.local"
39
+ func dotenvFiles (dir string ) (res []string ) {
40
+ filenames := []string {dir + "/" + defenv , dir + "/" + usrenv }
39
41
40
- if dir != "" {
41
- defenv = dir + "/" + defenv
42
- usrenv = dir + "/" + usrenv
42
+ envMap , err := godotenv . Read ( filenames ... )
43
+ if err != nil {
44
+ return
43
45
}
44
46
45
- // .env (default)
46
- godotenv .Load (defenv )
47
+ for k , v := range envMap {
48
+ res = append (res , k + "=" + v )
49
+ }
47
50
48
- // .env.local # local user specific (usually git ignored)
49
- godotenv .Overload (usrenv )
51
+ return
50
52
}
51
53
52
54
func op (ref string ) string {
0 commit comments