diff --git a/godotenv.go b/godotenv.go index 69e816c..8e311f4 100644 --- a/godotenv.go +++ b/godotenv.go @@ -22,6 +22,7 @@ import ( "os/exec" "regexp" "sort" + "strconv" "strings" ) @@ -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 diff --git a/godotenv_test.go b/godotenv_test.go index d1f73cb..7274c14 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -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`) }