1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-12-25 21:41:20 +00:00
go_helpers/env/env.go
Knut Ahlers 82b334b3d3
Fix: Do not panic on weird env list entries
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-03-09 23:25:57 +01:00

30 lines
683 B
Go

package env
import "strings"
// ListToMap converts a list of strings in format KEY=VALUE into a map
func ListToMap(list []string) map[string]string {
out := map[string]string{}
for _, entry := range list {
if len(entry) == 0 || entry[0] == '#' {
continue
}
parts := strings.SplitN(entry, "=", 2)
if len(parts) == 1 {
// No value, set to empty string
parts = append(parts, "")
}
out[parts[0]] = strings.Trim(parts[1], "\"")
}
return out
}
// MapToList converts a map into a list of strings in format KEY=VALUE
func MapToList(envMap map[string]string) []string {
out := []string{}
for k, v := range envMap {
out = append(out, k+"="+v)
}
return out
}