Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote environment variables. #150

Merged
merged 2 commits into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/spf13/cobra"
)

const doubleQuoteSpecialChars = "\\\n\r\"!$`"

// exportCmd represents the export command
var (
exportFormat string
Expand Down Expand Up @@ -100,7 +102,7 @@ func exportAsEnvFile(params map[string]string, w io.Writer) error {
for _, k := range sortedKeys(params) {
key := strings.ToUpper(k)
key = strings.Replace(key, "-", "_", -1)
w.Write([]byte(fmt.Sprintf("%s=%s\n", key, params[k])))
w.Write([]byte(fmt.Sprintf(`%s="%s"`+"\n", key, doubleQuoteEscape(params[k]))))
}
return nil
}
Expand Down Expand Up @@ -164,3 +166,17 @@ func sortedKeys(params map[string]string) []string {
sort.Strings(keys)
return keys
}

func doubleQuoteEscape(line string) string {
for _, c := range doubleQuoteSpecialChars {
toReplace := "\\" + string(c)
if c == '\n' {
toReplace = `\n`
}
if c == '\r' {
toReplace = `\r`
}
line = strings.Replace(line, string(c), toReplace, -1)
}
return line
}
41 changes: 41 additions & 0 deletions cmd/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestExportDotenv(t *testing.T) {
tests := []struct {
name string
params map[string]string
output []string
}{
{
"simple",
map[string]string{"foo": "bar"},
[]string{`FOO="bar"`},
},
{
"escaped dollar",
map[string]string{"foo": "bar", "baz": "$qux"},
[]string{`FOO="bar"`, `BAZ="\$qux"`},
},
{
"escaped quote",
map[string]string{"foo": "bar", "baz": `"qux"`},
[]string{`FOO="bar"`, `BAZ="\"qux\""`},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf := &bytes.Buffer{}
err := exportAsEnvFile(test.params, buf)
assert.Nil(t, err)
assert.ElementsMatch(t, test.output, strings.Split(strings.Trim(buf.String(), "\n"), "\n"))
})
}
}