Commit eab626deed

eab626deed98f008afd58b1e063c195a69db9f6f

parent: 4a1152687e

Unsigned

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

Track config options by introduced version for upgrade injects

Only append options registered in configOptionsIntroducedInVersion for
versions the user actually crossed (from,to], instead of dumping every
missing default. Bump to 2.0.3 so VimKeys and MpvPlaybackStartTimeout
inject once on upgrade from 2.0.2.

Layout: unified · split

Build/curd-windows-build.iss +1 −1
@@ -1,6 +1,6 @@
11[Setup]
22AppName=Curd Installer
3AppVersion=2.0.2
3AppVersion=2.0.3
44DefaultDirName={userappdata}\Curd
55PrivilegesRequired=lowest
66AllowNoIcons=yes
README.md +1 −1
@@ -378,7 +378,7 @@ If the browser reaches the localhost callback page but curd does not continue au
378378| `AnimeNameLanguage` | Enum | `english`, `romaji` | Sets the preferred language for anime names. |
379379| `MpvArgs` | List | all mpv args eg ["--fullscreen=yes", "--mute=yes"] | Add args to mpv player |
380380| `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`, 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. |
381| `AddMissingOptions` | Boolean | `true`, `false` | When `true`, on a **version upgrade** only options registered as new for that release are **appended** to `curd.conf` (not every historical default). Set `false` to opt out. Defaults still apply in memory always. |
382382| `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. |
383383| `AlternateScreen` | Boolean | `true`, `false` | Toggles the use of an alternate screen buffer for cleaner UI. |
384384| `RofiSelection` | Boolean | `true`, `false` | Enables or disables anime selection via Rofi. |
VERSION.txt +1 −1
@@ -1 +1 @@
12.0.2
\ No newline at end of file
12.0.3
cmd/curd/main.go +1 −1
@@ -17,7 +17,7 @@ var version string // Will be set by ldflags during build
1717
1818func resolvedVersion() string {
1919 if version == "" {
20 return "2.0.2"
20 return "2.0.3"
2121 }
2222
2323 return version
internal/config.go +4 −14
@@ -283,20 +283,10 @@ func LoadConfig(configPath string) (CurdConfig, error) {
283283 }
284284 }
285285
286 // Append brand-new default keys (e.g. VimKeys) once when absent — never rewrite the
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 }
286 // New config keys are NOT bulk-appended here. Only MigrateOnVersionUpgrade
287 // injects options registered in configOptionsIntroducedInVersion() for the
288 // versions the user actually crossed — so sparse configs never get every
289 // historical default dumped in on a normal launch.
300290
301291 // Persist legacy tracking / provider-token normalize (rewrite only those cases).
302292 if addMissing && updated {
internal/provider_migrate.go +48 −13
@@ -106,25 +106,58 @@ func writeStoredCurdVersion(storagePath, version string) error {
106106 return os.WriteFile(storageVersionFilePath(storagePath), []byte(version+"\n"), 0644)
107107}
108108
109// injectMissingConfigDefaults adds any defaultConfigMap keys that are absent from
110// configMap. Returns the sorted list of newly injected keys.
111func injectMissingConfigDefaults(configMap map[string]string) []string {
109// configOptionsIntroducedInVersion maps config keys to the first curd release that
110// introduced them. Only these keys are eligible for automatic append on upgrade.
111// Baseline options (Player, StoragePath, …) are NOT listed — they only appear via
112// createDefaultConfig for brand-new installs, never re-appended into sparse configs.
113//
114// When adding a new option:
115// 1. Add it to CurdConfig + defaultConfigMap()
116// 2. Register it here under the release version that ships it
117// 3. Bump VERSION.txt so MigrateOnVersionUpgrade runs for existing users
118func configOptionsIntroducedInVersion() map[string]string {
119 return map[string]string{
120 // 2.0.3 — playback fallback timeout + vim selection motions
121 "MpvPlaybackStartTimeout": "2.0.3",
122 "VimKeys": "2.0.3",
123 }
124}
125
126// injectConfigOptionsSince appends defaults for options introduced in versions
127// (fromVersion, toVersion] that are still missing from configMap.
128// Example: from 2.0.2 → 2.0.3 injects only keys introduced in 2.0.3, not every
129// historical default.
130func injectConfigOptionsSince(configMap map[string]string, fromVersion, toVersion string) []string {
112131 if configMap == nil {
113132 return nil
114133 }
115134 defaults := defaultConfigMap()
116 keys := make([]string, 0, len(defaults))
117 for key := range defaults {
135 introduced := configOptionsIntroducedInVersion()
136
137 keys := make([]string, 0, len(introduced))
138 for key := range introduced {
118139 keys = append(keys, key)
119140 }
120141 sort.Strings(keys)
121142
122143 added := make([]string, 0)
123144 for _, key := range keys {
145 since := introduced[key]
146 // Only options born after the user's previous version and at/before this release.
147 if !versionLess(fromVersion, since) {
148 continue // introduced at or before fromVersion — user already "passed" that release
149 }
150 if !versionLessOrEqual(since, toVersion) {
151 continue // not shipped yet in toVersion
152 }
124153 if _, exists := configMap[key]; exists {
125154 continue
126155 }
127 configMap[key] = defaults[key]
156 value, ok := defaults[key]
157 if !ok {
158 continue
159 }
160 configMap[key] = value
128161 added = append(added, key)
129162 }
130163 return added
@@ -155,9 +188,9 @@ func appendConfigKeys(configPath string, configMap map[string]string, keys []str
155188}
156189
157190// MigrateOnVersionUpgrade updates stored state and config when curd is upgraded.
158// On version change it injects any new default config options (append-only) and
159// runs provider migrations. Same-version launches do not rewrite the config file
160// just to fill defaults — that avoids churning user config every start.
191// On version change it appends only config options registered as introduced in
192// versions (storedVersion, appVersion], then runs provider migrations.
193// Same-version launches do not rewrite the config file.
161194// Returns whether the config file was updated.
162195func MigrateOnVersionUpgrade(configPath string, config *CurdConfig, appVersion string) (bool, error) {
163196 if config == nil {
@@ -176,8 +209,9 @@ func MigrateOnVersionUpgrade(configPath string, config *CurdConfig, appVersion s
176209
177210 storedVersion := readStoredCurdVersion(storagePath)
178211 configUpdated := false
212 versionChanged := storedVersion != appVersion
179213
180 if storedVersion != appVersion && strings.TrimSpace(configPath) != "" {
214 if versionChanged && strings.TrimSpace(configPath) != "" {
181215 configMap, err := LoadConfigFromFile(configPath)
182216 if err != nil {
183217 return false, err
@@ -195,12 +229,13 @@ func MigrateOnVersionUpgrade(configPath string, config *CurdConfig, appVersion s
195229 }
196230
197231 if addMissing {
198 if added := injectMissingConfigDefaults(configMap); len(added) > 0 {
232 if added := injectConfigOptionsSince(configMap, storedVersion, appVersion); len(added) > 0 {
199233 if err := appendConfigKeys(configPath, configMap, added); err != nil {
200234 return false, fmt.Errorf("append new config options: %w", err)
201235 }
202236 configUpdated = true
203 Log(fmt.Sprintf("Injected new config options on upgrade to %s: %s", appVersion, strings.Join(added, ", ")))
237 Log(fmt.Sprintf("Injected config options for upgrade %s → %s: %s",
238 storedVersion, appVersion, strings.Join(added, ", ")))
204239 }
205240 }
206241
@@ -217,7 +252,7 @@ func MigrateOnVersionUpgrade(configPath string, config *CurdConfig, appVersion s
217252 next := PopulateConfig(configMap)
218253 normalizeTrackingConfig(&next)
219254 *config = next
220 } else if storedVersion != appVersion {
255 } else if versionChanged {
221256 // No config path, still run in-memory provider migration.
222257 if nextProvider, changed := migrateProviderConfig(config.Provider); changed {
223258 config.Provider = nextProvider
internal/provider_migrate_test.go +51 −40
@@ -114,11 +114,10 @@ func TestMigrateOnVersionUpgradeWritesVersionAndUpdatesProvider(t *testing.T) {
114114 }
115115}
116116
117func TestLoadConfigAppendsMissingOptionsIncludingVimKeys(t *testing.T) {
117func TestLoadConfigDoesNotDumpAllDefaultsIntoSparseFile(t *testing.T) {
118118 tempDir := t.TempDir()
119119 configPath := filepath.Join(tempDir, "curd.conf")
120120 storagePath := filepath.Join(tempDir, "share")
121 // Minimal config: no VimKeys — must be appended on load (not a full rewrite).
122121 initial := "StoragePath=" + storagePath + "\n" +
123122 "AddMissingOptions=true\n" +
124123 "Provider=stacked\n" +
@@ -131,8 +130,9 @@ func TestLoadConfigAppendsMissingOptionsIncludingVimKeys(t *testing.T) {
131130 if err != nil {
132131 t.Fatalf("LoadConfig: %v", err)
133132 }
133 // In-memory defaults still apply.
134134 if config.VimKeys {
135 t.Fatal("VimKeys default should be false")
135 t.Fatal("VimKeys default should be false in memory")
136136 }
137137
138138 after, err := os.ReadFile(configPath)
@@ -140,32 +140,19 @@ func TestLoadConfigAppendsMissingOptionsIncludingVimKeys(t *testing.T) {
140140 t.Fatalf("read config: %v", err)
141141 }
142142 text := string(after)
143 if !strings.Contains(text, "VimKeys=false") {
144 t.Fatalf("expected VimKeys=false in config file:\n%s", text)
145 }
146 // Original lines preserved (append-only).
147 if !strings.Contains(text, "Player=mpv") || !strings.Contains(text, "StoragePath="+storagePath) {
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)
143 // Sparse file must not gain every historical default on a normal load.
144 if strings.Contains(text, "VimKeys=") {
145 t.Fatalf("LoadConfig must not append versioned options without an upgrade:\n%s", text)
158146 }
159 if strings.Count(string(after2), "VimKeys=") != 1 {
160 t.Fatalf("VimKeys should appear once, got:\n%s", after2)
147 if strings.Contains(text, "SkipOp=") {
148 t.Fatalf("LoadConfig must not dump baseline defaults into sparse configs:\n%s", text)
161149 }
162150}
163151
164func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) {
152func TestMigrateOnVersionUpgradeInjectsOnlyOptionsForCrossedVersions(t *testing.T) {
165153 tempDir := t.TempDir()
166154 configPath := filepath.Join(tempDir, "curd.conf")
167155 storagePath := filepath.Join(tempDir, "share")
168 // Full-enough config written first so LoadConfig isn't under test here.
169156 initial := "StoragePath=" + storagePath + "\n" +
170157 "AddMissingOptions=true\n" +
171158 "Provider=stacked\n" +
@@ -173,11 +160,10 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) {
173160 if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil {
174161 t.Fatalf("write config: %v", err)
175162 }
176 // Pretend we're already on an older stored version with a sparse file.
177163 if err := os.MkdirAll(storagePath, 0755); err != nil {
178164 t.Fatalf("mkdir storage: %v", err)
179165 }
180 if err := os.WriteFile(filepath.Join(storagePath, "curd_version"), []byte("2.0.0\n"), 0644); err != nil {
166 if err := os.WriteFile(filepath.Join(storagePath, "curd_version"), []byte("2.0.2\n"), 0644); err != nil {
181167 t.Fatalf("write version: %v", err)
182168 }
183169
@@ -188,17 +174,12 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) {
188174 "Player": "mpv",
189175 })
190176
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)
194 }
195
196177 updated, err := MigrateOnVersionUpgrade(configPath, &config, "2.0.3")
197178 if err != nil {
198179 t.Fatalf("MigrateOnVersionUpgrade: %v", err)
199180 }
200181 if !updated {
201 t.Fatal("expected config file to gain new options on version upgrade")
182 t.Fatal("expected config file to gain 2.0.3 options on upgrade")
202183 }
203184
204185 after, err := os.ReadFile(configPath)
@@ -207,10 +188,16 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) {
207188 }
208189 text := string(after)
209190 if !strings.Contains(text, "VimKeys=false") {
210 t.Fatalf("expected VimKeys=false appended on upgrade:\n%s", text)
191 t.Fatalf("expected VimKeys=false for 2.0.3 upgrade:\n%s", text)
192 }
193 if !strings.Contains(text, "MpvPlaybackStartTimeout=20") {
194 t.Fatalf("expected MpvPlaybackStartTimeout for 2.0.3 upgrade:\n%s", text)
195 }
196 // Must NOT dump unrelated baseline keys.
197 if strings.Contains(text, "SkipOp=") || strings.Contains(text, "DiscordPresence=") {
198 t.Fatalf("upgrade must not inject unregistered baseline options:\n%s", text)
211199 }
212200
213 // Same version: no further rewrite.
214201 updated, err = MigrateOnVersionUpgrade(configPath, &config, "2.0.3")
215202 if err != nil {
216203 t.Fatalf("second migrate: %v", err)
@@ -220,20 +207,44 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) {
220207 }
221208}
222209
223func TestInjectMissingConfigDefaultsIdempotent(t *testing.T) {
210func TestInjectConfigOptionsSinceOnlyCrossedVersions(t *testing.T) {
224211 m := map[string]string{"Player": "mpv"}
225 added := injectMissingConfigDefaults(m)
226 if len(added) == 0 {
227 t.Fatal("expected missing keys to be injected")
228 }
229 if _, ok := m["VimKeys"]; !ok {
230 t.Fatal("expected VimKeys default")
212 // Already on 2.0.3 — nothing new.
213 if added := injectConfigOptionsSince(m, "2.0.3", "2.0.3"); len(added) != 0 {
214 t.Fatalf("same version should inject nothing, got %v", added)
215 }
216 // 2.0.2 → 2.0.3 gets VimKeys + timeout only.
217 added := injectConfigOptionsSince(m, "2.0.2", "2.0.3")
218 if len(added) != 2 {
219 t.Fatalf("expected 2 options for 2.0.2→2.0.3, got %v", added)
220 }
221 want := map[string]bool{"VimKeys": true, "MpvPlaybackStartTimeout": true}
222 for _, key := range added {
223 if !want[key] {
224 t.Fatalf("unexpected injected key %q in %v", key, added)
225 }
231226 }
232 if second := injectMissingConfigDefaults(m); len(second) != 0 {
227 // Already present keys are skipped.
228 if second := injectConfigOptionsSince(m, "2.0.2", "2.0.3"); len(second) != 0 {
233229 t.Fatalf("second inject should be empty, got %v", second)
234230 }
235231}
236232
233func TestCompareVersionsOrdering(t *testing.T) {
234 if !versionLess("2.0.2", "2.0.3") {
235 t.Fatal("2.0.2 should be < 2.0.3")
236 }
237 if !versionLess("", "2.0.3") {
238 t.Fatal("empty stored version should be older than a release")
239 }
240 if versionLess("2.0.3", "2.0.3") {
241 t.Fatal("equal versions should not be less")
242 }
243 if !versionLessOrEqual("2.0.3", "2.0.3") {
244 t.Fatal("equal versions should be <= ")
245 }
246}
247
237248func TestConfiguredProviderNamesUsesStackedByDefault(t *testing.T) {
238249 withAllProvidersEnabledForTest(t)
239250 got := ConfiguredProviderNames(&CurdConfig{})
internal/version.go +84
@@ -2,6 +2,7 @@ package internal
22
33import (
44 "path/filepath"
5 "strconv"
56 "strings"
67)
78
@@ -26,3 +27,86 @@ func CurdVersion() string {
2627func storageVersionFilePath(storagePath string) string {
2728 return filepath.Join(strings.TrimSpace(storagePath), "curd_version")
2829}
30
31// parseVersionParts parses "2.0.3" / "v2.0.3-rc1" into [major, minor, patch].
32// Non-numeric suffixes are ignored. Unparseable versions return nil.
33func parseVersionParts(version string) []int {
34 version = strings.TrimSpace(version)
35 version = strings.TrimPrefix(version, "v")
36 version = strings.TrimPrefix(version, "V")
37 if version == "" || strings.EqualFold(version, "dev") {
38 return nil
39 }
40 // Drop pre-release / build metadata: 2.0.3-rc1+meta → 2.0.3
41 if i := strings.IndexAny(version, "-+"); i >= 0 {
42 version = version[:i]
43 }
44 parts := strings.Split(version, ".")
45 if len(parts) == 0 {
46 return nil
47 }
48 out := make([]int, 0, 3)
49 for i := 0; i < 3; i++ {
50 if i >= len(parts) {
51 out = append(out, 0)
52 continue
53 }
54 n, err := strconv.Atoi(parts[i])
55 if err != nil {
56 return nil
57 }
58 out = append(out, n)
59 }
60 return out
61}
62
63// compareVersions returns -1 if a<b, 0 if a==b, 1 if a>b.
64// Unparseable versions compare as equal to each other and less than parseable ones
65// only when empty; "dev" is treated as greater than any release (no auto-inject from dev churn).
66func compareVersions(a, b string) int {
67 a = strings.TrimSpace(a)
68 b = strings.TrimSpace(b)
69 if a == b {
70 return 0
71 }
72 ap := parseVersionParts(a)
73 bp := parseVersionParts(b)
74 if ap == nil && bp == nil {
75 if a < b {
76 return -1
77 }
78 if a > b {
79 return 1
80 }
81 return 0
82 }
83 if ap == nil {
84 // empty/unknown stored version → treat as older than any real release
85 if a == "" {
86 return -1
87 }
88 // "dev" or garbage: treat as equal to itself only; vs release → greater (skip inject churn)
89 return 1
90 }
91 if bp == nil {
92 if b == "" {
93 return 1
94 }
95 return -1
96 }
97 for i := 0; i < 3; i++ {
98 if ap[i] < bp[i] {
99 return -1
100 }
101 if ap[i] > bp[i] {
102 return 1
103 }
104 }
105 return 0
106}
107
108// versionLess reports whether a is strictly older than b.
109func versionLess(a, b string) bool { return compareVersions(a, b) < 0 }
110
111// versionLessOrEqual reports whether a is older than or equal to b.
112func versionLessOrEqual(a, b string) bool { return compareVersions(a, b) <= 0 }