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
@@ -7,6 +7,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
)
|
||||
@@ -370,25 +371,42 @@ func NormalizeCodec(raw string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func UpgradeOutdated(path string) (string, error) {
|
||||
// UpgradeOutdated rewrites an outdated config at the current version. It runs
|
||||
// automatically, without prompting, so it copies the original aside first: the
|
||||
// rewrite re-serializes the whole file from the parsed struct, which drops any
|
||||
// key this version does not know about. It returns the config path and the
|
||||
// backup path.
|
||||
//
|
||||
// This is a version stamp, not a migration framework. There are no per-version
|
||||
// transforms, so a future key rename still needs explicit handling here.
|
||||
func UpgradeOutdated(path string) (resolved string, backup string, err error) {
|
||||
resolvedPath, err := resolvePath(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
raw, err := os.ReadFile(resolvedPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
backupPath := fmt.Sprintf("%s.bak-%d", resolvedPath, time.Now().Unix())
|
||||
if err = os.WriteFile(backupPath, raw, 0o600); err != nil {
|
||||
return "", "", fmt.Errorf("could not back up %s before upgrading: %w", resolvedPath, err)
|
||||
}
|
||||
|
||||
data := DefaultConfigData()
|
||||
if err = toml.Unmarshal(raw, &data); err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
applyRuntimeDefaults(&data)
|
||||
data.Misc.Version = CurrentConfigVersion
|
||||
if err = saveConfigData(resolvedPath, data); err != nil {
|
||||
return "", err
|
||||
if err = Validate(&data); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return resolvedPath, nil
|
||||
if err = saveConfigData(resolvedPath, data); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return resolvedPath, backupPath, nil
|
||||
}
|
||||
|
||||
func (c *Config) SaveFile() error {
|
||||
|
||||
Reference in new issue
Block a user