Skip to content

Commit bc9de75

Browse files
authored
[envsec] Add envsec to path if used in config (#1505)
1 parent 266ccff commit bc9de75

File tree

5 files changed

+60
-34
lines changed

5 files changed

+60
-34
lines changed

internal/boxcli/featureflag/envsec.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

internal/devconfig/env.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package devconfig
22

33
import (
4-
"go.jetpack.io/devbox/internal/boxcli/featureflag"
54
"go.jetpack.io/devbox/internal/boxcli/usererr"
65
"go.jetpack.io/devbox/internal/integrations/envsec"
76
)
87

98
func (c *Config) ComputedEnv(projectDir string) (map[string]string, error) {
109
env := map[string]string{}
1110
var err error
12-
if featureflag.Envsec.Enabled() {
13-
if c.EnvFrom == "envsec" {
14-
env, err = envsec.Env(projectDir)
15-
if err != nil {
16-
return nil, err
17-
}
18-
} else if c.EnvFrom != "" {
19-
return nil, usererr.New("unknown from_env value: %s", c.EnvFrom)
11+
if c.IsEnvsecEnabled() {
12+
env, err = envsec.Env(projectDir)
13+
if err != nil {
14+
return nil, err
2015
}
16+
} else if c.EnvFrom != "" {
17+
return nil, usererr.New("unknown from_env value: %s", c.EnvFrom)
2118
}
2219
for k, v := range c.Env {
2320
env[k] = v
2421
}
2522
return env, nil
2623
}
24+
25+
func (c *Config) IsEnvsecEnabled() bool {
26+
return c.EnvFrom == "envsec"
27+
}

internal/impl/devbox.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,11 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
871871
)
872872
}
873873

874+
env["PATH"], err = d.addUtilitiesToPath(env["PATH"])
875+
if err != nil {
876+
return nil, err
877+
}
878+
874879
// Include env variables in devbox.json
875880
configEnv, err := d.configEnvs(env)
876881
if err != nil {

internal/impl/util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111

1212
"github.com/pkg/errors"
13+
"go.jetpack.io/devbox/internal/integrations/envsec"
1314
"go.jetpack.io/devbox/internal/nix/nixprofile"
1415

1516
"go.jetpack.io/devbox/internal/xdg"
@@ -33,6 +34,21 @@ func (d *Devbox) addDevboxUtilityPackage(ctx context.Context, pkg string) error
3334
})
3435
}
3536

37+
// addDevboxUtilityPackages adds binaries that we want the user to have access
38+
// to (e.g. envsec).
39+
// Question: Should we add utilityBinPath here? That would allow user to use
40+
// process-compose, etc
41+
func (d *Devbox) addUtilitiesToPath(path string) (string, error) {
42+
if d.cfg.IsEnvsecEnabled() {
43+
envsecPath, err := envsec.EnsureInstalled()
44+
if err != nil {
45+
return "", err
46+
}
47+
path = path + string(os.PathListSeparator) + filepath.Dir(envsecPath)
48+
}
49+
return path, nil
50+
}
51+
3652
func utilityLookPath(binName string) (string, error) {
3753
binPath, err := utilityBinPath()
3854
if err != nil {

internal/integrations/envsec/envsec.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package envsec
33
import (
44
"bytes"
55
"encoding/json"
6-
"os"
76
"os/exec"
7+
"path/filepath"
88

99
"github.com/pkg/errors"
1010
"go.jetpack.io/devbox/internal/boxcli/usererr"
11-
"go.jetpack.io/devbox/internal/cmdutil"
1211
"go.jetpack.io/devbox/internal/debug"
1312
"go.jetpack.io/pkg/sandbox/runx"
1413
)
1514

1615
var envCache map[string]string
16+
var binPathCache string
1717

1818
func Env(projectDir string) (map[string]string, error) {
1919

@@ -23,11 +23,7 @@ func Env(projectDir string) (map[string]string, error) {
2323
return envCache, nil
2424
}
2525

26-
if err := ensureEnvsecInstalled(); err != nil {
27-
return nil, err
28-
}
29-
30-
if err := ensureEnvsecInitialized(projectDir); err != nil {
26+
if err := ensureInitialized(projectDir); err != nil {
3127
return nil, err
3228
}
3329

@@ -37,25 +33,35 @@ func Env(projectDir string) (map[string]string, error) {
3733
return envCache, err
3834
}
3935

40-
func ensureEnvsecInstalled() error {
41-
// In newer runx version this will return the paths
36+
func EnsureInstalled() (string, error) {
37+
if binPathCache != "" {
38+
return binPathCache, nil
39+
}
40+
41+
if path, err := exec.LookPath("envsec"); err == nil {
42+
binPathCache = path
43+
return binPathCache, nil
44+
}
45+
4246
paths, err := runx.Install("jetpack-io/envsec")
4347
if err != nil {
44-
return errors.Wrap(err, "failed to install envsec")
48+
return "", errors.Wrap(err, "failed to install envsec")
4549
}
4650

47-
for _, path := range paths {
48-
os.Setenv("PATH", path+string(os.PathListSeparator)+os.Getenv("PATH"))
51+
if len(paths) == 0 {
52+
return "", usererr.New("envsec is not installed or not in path")
4953
}
5054

51-
if !cmdutil.Exists("envsec") {
52-
return usererr.New("envsec is not installed or not in path")
53-
}
54-
return nil
55+
binPathCache = filepath.Join(paths[0], "envsec")
56+
return binPathCache, nil
5557
}
5658

57-
func ensureEnvsecInitialized(projectDir string) error {
58-
cmd := exec.Command("envsec", "init", "--json-errors")
59+
func ensureInitialized(projectDir string) error {
60+
binPath, err := EnsureInstalled()
61+
if err != nil {
62+
return err
63+
}
64+
cmd := exec.Command(binPath, "init", "--json-errors")
5965
cmd.Dir = projectDir
6066
var bufErr bytes.Buffer
6167
cmd.Stderr = &bufErr
@@ -67,8 +73,12 @@ func ensureEnvsecInitialized(projectDir string) error {
6773
}
6874

6975
func envsecList(projectDir string) (map[string]string, error) {
76+
binPath, err := EnsureInstalled()
77+
if err != nil {
78+
return nil, err
79+
}
7080
cmd := exec.Command(
71-
"envsec", "ls", "--show",
81+
binPath, "ls", "--show",
7282
"--format", "json",
7383
"--environment", "dev",
7484
"--json-errors")

0 commit comments

Comments
 (0)