1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-12-25 13:31:21 +00:00

Fix: Do not panic on weird env list entries

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-03-09 23:25:57 +01:00
parent 76c41f9c37
commit 82b334b3d3
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 6 additions and 2 deletions

4
env/env.go vendored
View file

@ -11,6 +11,10 @@ func ListToMap(list []string) map[string]string {
} }
parts := strings.SplitN(entry, "=", 2) 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], "\"") out[parts[0]] = strings.Trim(parts[1], "\"")
} }
return out return out

4
env/env_test.go vendored
View file

@ -10,19 +10,20 @@ import (
) )
var _ = Describe("Env", func() { var _ = Describe("Env", func() {
Context("ListToMap", func() { Context("ListToMap", func() {
var ( var (
list = []string{ list = []string{
"FIRST_KEY=firstvalue", "FIRST_KEY=firstvalue",
"SECOND_KEY=secondvalue", "SECOND_KEY=secondvalue",
"WEIRD=", "WEIRD=",
"NOVALUE",
"", "",
} }
emap = map[string]string{ emap = map[string]string{
"FIRST_KEY": "firstvalue", "FIRST_KEY": "firstvalue",
"SECOND_KEY": "secondvalue", "SECOND_KEY": "secondvalue",
"WEIRD": "", "WEIRD": "",
"NOVALUE": "",
} }
) )
@ -51,5 +52,4 @@ var _ = Describe("Env", func() {
Expect(l).To(Equal(list)) Expect(l).To(Equal(list))
}) })
}) })
}) })