@@ -3,17 +3,17 @@ package envsec
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
- "os"
7
6
"os/exec"
7
+ "path/filepath"
8
8
9
9
"github.com/pkg/errors"
10
10
"go.jetpack.io/devbox/internal/boxcli/usererr"
11
- "go.jetpack.io/devbox/internal/cmdutil"
12
11
"go.jetpack.io/devbox/internal/debug"
13
12
"go.jetpack.io/pkg/sandbox/runx"
14
13
)
15
14
16
15
var envCache map [string ]string
16
+ var binPathCache string
17
17
18
18
func Env (projectDir string ) (map [string ]string , error ) {
19
19
@@ -23,11 +23,7 @@ func Env(projectDir string) (map[string]string, error) {
23
23
return envCache , nil
24
24
}
25
25
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 {
31
27
return nil , err
32
28
}
33
29
@@ -37,25 +33,35 @@ func Env(projectDir string) (map[string]string, error) {
37
33
return envCache , err
38
34
}
39
35
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
+
42
46
paths , err := runx .Install ("jetpack-io/envsec" )
43
47
if err != nil {
44
- return errors .Wrap (err , "failed to install envsec" )
48
+ return "" , errors .Wrap (err , "failed to install envsec" )
45
49
}
46
50
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" )
49
53
}
50
54
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
55
57
}
56
58
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" )
59
65
cmd .Dir = projectDir
60
66
var bufErr bytes.Buffer
61
67
cmd .Stderr = & bufErr
@@ -67,8 +73,12 @@ func ensureEnvsecInitialized(projectDir string) error {
67
73
}
68
74
69
75
func envsecList (projectDir string ) (map [string ]string , error ) {
76
+ binPath , err := EnsureInstalled ()
77
+ if err != nil {
78
+ return nil , err
79
+ }
70
80
cmd := exec .Command (
71
- "envsec" , "ls" , "--show" ,
81
+ binPath , "ls" , "--show" ,
72
82
"--format" , "json" ,
73
83
"--environment" , "dev" ,
74
84
"--json-errors" )
0 commit comments