Skip to content

Commit

Permalink
Merge pull request #109 from mniak/pr
Browse files Browse the repository at this point in the history
Write ints without quotes
  • Loading branch information
joho committed Nov 8, 2020
2 parents a4d9cf1 + 6e653f9 commit 3e4069b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion godotenv.go
Expand Up @@ -22,6 +22,7 @@ import (
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
)

Expand Down Expand Up @@ -169,7 +170,11 @@ func Write(envMap map[string]string, filename string) error {
func Marshal(envMap map[string]string) (string, error) {
lines := make([]string, 0, len(envMap))
for k, v := range envMap {
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v)))
if d, err := strconv.Atoi(v); err == nil {
lines = append(lines, fmt.Sprintf(`%s=%d`, k, d))
} else {
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v)))
}
}
sort.Strings(lines)
return strings.Join(lines, "\n"), nil
Expand Down
2 changes: 2 additions & 0 deletions godotenv_test.go
Expand Up @@ -445,6 +445,8 @@ func TestWrite(t *testing.T) {
writeAndCompare(`foo="\n\r\\r!"`, `foo="\n\r\\r\!"`)
// lines should be sorted
writeAndCompare("foo=bar\nbaz=buzz", "baz=\"buzz\"\nfoo=\"bar\"")
// integers should not be quoted
writeAndCompare(`key="10"`, `key=10`)

}

Expand Down

0 comments on commit 3e4069b

Please sign in to comment.