Commit 2857475da0

2857475da0192e0e88d86c96dfe31612e47953fa

parent: ad8eb038e2

Unsigned

Wraient <wraient.911@gmail.com> · 2026-07-22 19:54 UTC

Fix: append missing config keys like VimKeys on load

Missing defaults (including VimKeys) are appended once to curd.conf when
absent, instead of only on curd_version bumps. Existing keys stay untouched;
subsequent launches do not rewrite the file.

Layout: unified · split

README.md +2 −2
@@ -378,8 +378,8 @@ If the browser reaches the localhost callback page but curd does not continue au
378| `AnimeNameLanguage` | Enum | `english`, `romaji` | Sets the preferred language for anime names. | 378| `AnimeNameLanguage` | Enum | `english`, `romaji` | Sets the preferred language for anime names. |
379| `MpvArgs` | List | all mpv args eg ["--fullscreen=yes", "--mute=yes"] | Add args to mpv player | 379| `MpvArgs` | List | all mpv args eg ["--fullscreen=yes", "--mute=yes"] | Add args to mpv player |
380| `MpvPlaybackStartTimeout` | Integer | `1` to `600` (seconds) | How long to wait for MPV playback to start before trying another provider. Default: `20` | 380| `MpvPlaybackStartTimeout` | Integer | `1` to `600` (seconds) | How long to wait for MPV playback to start before trying another provider. Default: `20` |
381| `AddMissingOptions` | Boolean | `true`, `false` | When `true`, missing options are appended to the config file on **curd version upgrades** (not every launch). Defaults still apply in memory always. | 381| `AddMissingOptions` | Boolean | `true`, `false` | When `true`, any **new** default keys missing from `curd.conf` are **appended once** (never a full rewrite every start). Set `false` to opt out of writing new keys. Defaults still apply in memory always. |
382| `VimKeys` | Boolean | `true`, `false` | When `true`, selection menus use vim motions: `j`/`k`/`h`/`l` (and arrows) to move, `/` or `?` to search. Default: `false` (type-to-filter immediately). | 382| `VimKeys` | Boolean | `true`, `false` | When `true`, selection menus use vim motions: `j`/`k`/`h`/`l` (and arrows) to move, `/` or `?` to search. Default: `false` (type-to-filter immediately). Add `VimKeys=true` to `~/.config/curd/curd.conf` to enable. |
383| `AlternateScreen` | Boolean | `true`, `false` | Toggles the use of an alternate screen buffer for cleaner UI. | 383| `AlternateScreen` | Boolean | `true`, `false` | Toggles the use of an alternate screen buffer for cleaner UI. |
384| `RofiSelection` | Boolean | `true`, `false` | Enables or disables anime selection via Rofi. | 384| `RofiSelection` | Boolean | `true`, `false` | Enables or disables anime selection via Rofi. |
385| `PercentageToMarkComplete`| Integer | `0` to `100` | Sets the percentage of an episode watched to consider it as completed. | 385| `PercentageToMarkComplete`| Integer | `0` to `100` | Sets the percentage of an episode watched to consider it as completed. |
internal/config.go +17 −3
@@ -249,7 +249,7 @@ func LoadConfig(configPath string) (CurdConfig, error) {
249 _, hadTrackingConfigured := configMap["TrackingConfigured"] 249 _, hadTrackingConfigured := configMap["TrackingConfigured"]
250 legacyConfig := !createdConfig && !hadTrackingRemote && !hadTrackingConfigured 250 legacyConfig := !createdConfig && !hadTrackingRemote && !hadTrackingConfigured
251 251
252 // fileMap = keys actually present on disk (never bulk-fill with all defaults). 252 // fileMap = keys actually present on disk.
253 // workMap = file keys + in-memory defaults for PopulateConfig. 253 // workMap = file keys + in-memory defaults for PopulateConfig.
254 fileMap := configMap 254 fileMap := configMap
255 workMap := make(map[string]string, len(fileMap)+len(defaultConfigMap())) 255 workMap := make(map[string]string, len(fileMap)+len(defaultConfigMap()))
@@ -283,8 +283,22 @@ func LoadConfig(configPath string) (CurdConfig, error) {
283 } 283 }
284 } 284 }
285 285
286 // Persist only real migrations (legacy tracking / provider normalize) — not every 286 // Append brand-new default keys (e.g. VimKeys) once when absent — never rewrite the
287 // missing default. New defaults are appended once in MigrateOnVersionUpgrade. 287 // whole file for them. After they're on disk, subsequent starts do nothing.
288 if addMissing && !createdConfig {
289 if added := injectMissingConfigDefaults(fileMap); len(added) > 0 {
290 // Keep workMap in sync with anything just injected.
291 for _, key := range added {
292 workMap[key] = fileMap[key]
293 }
294 if err := appendConfigKeys(configPath, fileMap, added); err != nil {
295 return CurdConfig{}, fmt.Errorf("error appending new config options: %v", err)
296 }
297 Log(fmt.Sprintf("Appended new config options: %s", strings.Join(added, ", ")))
298 }
299 }
300
301 // Persist legacy tracking / provider-token normalize (rewrite only those cases).
288 if addMissing && updated { 302 if addMissing && updated {
289 if err := SaveConfigToFile(configPath, fileMap); err != nil { 303 if err := SaveConfigToFile(configPath, fileMap); err != nil {
290 return CurdConfig{}, fmt.Errorf("error saving updated config file: %v", err) 304 return CurdConfig{}, fmt.Errorf("error saving updated config file: %v", err)
internal/provider_migrate_test.go +58 −16
@@ -114,11 +114,11 @@ func TestMigrateOnVersionUpgradeWritesVersionAndUpdatesProvider(t *testing.T) {
114 } 114 }
115} 115}
116 116
117func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { 117func TestLoadConfigAppendsMissingOptionsIncludingVimKeys(t *testing.T) {
118 tempDir := t.TempDir() 118 tempDir := t.TempDir()
119 configPath := filepath.Join(tempDir, "curd.conf") 119 configPath := filepath.Join(tempDir, "curd.conf")
120 storagePath := filepath.Join(tempDir, "share") 120 storagePath := filepath.Join(tempDir, "share")
121 // Minimal pre-upgrade config: no VimKeys, no MpvPlaybackStartTimeout. 121 // Minimal config: no VimKeys — must be appended on load (not a full rewrite).
122 initial := "StoragePath=" + storagePath + "\n" + 122 initial := "StoragePath=" + storagePath + "\n" +
123 "AddMissingOptions=true\n" + 123 "AddMissingOptions=true\n" +
124 "Provider=stacked\n" + 124 "Provider=stacked\n" +
@@ -131,17 +131,66 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) {
131 if err != nil { 131 if err != nil {
132 t.Fatalf("LoadConfig: %v", err) 132 t.Fatalf("LoadConfig: %v", err)
133 } 133 }
134 // LoadConfig must not rewrite the file just for missing defaults. 134 if config.VimKeys {
135 before, err := os.ReadFile(configPath) 135 t.Fatal("VimKeys default should be false")
136 }
137
138 after, err := os.ReadFile(configPath)
136 if err != nil { 139 if err != nil {
137 t.Fatalf("read config: %v", err) 140 t.Fatalf("read config: %v", err)
138 } 141 }
139 if strings.Contains(string(before), "VimKeys=") { 142 text := string(after)
140 t.Fatalf("LoadConfig should not inject VimKeys into the file every start:\n%s", before) 143 if !strings.Contains(text, "VimKeys=false") {
144 t.Fatalf("expected VimKeys=false in config file:\n%s", text)
141 } 145 }
142 // In-memory default still applies. 146 // Original lines preserved (append-only).
143 if config.VimKeys { 147 if !strings.Contains(text, "Player=mpv") || !strings.Contains(text, "StoragePath="+storagePath) {
144 t.Fatal("VimKeys default should be false in memory") 148 t.Fatalf("original keys should remain:\n%s", text)
149 }
150
151 // Second load must not duplicate VimKeys.
152 if _, err := LoadConfig(configPath); err != nil {
153 t.Fatalf("second LoadConfig: %v", err)
154 }
155 after2, err := os.ReadFile(configPath)
156 if err != nil {
157 t.Fatalf("read config again: %v", err)
158 }
159 if strings.Count(string(after2), "VimKeys=") != 1 {
160 t.Fatalf("VimKeys should appear once, got:\n%s", after2)
161 }
162}
163
164func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) {
165 tempDir := t.TempDir()
166 configPath := filepath.Join(tempDir, "curd.conf")
167 storagePath := filepath.Join(tempDir, "share")
168 // Full-enough config written first so LoadConfig isn't under test here.
169 initial := "StoragePath=" + storagePath + "\n" +
170 "AddMissingOptions=true\n" +
171 "Provider=stacked\n" +
172 "Player=mpv\n"
173 if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil {
174 t.Fatalf("write config: %v", err)
175 }
176 // Pretend we're already on an older stored version with a sparse file.
177 if err := os.MkdirAll(storagePath, 0755); err != nil {
178 t.Fatalf("mkdir storage: %v", err)
179 }
180 if err := os.WriteFile(filepath.Join(storagePath, "curd_version"), []byte("2.0.0\n"), 0644); err != nil {
181 t.Fatalf("write version: %v", err)
182 }
183
184 config := PopulateConfig(map[string]string{
185 "StoragePath": storagePath,
186 "AddMissingOptions": "true",
187 "Provider": "stacked",
188 "Player": "mpv",
189 })
190
191 // Wipe keys that LoadConfig would have added so migration is the injector.
192 if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil {
193 t.Fatalf("rewrite sparse config: %v", err)
145 } 194 }
146 195
147 updated, err := MigrateOnVersionUpgrade(configPath, &config, "2.0.3") 196 updated, err := MigrateOnVersionUpgrade(configPath, &config, "2.0.3")
@@ -160,13 +209,6 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) {
160 if !strings.Contains(text, "VimKeys=false") { 209 if !strings.Contains(text, "VimKeys=false") {
161 t.Fatalf("expected VimKeys=false appended on upgrade:\n%s", text) 210 t.Fatalf("expected VimKeys=false appended on upgrade:\n%s", text)
162 } 211 }
163 if !strings.Contains(text, "MpvPlaybackStartTimeout=20") {
164 t.Fatalf("expected MpvPlaybackStartTimeout appended on upgrade:\n%s", text)
165 }
166 // Original keys preserved at the top (append-only for new keys).
167 if !strings.HasPrefix(strings.TrimSpace(text), "StoragePath=") && !strings.Contains(text, "StoragePath="+storagePath) {
168 t.Fatalf("original StoragePath should remain:\n%s", text)
169 }
170 212
171 // Same version: no further rewrite. 213 // Same version: no further rewrite.
172 updated, err = MigrateOnVersionUpgrade(configPath, &config, "2.0.3") 214 updated, err = MigrateOnVersionUpgrade(configPath, &config, "2.0.3")