From e62c80d6bc93678d172e9bb04628f7af8dc7ced4 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Mon, 18 Dec 2023 15:48:20 +0100 Subject: [PATCH] Add option to skip shell escaping --- cmd/ejson2env/main.go | 8 ++++++++ exportfunctions.go | 8 ++++++++ secrets_test.go | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/cmd/ejson2env/main.go b/cmd/ejson2env/main.go index 1f0b540..95eec62 100644 --- a/cmd/ejson2env/main.go +++ b/cmd/ejson2env/main.go @@ -38,6 +38,10 @@ func main() { Name: "quiet, q", Usage: "Suppress export statement", }, + cli.BoolFlag{ + Name: "raw, r", + Usage: "Skip shell-escaping values", + }, } app.Action = func(c *cli.Context) { @@ -46,12 +50,16 @@ func main() { keydir := c.String("keydir") quiet := c.Bool("quiet") + raw := c.Bool("raw") // select the ExportFunction to use exportFunc := ejson2env.ExportEnv if quiet { exportFunc = ejson2env.ExportQuiet } + if raw { + exportFunc = ejson2env.ExportRaw + } if c.Bool("key-from-stdin") { var err error diff --git a/exportfunctions.go b/exportfunctions.go index 4112589..4051720 100644 --- a/exportfunctions.go +++ b/exportfunctions.go @@ -22,3 +22,11 @@ func ExportQuiet(w io.Writer, values map[string]string) { fmt.Fprintf(w, "%s=%s\n", key, shell.Escape(value)) } } + +// ExportRaw writes the passed environment values to the passed +// io.Writer in %s=%s format without shell escaping. +func ExportRaw(w io.Writer, values map[string]string) { + for key, value := range values { + fmt.Fprintf(w, "%s=%s\n", key, value) + } +} diff --git a/secrets_test.go b/secrets_test.go index 8ad925b..d3804d7 100644 --- a/secrets_test.go +++ b/secrets_test.go @@ -43,6 +43,11 @@ func TestReadAndExportEnv(t *testing.T) { exportFunc: ExportQuiet, expectedOutput: "test_key='test value'\n", }, + { + name: "ExportRaw", + exportFunc: ExportRaw, + expectedOutput: "test_key=test value\n", + }, } for _, test := range tests {