Skip to content

Commit

Permalink
Merge pull request #68 from shanesmith/trim-underscore-flag
Browse files Browse the repository at this point in the history
Add option to trim leading underscore
  • Loading branch information
burke authored Oct 7, 2024
2 parents 9515f91 + 9a5330b commit 47c26a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
9 changes: 9 additions & 0 deletions cmd/ejson2env/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func main() {
Name: "quiet, q",
Usage: "Suppress export statement",
},
cli.BoolFlag{
Name: "trim-underscore",
Usage: "Trim leading underscore from variable names",
},
}

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

keydir := c.String("keydir")
quiet := c.Bool("quiet")
trim_underscore := c.Bool("trim-underscore")

// select the ExportFunction to use
exportFunc := ejson2env.ExportEnv
if quiet {
exportFunc = ejson2env.ExportQuiet
}

if trim_underscore {
exportFunc = ejson2env.TrimLeadingUnderscoreExportWrapper(exportFunc)
}

if c.Bool("key-from-stdin") {
var err error
userSuppliedPrivateKey, err = readKey(os.Stdin)
Expand Down
13 changes: 13 additions & 0 deletions exportfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ejson2env
import (
"fmt"
"io"
"strings"

"github.com/taskcluster/shell"
)
Expand All @@ -22,3 +23,15 @@ func ExportQuiet(w io.Writer, values map[string]string) {
fmt.Fprintf(w, "%s=%s\n", key, shell.Escape(value))
}
}

func TrimLeadingUnderscoreExportWrapper(exportfunc ExportFunction) ExportFunction {
return func(w io.Writer, values map[string]string) {
newValues := make(map[string]string, len(values))

for key, value := range values {
newValues[strings.TrimLeft(key, "_")] = value
}

exportfunc(w, newValues)
}
}
17 changes: 16 additions & 1 deletion secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,37 @@ func TestReadAndExportEnv(t *testing.T) {
tests := []struct {
name string
exportFunc ExportFunction
ejsonFile string
expectedOutput string
}{
{
name: "ExportEnv",
exportFunc: ExportEnv,
ejsonFile: "testdata/test-expected-usage.ejson",
expectedOutput: "export test_key='test value'\n",
},
{
name: "ExportQuiet",
exportFunc: ExportQuiet,
ejsonFile: "testdata/test-expected-usage.ejson",
expectedOutput: "test_key='test value'\n",
},
{
name: "ExportEnvTrimUnderscore",
exportFunc: TrimLeadingUnderscoreExportWrapper(ExportEnv),
ejsonFile: "testdata/test-leading-underscore-env-key.ejson",
expectedOutput: "export test_key='test value'\n",
},
{
name: "ExportEnvNoTrimUnderscore",
exportFunc: ExportEnv,
ejsonFile: "testdata/test-leading-underscore-env-key.ejson",
expectedOutput: "export _test_key='test value'\n",
},
}

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

0 comments on commit 47c26a2

Please sign in to comment.