rano/config/dotenv/write.go

63 lines
1.4 KiB
Go
Raw Normal View History

package dotenv
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
)
//
2024-11-18 15:23:13 +00:00
// ported from https://github.com/joho/godotenv/blob/main/godotenv.go
//
const doubleQuoteSpecialChars = "\\\n\r\"!$`"
// Write serializes the given environment and writes it to a file.
func Write(envMap map[string]string, filename string) error {
content, err := marshal(envMap)
if err != nil {
return err
}
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(content + "\n")
if err != nil {
return err
}
return file.Sync()
}
// marshal outputs the given environment as a dotenv-formatted environment file.
// Each line is in the format: KEY="VALUE" where VALUE is backslash-escaped.
func marshal(envMap map[string]string) (string, error) {
lines := make([]string, 0, len(envMap))
for k, v := range envMap {
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
}
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
}