mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-10-16 22:07:30 +02:00
refactor: finish the jsonutil dedupe; back up config before auto-upgrade
Duplicated helpers cmd/rip had its own copies of six helpers that already existed in internal/jsonutil: asString (StringFromAny), searchInt (IntFromAny), nestedSearchInt and nestedSearchString (NestedAny/NestedString), firstNonEmpty (FirstNonEmpty) and firstPositiveInt. firstPositiveInt was duplicated a third and fourth time in provider/yandex and provider/beatport, so it moves into jsonutil alongside the rest. jsonutil is back to 100% coverage. searchBool is deliberately left alone, with a comment saying why: it accepts only a real bool, and switching it to jsonutil.BoolFromAny would start coercing strings and numbers, changing how search results get labelled. config auto-upgrade is no longer destructive UpgradeOutdated re-serializes the whole config from the parsed struct, so any key this version does not know about is dropped, and it runs automatically from run() without prompting. It now copies the original to <path>.bak-<unixtime> (0600, since it holds the same credentials) and validates the result before writing. It returns the backup path so the CLI can tell the user where it went. The comment states plainly that this is a version stamp rather than a migration framework. cmd/rip tests The command bodies are testable now that they are functions returning exit codes rather than arms of a switch that called os.Exit. Covers the usage errors for every command, the quality floor on `rip id`, config path output, the --version flag, and that applyQualityOverride reaches all six sources and clamps only the two with a floor. cmd/rip coverage 17% -> 26%. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
13 files changed
+447
-211
No files matched your search
@@ -532,7 +532,7 @@ func (c *Client) normalizeRelease(raw map[string]any, tracks []any) map[string]a
|
||||
"title": jsonutil.StringFromAny(raw["name"]),
|
||||
"artist": map[string]any{"name": artist},
|
||||
"release_date_original": jsonutil.StringFromAny(raw["new_release_date"]),
|
||||
"tracks_count": firstPositiveInt(jsonutil.IntFromAny(raw["track_count"]), len(items)),
|
||||
"tracks_count": jsonutil.FirstPositiveInt(jsonutil.IntFromAny(raw["track_count"]), len(items)),
|
||||
"maximum_bit_depth": 16,
|
||||
"maximum_sampling_rate": "44.1",
|
||||
"image": imageMap(raw["image"]),
|
||||
@@ -648,7 +648,7 @@ func playlistMetadata(raw map[string]any, tracks []any) map[string]any {
|
||||
"id": jsonutil.StringFromAny(raw["id"]),
|
||||
"name": name,
|
||||
"title": name,
|
||||
"tracks_count": firstPositiveInt(jsonutil.IntFromAny(raw["track_count"]), len(tracks)),
|
||||
"tracks_count": jsonutil.FirstPositiveInt(jsonutil.IntFromAny(raw["track_count"]), len(tracks)),
|
||||
"image": imageMap(raw["image"]),
|
||||
"tracks": map[string]any{"items": tracks},
|
||||
}
|
||||
@@ -829,15 +829,6 @@ func sliceAny(v any) []any {
|
||||
return s
|
||||
}
|
||||
|
||||
func firstPositiveInt(vals ...int) int {
|
||||
for _, v := range vals {
|
||||
if v > 0 {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func cloneValues(in url.Values) url.Values {
|
||||
out := url.Values{}
|
||||
for k, vals := range in {
|
||||
|
||||
@@ -581,7 +581,7 @@ func normalizeTrack(raw map[string]any, fallbackID string, artistSeparator strin
|
||||
}
|
||||
trackNumber, discNumber := trackNumbers(albumRaw)
|
||||
trackTotal := jsonutil.IntFromAny(albumRaw["trackCount"])
|
||||
discTotal := firstPositiveInt(
|
||||
discTotal := jsonutil.FirstPositiveInt(
|
||||
jsonutil.IntFromAny(raw["volumesCount"]),
|
||||
jsonutil.IntFromAny(albumRaw["volumesCount"]),
|
||||
)
|
||||
@@ -726,7 +726,7 @@ func normalizeArtistSearchItem(raw map[string]any) map[string]any {
|
||||
"id": jsonutil.StringFromAny(raw["id"]),
|
||||
"title": jsonutil.StringFromAny(raw["name"]),
|
||||
"name": jsonutil.StringFromAny(raw["name"]),
|
||||
"albums_count": firstPositiveInt(
|
||||
"albums_count": jsonutil.FirstPositiveInt(
|
||||
jsonutil.IntFromAny(jsonutil.NestedAny(raw, "counts", "directAlbums")),
|
||||
jsonutil.IntFromAny(jsonutil.NestedAny(raw, "counts", "tracks")),
|
||||
),
|
||||
@@ -833,7 +833,7 @@ func trackNumbers(albumRaw map[string]any) (int, int) {
|
||||
if trackNumber > 0 {
|
||||
trackNumber++
|
||||
}
|
||||
return trackNumber, firstPositiveInt(jsonutil.IntFromAny(position["volume"]), 1)
|
||||
return trackNumber, jsonutil.FirstPositiveInt(jsonutil.IntFromAny(position["volume"]), 1)
|
||||
}
|
||||
|
||||
func canonicalTrackRefFromRaw(raw map[string]any, fallback string) string {
|
||||
@@ -1019,15 +1019,6 @@ func waitRetry(ctx context.Context, delay time.Duration) error {
|
||||
}
|
||||
}
|
||||
|
||||
func firstPositiveInt(vals ...int) int {
|
||||
for _, v := range vals {
|
||||
if v > 0 {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// signKey and legacyMP3Salt read the vendor request-signing keys from config,
|
||||
// falling back to the built-in defaults.
|
||||
func (c *Client) signKey() string {
|
||||
|
||||
Reference in new issue
Block a user