Skip to content

Commit 47c26a2

Browse files
authored
Merge pull request #68 from shanesmith/trim-underscore-flag
Add option to trim leading underscore
2 parents 9515f91 + 9a5330b commit 47c26a2

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

cmd/ejson2env/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func main() {
3838
Name: "quiet, q",
3939
Usage: "Suppress export statement",
4040
},
41+
cli.BoolFlag{
42+
Name: "trim-underscore",
43+
Usage: "Trim leading underscore from variable names",
44+
},
4145
}
4246

4347
app.Action = func(c *cli.Context) {
@@ -46,13 +50,18 @@ func main() {
4650

4751
keydir := c.String("keydir")
4852
quiet := c.Bool("quiet")
53+
trim_underscore := c.Bool("trim-underscore")
4954

5055
// select the ExportFunction to use
5156
exportFunc := ejson2env.ExportEnv
5257
if quiet {
5358
exportFunc = ejson2env.ExportQuiet
5459
}
5560

61+
if trim_underscore {
62+
exportFunc = ejson2env.TrimLeadingUnderscoreExportWrapper(exportFunc)
63+
}
64+
5665
if c.Bool("key-from-stdin") {
5766
var err error
5867
userSuppliedPrivateKey, err = readKey(os.Stdin)

exportfunctions.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ejson2env
33
import (
44
"fmt"
55
"io"
6+
"strings"
67

78
"github.com/taskcluster/shell"
89
)
@@ -22,3 +23,15 @@ func ExportQuiet(w io.Writer, values map[string]string) {
2223
fmt.Fprintf(w, "%s=%s\n", key, shell.Escape(value))
2324
}
2425
}
26+
27+
func TrimLeadingUnderscoreExportWrapper(exportfunc ExportFunction) ExportFunction {
28+
return func(w io.Writer, values map[string]string) {
29+
newValues := make(map[string]string, len(values))
30+
31+
for key, value := range values {
32+
newValues[strings.TrimLeft(key, "_")] = value
33+
}
34+
35+
exportfunc(w, newValues)
36+
}
37+
}

secrets_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,37 @@ func TestReadAndExportEnv(t *testing.T) {
3131
tests := []struct {
3232
name string
3333
exportFunc ExportFunction
34+
ejsonFile string
3435
expectedOutput string
3536
}{
3637
{
3738
name: "ExportEnv",
3839
exportFunc: ExportEnv,
40+
ejsonFile: "testdata/test-expected-usage.ejson",
3941
expectedOutput: "export test_key='test value'\n",
4042
},
4143
{
4244
name: "ExportQuiet",
4345
exportFunc: ExportQuiet,
46+
ejsonFile: "testdata/test-expected-usage.ejson",
4447
expectedOutput: "test_key='test value'\n",
4548
},
49+
{
50+
name: "ExportEnvTrimUnderscore",
51+
exportFunc: TrimLeadingUnderscoreExportWrapper(ExportEnv),
52+
ejsonFile: "testdata/test-leading-underscore-env-key.ejson",
53+
expectedOutput: "export test_key='test value'\n",
54+
},
55+
{
56+
name: "ExportEnvNoTrimUnderscore",
57+
exportFunc: ExportEnv,
58+
ejsonFile: "testdata/test-leading-underscore-env-key.ejson",
59+
expectedOutput: "export _test_key='test value'\n",
60+
},
4661
}
4762

4863
for _, test := range tests {
49-
err := ReadAndExportEnv("testdata/test-expected-usage.ejson", "./key", TestKeyValue, test.exportFunc)
64+
err := ReadAndExportEnv(test.ejsonFile, "./key", TestKeyValue, test.exportFunc)
5065
if nil != err {
5166
t.Errorf("testing %s failed: %s", test.name, err)
5267
continue

0 commit comments

Comments
 (0)