Skip to content

Commit 542c551

Browse files
ploMP4tharropoulos
authored andcommitted
refactor(config): simplify configuration loading and merge logic
- Replace Initialize() with Load() function that uses global viper instance - Remove complex MergeProperties() function in favor of simpler property merging - Consolidate Config/GlobalConfig/PresetConfig types into unified config struct - Move config loading responsibility from parseSlides() to main RunE function - Simplify Properties.UnmarshalYAML() to handle preset resolution directly - Remove merge.go and associated test files, reducing code complexity - Update all tests to use global viper instance instead of local instances - Rename ApplyStyle() to Apply() for consistency
1 parent 3bda42a commit 542c551

File tree

7 files changed

+177
-451
lines changed

7 files changed

+177
-451
lines changed

cmd/root.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ var rootCmd = &cobra.Command{
4040
return nil
4141
},
4242
RunE: func(cmd *cobra.Command, args []string) error {
43+
if err := config.Load(configPath); err != nil {
44+
return err
45+
}
46+
4347
filename := args[0]
4448

4549
data, err := os.ReadFile(filename)
@@ -98,7 +102,12 @@ var rootCmd = &cobra.Command{
98102
},
99103
}
100104

101-
func watchFileChanges(watcher *fsnotify.Watcher, p *tea.Program, filename, absPath string, configPath string) {
105+
func watchFileChanges(
106+
watcher *fsnotify.Watcher,
107+
p *tea.Program,
108+
filename, absPath string,
109+
configPath string,
110+
) {
102111
var debounceTimer *time.Timer
103112

104113
for {
@@ -153,8 +162,6 @@ func Execute() {
153162
func parseSlides(data string) (*tui.Slide, error) {
154163
slides := strings.Split(string(data), "----\n")
155164

156-
config.SetConfigPath(configPath)
157-
158165
rootSlide, properties := parseSlide(slides[0])
159166
p, err := config.NewProperties(properties)
160167

internal/config/config.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,59 @@ import (
55
"os"
66
"path/filepath"
77

8-
"github.com/museslabs/kyma/internal/tui/transitions"
98
"github.com/spf13/viper"
9+
10+
"github.com/museslabs/kyma/internal/tui/transitions"
1011
)
1112

1213
const (
1314
configName = "kyma"
1415
configType = "yaml"
1516
)
1617

17-
var configPath string
18-
19-
func SetConfigPath(path string) {
20-
configPath = path
21-
}
22-
23-
type Config struct {
24-
Global GlobalConfig `mapstructure:"global"`
25-
Presets map[string]PresetConfig `mapstructure:"presets"`
26-
}
18+
var GlobalConfig config
2719

28-
type GlobalConfig struct {
29-
Style StyleConfig `mapstructure:"style"`
30-
Transition transitions.Transition `mapstructure:"transition"`
20+
type config struct {
21+
Global presetConfig `mapstructure:"global"`
22+
Presets map[string]presetConfig `mapstructure:"presets"`
3123
}
3224

33-
type PresetConfig struct {
25+
type presetConfig struct {
3426
Style StyleConfig `mapstructure:"style"`
3527
Transition transitions.Transition `mapstructure:"transition"`
3628
}
3729

38-
func Initialize(configPath string) (*viper.Viper, error) {
39-
v := viper.New()
40-
v.SetConfigName(configName)
41-
v.SetConfigType(configType)
30+
func Load(configPath string) error {
31+
viper.SetConfigName(configName)
32+
viper.SetConfigType(configType)
4233

4334
home, err := os.UserHomeDir()
4435
if err != nil {
45-
return nil, fmt.Errorf("failed to get user home directory: %w", err)
36+
return fmt.Errorf("failed to get user home directory: %w", err)
4637
}
4738

4839
if configPath != "" {
49-
v.SetConfigFile(configPath)
40+
viper.SetConfigFile(configPath)
5041
} else {
51-
v.AddConfigPath(".")
42+
viper.AddConfigPath(".")
5243

5344
// Check XDG_CONFIG_HOME first, then fall back to ~/.config
5445
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
5546
if xdgConfigHome != "" {
56-
v.AddConfigPath(filepath.Join(xdgConfigHome, "kyma"))
47+
viper.AddConfigPath(filepath.Join(xdgConfigHome, "kyma"))
5748
}
58-
v.AddConfigPath(filepath.Join(home, ".config", "kyma"))
49+
viper.AddConfigPath(filepath.Join(home, ".config", "kyma"))
5950

6051
if err := createDefaultConfig(home); err != nil {
61-
return nil, fmt.Errorf("failed to create default config: %w", err)
52+
return fmt.Errorf("failed to create default config: %w", err)
6253
}
6354
}
6455

65-
if err := v.ReadInConfig(); err != nil {
66-
return nil, fmt.Errorf("failed to read config: %w", err)
56+
if err := viper.ReadInConfig(); err != nil {
57+
return fmt.Errorf("failed to read config: %w", err)
6758
}
6859

69-
return v, nil
60+
return nil
7061
}
7162

7263
func createDefaultConfig(home string) error {

internal/config/config_test.go

Lines changed: 112 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/spf13/viper"
99
)
1010

11-
func TestInitialize(t *testing.T) {
11+
func TestLoad(t *testing.T) {
1212
tmpDir, err := os.MkdirTemp("", "kyma-test-*")
1313
if err != nil {
1414
t.Fatalf("Failed to create temp dir: %v", err)
@@ -71,58 +71,82 @@ presets:
7171
os.Setenv("HOME", tmpDir)
7272
defer os.Setenv("HOME", oldHome)
7373

74-
v, err := Initialize(tt.configPath)
74+
err := Load(tt.configPath)
7575
if (err != nil) != tt.wantErr {
7676
t.Errorf("Initialize() error = %v, wantErr %v", err, tt.wantErr)
7777
return
7878
}
7979

8080
if tt.checkVals {
8181
if tt.configPath == testConfigPath {
82-
if v.GetString("global.style.border") != "rounded" {
83-
t.Errorf("global.style.border = %v, want %v", v.GetString("global.style.border"), "rounded")
82+
if viper.GetString("global.style.border") != "rounded" {
83+
t.Errorf(
84+
"global.style.border = %v, want %v",
85+
viper.GetString("global.style.border"),
86+
"rounded",
87+
)
8488
}
85-
if v.GetString("global.style.border_color") != "#FF0000" {
86-
t.Errorf("global.style.border_color = %v, want %v", v.GetString("global.style.border_color"), "#FF0000")
89+
if viper.GetString("global.style.border_color") != "#FF0000" {
90+
t.Errorf(
91+
"global.style.border_color = %v, want %v",
92+
viper.GetString("global.style.border_color"),
93+
"#FF0000",
94+
)
8795
}
88-
if v.GetString("global.style.layout") != "center" {
89-
t.Errorf("global.style.layout = %v, want %v", v.GetString("global.style.layout"), "center")
96+
if viper.GetString("global.style.layout") != "center" {
97+
t.Errorf(
98+
"global.style.layout = %v, want %v",
99+
viper.GetString("global.style.layout"),
100+
"center",
101+
)
90102
}
91-
if v.GetString("global.style.theme") != "dracula" {
92-
t.Errorf("global.style.theme = %v, want %v", v.GetString("global.style.theme"), "dracula")
103+
if viper.GetString("global.style.theme") != "dracula" {
104+
t.Errorf(
105+
"global.style.theme = %v, want %v",
106+
viper.GetString("global.style.theme"),
107+
"dracula",
108+
)
93109
}
94110

95-
if v.GetString("presets.test.style.border") != "hidden" {
96-
t.Errorf("presets.test.style.border = %v, want %v", v.GetString("presets.test.style.border"), "hidden")
111+
if viper.GetString("presets.test.style.border") != "hidden" {
112+
t.Errorf(
113+
"presets.test.style.border = %v, want %v",
114+
viper.GetString("presets.test.style.border"),
115+
"hidden",
116+
)
97117
}
98-
if v.GetString("presets.test.style.theme") != "notty" {
99-
t.Errorf("presets.test.style.theme = %v, want %v", v.GetString("presets.test.style.theme"), "notty")
118+
if viper.GetString("presets.test.style.theme") != "notty" {
119+
t.Errorf(
120+
"presets.test.style.theme = %v, want %v",
121+
viper.GetString("presets.test.style.theme"),
122+
"notty",
123+
)
100124
}
101125
} else {
102-
if v.GetString("global.style.border") != "rounded" {
103-
t.Errorf("global.style.border = %v, want %v", v.GetString("global.style.border"), "rounded")
126+
if viper.GetString("global.style.border") != "rounded" {
127+
t.Errorf("global.style.border = %v, want %v", viper.GetString("global.style.border"), "rounded")
104128
}
105-
if v.GetString("global.style.border_color") != "#9999CC" {
106-
t.Errorf("global.style.border_color = %v, want %v", v.GetString("global.style.border_color"), "#9999CC")
129+
if viper.GetString("global.style.border_color") != "#9999CC" {
130+
t.Errorf("global.style.border_color = %v, want %v", viper.GetString("global.style.border_color"), "#9999CC")
107131
}
108-
if v.GetString("global.style.layout") != "center" {
109-
t.Errorf("global.style.layout = %v, want %v", v.GetString("global.style.layout"), "center")
132+
if viper.GetString("global.style.layout") != "center" {
133+
t.Errorf("global.style.layout = %v, want %v", viper.GetString("global.style.layout"), "center")
110134
}
111-
if v.GetString("global.style.theme") != "dracula" {
112-
t.Errorf("global.style.theme = %v, want %v", v.GetString("global.style.theme"), "dracula")
135+
if viper.GetString("global.style.theme") != "dracula" {
136+
t.Errorf("global.style.theme = %v, want %v", viper.GetString("global.style.theme"), "dracula")
113137
}
114138

115-
if v.GetString("presets.minimal.style.border") != "hidden" {
116-
t.Errorf("presets.minimal.style.border = %v, want %v", v.GetString("presets.minimal.style.border"), "hidden")
139+
if viper.GetString("presets.minimal.style.border") != "hidden" {
140+
t.Errorf("presets.minimal.style.border = %v, want %v", viper.GetString("presets.minimal.style.border"), "hidden")
117141
}
118-
if v.GetString("presets.minimal.style.theme") != "notty" {
119-
t.Errorf("presets.minimal.style.theme = %v, want %v", v.GetString("presets.minimal.style.theme"), "notty")
142+
if viper.GetString("presets.minimal.style.theme") != "notty" {
143+
t.Errorf("presets.minimal.style.theme = %v, want %v", viper.GetString("presets.minimal.style.theme"), "notty")
120144
}
121-
if v.GetString("presets.dark.style.border") != "rounded" {
122-
t.Errorf("presets.dark.style.border = %v, want %v", v.GetString("presets.dark.style.border"), "rounded")
145+
if viper.GetString("presets.dark.style.border") != "rounded" {
146+
t.Errorf("presets.dark.style.border = %v, want %v", viper.GetString("presets.dark.style.border"), "rounded")
123147
}
124-
if v.GetString("presets.dark.style.theme") != "dracula" {
125-
t.Errorf("presets.dark.style.theme = %v, want %v", v.GetString("presets.dark.style.theme"), "dracula")
148+
if viper.GetString("presets.dark.style.theme") != "dracula" {
149+
t.Errorf("presets.dark.style.theme = %v, want %v", viper.GetString("presets.dark.style.theme"), "dracula")
126150
}
127151
}
128152
}
@@ -147,35 +171,66 @@ func TestCreateDefaultConfig(t *testing.T) {
147171
t.Errorf("Default config file was not created")
148172
}
149173

150-
v := viper.New()
151-
v.SetConfigFile(configFile)
152-
if err := v.ReadInConfig(); err != nil {
174+
viper.SetConfigFile(configFile)
175+
if err := viper.ReadInConfig(); err != nil {
153176
t.Fatalf("Failed to read default config: %v", err)
154177
}
155178

156-
if v.GetString("global.style.border") != "rounded" {
157-
t.Errorf("global.style.border = %v, want %v", v.GetString("global.style.border"), "rounded")
158-
}
159-
if v.GetString("global.style.border_color") != "#9999CC" {
160-
t.Errorf("global.style.border_color = %v, want %v", v.GetString("global.style.border_color"), "#9999CC")
161-
}
162-
if v.GetString("global.style.layout") != "center" {
163-
t.Errorf("global.style.layout = %v, want %v", v.GetString("global.style.layout"), "center")
164-
}
165-
if v.GetString("global.style.theme") != "dracula" {
166-
t.Errorf("global.style.theme = %v, want %v", v.GetString("global.style.theme"), "dracula")
167-
}
168-
169-
if v.GetString("presets.minimal.style.border") != "hidden" {
170-
t.Errorf("presets.minimal.style.border = %v, want %v", v.GetString("presets.minimal.style.border"), "hidden")
171-
}
172-
if v.GetString("presets.minimal.style.theme") != "notty" {
173-
t.Errorf("presets.minimal.style.theme = %v, want %v", v.GetString("presets.minimal.style.theme"), "notty")
174-
}
175-
if v.GetString("presets.dark.style.border") != "rounded" {
176-
t.Errorf("presets.dark.style.border = %v, want %v", v.GetString("presets.dark.style.border"), "rounded")
177-
}
178-
if v.GetString("presets.dark.style.theme") != "dracula" {
179-
t.Errorf("presets.dark.style.theme = %v, want %v", v.GetString("presets.dark.style.theme"), "dracula")
179+
if viper.GetString("global.style.border") != "rounded" {
180+
t.Errorf(
181+
"global.style.border = %v, want %v",
182+
viper.GetString("global.style.border"),
183+
"rounded",
184+
)
185+
}
186+
if viper.GetString("global.style.border_color") != "#9999CC" {
187+
t.Errorf(
188+
"global.style.border_color = %v, want %v",
189+
viper.GetString("global.style.border_color"),
190+
"#9999CC",
191+
)
192+
}
193+
if viper.GetString("global.style.layout") != "center" {
194+
t.Errorf(
195+
"global.style.layout = %v, want %v",
196+
viper.GetString("global.style.layout"),
197+
"center",
198+
)
199+
}
200+
if viper.GetString("global.style.theme") != "dracula" {
201+
t.Errorf(
202+
"global.style.theme = %v, want %v",
203+
viper.GetString("global.style.theme"),
204+
"dracula",
205+
)
206+
}
207+
208+
if viper.GetString("presets.minimal.style.border") != "hidden" {
209+
t.Errorf(
210+
"presets.minimal.style.border = %v, want %v",
211+
viper.GetString("presets.minimal.style.border"),
212+
"hidden",
213+
)
214+
}
215+
if viper.GetString("presets.minimal.style.theme") != "notty" {
216+
t.Errorf(
217+
"presets.minimal.style.theme = %v, want %v",
218+
viper.GetString("presets.minimal.style.theme"),
219+
"notty",
220+
)
221+
}
222+
if viper.GetString("presets.dark.style.border") != "rounded" {
223+
t.Errorf(
224+
"presets.dark.style.border = %v, want %v",
225+
viper.GetString("presets.dark.style.border"),
226+
"rounded",
227+
)
228+
}
229+
if viper.GetString("presets.dark.style.theme") != "dracula" {
230+
t.Errorf(
231+
"presets.dark.style.theme = %v, want %v",
232+
viper.GetString("presets.dark.style.theme"),
233+
"dracula",
234+
)
180235
}
181236
}

0 commit comments

Comments
 (0)