Skip to content

Commit 4d8b9be

Browse files
committed
fixup! feat: add config migration
1 parent 5e8d98a commit 4d8b9be

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

config/migrate/migrate.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package migrate
22

33
import (
4+
"encoding/csv"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -111,7 +112,7 @@ func buildEnv(c oldConfig) map[string]string {
111112
}
112113
list := func(key string, value []string) {
113114
if value != nil {
114-
out[key] = strings.Join(value, ",")
115+
out[key] = marshalList(value)
115116
}
116117
}
117118
headers := func(key string, value map[string]string) {
@@ -171,3 +172,14 @@ func marshalMap(m map[string]string) string {
171172
}
172173
return string(data)
173174
}
175+
176+
func marshalList(values []string) string {
177+
var sb strings.Builder
178+
writer := csv.NewWriter(&sb)
179+
writer.UseCRLF = false
180+
if err := writer.Write(values); err != nil {
181+
return ""
182+
}
183+
writer.Flush()
184+
return strings.TrimRight(sb.String(), "\n")
185+
}

config/migrate/migrate_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ GOTIFY_SERVER_PORT=80
142142
GOTIFY_SERVER_SSL_REDIRECTTOHTTPS="true"`, runMigrate(t, yaml))
143143
}
144144

145+
func TestMigrateConfigEscapesListEntries(t *testing.T) {
146+
yaml := `server:
147+
cors:
148+
alloworigins:
149+
- a,b
150+
- 'say "hi"'
151+
- c
152+
`
153+
// The CSV-encoded list contains commas and quotes, which godotenv then
154+
// double-quotes and escapes.
155+
assert.Contains(t, runMigrate(t, yaml),
156+
`GOTIFY_SERVER_CORS_ALLOWORIGINS="\"a,b\",\"say \"\"hi\"\"\",c"`)
157+
}
158+
145159
func TestMigrateConfigErrors(t *testing.T) {
146160
_, err := Config("")
147161
assert.ErrorContains(t, err, "requires one argument", "no path -> usage error")

0 commit comments

Comments
 (0)