Skip to content

Commit d88b179

Browse files
committed
fix: error when legacy config exists
1 parent 9f7dc63 commit d88b179

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

config/config_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/gotify/server/v2/mode"
9+
"github.com/rs/zerolog"
910
"github.com/stretchr/testify/assert"
1011
)
1112

@@ -74,6 +75,26 @@ func TestGotifyConfigFile(t *testing.T) {
7475
assert.Equal(t, "fromfile", conf.DefaultUser.Name)
7576
}
7677

78+
func TestLegacyConfigFile(t *testing.T) {
79+
mode.Set(mode.TestDev)
80+
dir := t.TempDir()
81+
assert.Nil(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte("server:\n port: 1234\n"), 0o600))
82+
t.Chdir(dir)
83+
84+
_, logs := Get()
85+
86+
fatals := []string{}
87+
for _, log := range logs {
88+
if log.Level == zerolog.FatalLevel {
89+
fatals = append(fatals, log.Msg)
90+
}
91+
}
92+
assert.Len(t, fatals, 1)
93+
assert.Contains(t, fatals[0], "config.yml")
94+
assert.Contains(t, fatals[0], "migrate-config")
95+
assert.Contains(t, fatals[0], "https://gotify.net/docs/migrate-to-3")
96+
}
97+
7798
func TestAddSlash(t *testing.T) {
7899
mode.Set(mode.TestDev)
79100
os.Setenv("GOTIFY_UPLOADEDIMAGESDIR", "../data/images")

config/file.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@ import (
1111

1212
var osStat = os.Stat
1313

14+
func checkLegacyConfigFiles() []FutureLog {
15+
var logs []FutureLog
16+
for _, file := range []string{"config.yml", "/etc/gotify/config.yml"} {
17+
if _, err := osStat(file); err == nil {
18+
logs = append(logs, futureFatal(fmt.Sprintf("found %s, the YAML config file is no longer supported. Convert it with 'gotify-server migrate-config %s', see https://gotify.net/docs/migrate-to-3", file, file)))
19+
}
20+
}
21+
return logs
22+
}
23+
1424
func loadFiles() []FutureLog {
25+
if logs := checkLegacyConfigFiles(); len(logs) > 0 {
26+
return logs
27+
}
28+
1529
if configFile := os.Getenv("GOTIFY_CONFIG_FILE"); configFile != "" {
1630
log, _ := loadFile(configFile)
1731
return []FutureLog{log}

0 commit comments

Comments
 (0)