Skip to content

Commit 4c42b36

Browse files
committed
fixup! feat!: rework config parsing
1 parent fd1d93f commit 4c42b36

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

config/config_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,28 @@ func TestNotAddSlash(t *testing.T) {
8989
assert.Equal(t, "../data/", conf.UploadedImagesDir)
9090
os.Unsetenv("GOTIFY_UPLOADEDIMAGESDIR")
9191
}
92+
93+
func TestParseList(t *testing.T) {
94+
const env = "GOTIFY_TEST_PARSELIST"
95+
96+
tests := []struct {
97+
name string
98+
raw string
99+
want []string
100+
}{
101+
{name: "escaped quotes", raw: `"a,b","c""d",e`, want: []string{`a,b`, `c"d`, `e`}},
102+
{name: "lazy bare quote", raw: `a"b,c`, want: []string{`a"b`, `c`}},
103+
{name: "lazy quote in quoted field", raw: `"ab"cd",test`, want: []string{`ab"cd`, `test`}},
104+
}
105+
106+
for _, tc := range tests {
107+
t.Run(tc.name, func(t *testing.T) {
108+
os.Setenv(env, tc.raw)
109+
defer os.Unsetenv(env)
110+
111+
var got []string
112+
assert.Nil(t, parseList(&got, env))
113+
assert.Equal(t, tc.want, got)
114+
})
115+
}
116+
}

config/parse.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"encoding/csv"
45
"encoding/json"
56
"fmt"
67
"os"
@@ -74,11 +75,14 @@ func parseList(target *[]string, env string) error {
7475
if !ok || raw == "" {
7576
return nil
7677
}
77-
var out []string
78-
for part := range strings.SplitSeq(raw, ",") {
79-
out = append(out, strings.TrimSpace(part))
78+
reader := csv.NewReader(strings.NewReader(raw))
79+
reader.TrimLeadingSpace = true
80+
reader.LazyQuotes = true
81+
record, err := reader.Read()
82+
if err != nil {
83+
return fmt.Errorf("invalid CSV for %s (%q): %w", env, raw, err)
8084
}
81-
*target = out
85+
*target = record
8286
return nil
8387
}
8488

gotify-server.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
# text a plain string value.
1818
# number an integer value.
1919
# boolean `true` or `false`.
20-
# text-list comma-separated list of strings; whitespace around each entry is trimmed.
20+
# text-list comma-separated list of strings, parsed as a single CSV line.
21+
# A comma can be escaped by wrapping the value in quotes.
2122
# Example: a,b,c
23+
# Example: "a,b",c -> entries: `a,b` and `c`
2224
# json-map a JSON object mapping string keys to string values.
2325
# Example: {"X-Foo":"bar","X-Baz":"qux"}
2426
#

0 commit comments

Comments
 (0)