You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a -print flag which accepts no additional arguments and prints any
found SSM env vars to stdout instead of exec-ing a process with the env
vars set.
```sh
-print
Print the decrypted env vars without exporting them and exit
```
The use-case for this is in places like CI jobs where you may want to
resolve SSM parameters and then write them to a config file, or persist
them elsewhere for subsequent use.
ssm-env is already a bit architecturally overloaded, and this strains it
further. I'm not inclined to do a major refactor/rewrite at this point,
but if we want to continue extending it that may be required at some
point. I'd prob start by separating the interfaces for outputs and
fallibility to avoid overloading the expandEnviron/setEnviron functions
the way they currently are.
Copy file name to clipboardExpand all lines: main.go
+84-15Lines changed: 84 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -50,6 +50,7 @@ func main() {
50
50
template=flag.String("template", DefaultTemplate, "The template used to determine what the SSM parameter name is for an environment variable. When this template returns an empty string, the env variable is not an SSM parameter")
51
51
decrypt=flag.Bool("with-decryption", false, "Will attempt to decrypt the parameter, and set the env var as plaintext")
52
52
nofail=flag.Bool("no-fail", false, "Don't fail if error retrieving parameter")
53
+
print=flag.Bool("print", false, "Print the decrypted env vars without exporting them and exit")
53
54
print_version=flag.Bool("V", false, "Print the version and exit")
54
55
)
55
56
flag.Parse()
@@ -61,26 +62,51 @@ func main() {
61
62
return
62
63
}
63
64
64
-
iflen(args) <=0 {
65
+
if!*print&&len(args) <=0 {
65
66
flag.Usage()
66
-
os.Exit(1)
67
+
fmt.Fprintf(os.Stderr, "\nmissing program to execute\n")
68
+
os.Exit(2)
67
69
}
68
70
69
-
path, err:=exec.LookPath(args[0])
70
-
must(err)
71
+
if*print&&len(args) >0 {
72
+
flag.Usage()
73
+
fmt.Fprintf(os.Stderr, "\n-print is incompatible with arguments\n")
74
+
os.Exit(3)
75
+
}
71
76
72
-
varososEnviron
77
+
varosEnvosEnviron
73
78
79
+
// Construct the template we'll use for extracting the ssm params we need to
80
+
// fetch.
74
81
t, err:=parseTemplate(*template)
75
82
must(err)
83
+
84
+
// Construct an expander with the configs for fetching/replacing env vars.
76
85
e:=&expander{
77
86
batchSize: defaultBatchSize,
78
87
t: t,
79
88
ssm: &lazySSMClient{},
80
-
os: os,
89
+
os: osEnv,
81
90
}
82
-
must(e.expandEnviron(*decrypt, *nofail))
83
-
must(syscall.Exec(path, args[0:], os.Environ()))
91
+
// Attempt to "expand" ssm vars.
92
+
vars, err:=e.expandEnviron(*decrypt, *nofail)
93
+
must(err)
94
+
95
+
// Actually set the env vars for the process.
96
+
e.setEnviron(*print, vars)
97
+
// If -print was passed, we're done.
98
+
if*print {
99
+
os.Exit(0)
100
+
}
101
+
102
+
// Make sure that we're invoking ssm-env with an executable that actually
103
+
// exists.
104
+
path, err:=exec.LookPath(args[0])
105
+
must(err)
106
+
107
+
// Exec whatever command was passed, using the current process' env vars
0 commit comments