config_migrator.go RawHistoryFolder
1package port
2
3// MigrationResult contains the result of a config migration check.
4type MigrationResult struct {
5 // MissingKeys contains the keys that exist in defaults but not in user config.
6 MissingKeys []string
7 // ConfigFile is the path to the user's config file.
8 ConfigFile string
9}
10
11// KeyInfo contains metadata about a config key for display purposes.
12type KeyInfo struct {
13 // Key is the dot-notation key path (e.g., "update.enable_on_startup").
14 Key string
15 // Type is the Go type of the value (e.g., "bool", "int", "string").
16 Type string
17 // DefaultValue is a string representation of the default value.
18 DefaultValue string
19}
20
21// KeyChangeType indicates the type of config key change.
22type KeyChangeType int
23
24const (
25 // KeyChangeAdded indicates a new key was added to defaults.
26 KeyChangeAdded KeyChangeType = iota
27 // KeyChangeRemoved indicates a key in user config no longer exists in defaults.
28 KeyChangeRemoved
29 // KeyChangeRenamed indicates a key was renamed (detected via similarity).
30 KeyChangeRenamed
31)
32
33// String returns a display symbol for the change type.
34func (t KeyChangeType) String() string {
35 switch t {
36 case KeyChangeAdded:
37 return "+"
38 case KeyChangeRemoved:
39 return "-"
40 case KeyChangeRenamed:
41 return "~"
42 default:
43 return "?"
44 }
45}
46
47// KeyChange represents a detected change between user config and defaults.
48type KeyChange struct {
49 Type KeyChangeType // Type of change
50 OldKey string // Old key name (for removed/renamed)
51 NewKey string // New key name (for added/renamed)
52 OldValue string // Old value (for renamed/consolidated)
53 NewValue string // New/default value
54}
55
56// ConfigMigrator checks for and applies config migrations.
57type ConfigMigrator interface {
58 // CheckMigration checks if user config is missing any default keys.
59 // Returns nil if no migration is needed (config file doesn't exist or is complete).
60 CheckMigration() (*MigrationResult, error)
61
62 // DetectChanges analyzes user config and returns all detected changes.
63 // This provides a detailed diff-like view of what migration would do.
64 DetectChanges() ([]KeyChange, error)
65
66 // Migrate adds missing default keys and removes deprecated keys from the user's config file.
67 // Returns the list of keys that were added/renamed/removed.
68 Migrate() ([]string, error)
69
70 // GetKeyInfo returns detailed information about a config key.
71 GetKeyInfo(key string) KeyInfo
72
73 // GetConfigFile returns the path to the user's config file.
74 GetConfigFile() (string, error)
75}
76
77// DiffFormatter formats config changes for display.
78type DiffFormatter interface {
79 // FormatChangesAsDiff returns changes formatted as a diff-like string for display.
80 FormatChangesAsDiff(changes []KeyChange) string
81}