Skip to content

Commit 3e4069b

Browse files
authored
Merge pull request #109 from mniak/pr
Write ints without quotes
2 parents a4d9cf1 + 6e653f9 commit 3e4069b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

godotenv.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os/exec"
2323
"regexp"
2424
"sort"
25+
"strconv"
2526
"strings"
2627
)
2728

@@ -169,7 +170,11 @@ func Write(envMap map[string]string, filename string) error {
169170
func Marshal(envMap map[string]string) (string, error) {
170171
lines := make([]string, 0, len(envMap))
171172
for k, v := range envMap {
172-
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v)))
173+
if d, err := strconv.Atoi(v); err == nil {
174+
lines = append(lines, fmt.Sprintf(`%s=%d`, k, d))
175+
} else {
176+
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v)))
177+
}
173178
}
174179
sort.Strings(lines)
175180
return strings.Join(lines, "\n"), nil

godotenv_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ func TestWrite(t *testing.T) {
445445
writeAndCompare(`foo="\n\r\\r!"`, `foo="\n\r\\r\!"`)
446446
// lines should be sorted
447447
writeAndCompare("foo=bar\nbaz=buzz", "baz=\"buzz\"\nfoo=\"bar\"")
448+
// integers should not be quoted
449+
writeAndCompare(`key="10"`, `key=10`)
448450

449451
}
450452

0 commit comments

Comments
 (0)