Skip to content

Commit 9a061eb

Browse files
committed
fix environ order and overload
1 parent 8196a11 commit 9a061eb

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

Lunafile

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ http.get(u)
1010
# common
1111

1212
# define default for an env var, provided env var will override
13-
env("OP_ENABLED", False)
13+
env("OP_ENABLED", True)
1414

1515
# ldflags('-X github.com/moonwalker/platform/internal/build.version=$VERSION')
1616
# docker_repo('europe-docker.pkg.dev/mw-lunar/platform')
@@ -30,7 +30,7 @@ task("docs2", cmds=['echo "gello"', 'echo "bboo"'])
3030

3131

3232
def docs():
33-
sh("echo $VERSION @ $FOO, $OP_TEST", env=["FOO=BAR"])
33+
sh("echo $VERSION @ $FOO, $OP_ENABLED", env=["FOO=BAR3"])
3434

3535

3636
def stacks(stack, command, *args):

internal/support/env.go

+21-19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"slices"
78
"strconv"
89
"strings"
910
"sync"
@@ -13,40 +14,41 @@ import (
1314
)
1415

1516
var (
17+
defenv = ".env"
18+
usrenv = ".env.local"
1619
once sync.Once
1720
opclient *onepassword.Client
1821
)
1922

23+
// load global .env files
2024
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)
2230
}
2331

2432
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)
3235

33-
return environ
36+
return slices.Concat(osenv, dotenv, env)
3437
}
3538

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}
3941

40-
if dir != "" {
41-
defenv = dir + "/" + defenv
42-
usrenv = dir + "/" + usrenv
42+
envMap, err := godotenv.Read(filenames...)
43+
if err != nil {
44+
return
4345
}
4446

45-
// .env (default)
46-
godotenv.Load(defenv)
47+
for k, v := range envMap {
48+
res = append(res, k+"="+v)
49+
}
4750

48-
// .env.local # local user specific (usually git ignored)
49-
godotenv.Overload(usrenv)
51+
return
5052
}
5153

5254
func op(ref string) string {

0 commit comments

Comments
 (0)