Skip to content

Commit 4af6438

Browse files
ploMP4tharropoulos
authored andcommitted
fix(config): decode config into GlobalConfig variable
1 parent 542c551 commit 4af6438

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

internal/config/config.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"reflect"
78

9+
"github.com/go-viper/mapstructure/v2"
810
"github.com/spf13/viper"
911

1012
"github.com/museslabs/kyma/internal/tui/transitions"
@@ -27,6 +29,34 @@ type presetConfig struct {
2729
Transition transitions.Transition `mapstructure:"transition"`
2830
}
2931

32+
func styleConfigDecodeHook() mapstructure.DecodeHookFunc {
33+
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
34+
if to == reflect.TypeOf(StyleConfig{}) {
35+
m, ok := data.(map[string]any)
36+
if !ok {
37+
return data, nil
38+
}
39+
var s StyleConfig
40+
if err := s.DecodeMap(m); err != nil {
41+
return nil, err
42+
}
43+
return s, nil
44+
}
45+
return data, nil
46+
}
47+
}
48+
49+
func transitionDecodeHook() mapstructure.DecodeHookFunc {
50+
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
51+
if from.Kind() == reflect.String &&
52+
to == reflect.TypeOf((*transitions.Transition)(nil)).Elem() {
53+
name := data.(string)
54+
return transitions.Get(name, transitions.Fps), nil
55+
}
56+
return data, nil
57+
}
58+
}
59+
3060
func Load(configPath string) error {
3161
viper.SetConfigName(configName)
3262
viper.SetConfigType(configType)
@@ -57,6 +87,22 @@ func Load(configPath string) error {
5787
return fmt.Errorf("failed to read config: %w", err)
5888
}
5989

90+
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
91+
DecodeHook: mapstructure.ComposeDecodeHookFunc(
92+
styleConfigDecodeHook(),
93+
transitionDecodeHook(),
94+
),
95+
Result: &GlobalConfig,
96+
TagName: "mapstructure",
97+
})
98+
if err != nil {
99+
return err
100+
}
101+
102+
if err := decoder.Decode(viper.AllSettings()); err != nil {
103+
return err
104+
}
105+
60106
return nil
61107
}
62108

internal/config/style.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/charmbracelet/glamour/ansi"
1010
"github.com/charmbracelet/glamour/styles"
1111
"github.com/charmbracelet/lipgloss"
12+
"github.com/go-viper/mapstructure/v2"
1213
"github.com/goccy/go-yaml"
1314

1415
"github.com/museslabs/kyma/internal/tui/transitions"
@@ -36,6 +37,30 @@ type StyleConfig struct {
3637
Theme GlamourTheme `yaml:"theme"`
3738
}
3839

40+
func (s *StyleConfig) DecodeMap(input map[string]any) error {
41+
aux := struct {
42+
Layout string `mapstructure:"layout"`
43+
Border string `mapstructure:"border"`
44+
BorderColor string `mapstructure:"border_color"`
45+
Theme string `mapstructure:"theme"`
46+
}{}
47+
48+
if err := mapstructure.Decode(input, &aux); err != nil {
49+
return err
50+
}
51+
52+
var err error
53+
s.Layout, err = getLayout(aux.Layout)
54+
if err != nil {
55+
return err
56+
}
57+
s.Border = getBorder(aux.Border)
58+
s.BorderColor = aux.BorderColor
59+
s.Theme = getTheme(aux.Theme)
60+
61+
return nil
62+
}
63+
3964
func (s *StyleConfig) UnmarshalYAML(bytes []byte) error {
4065
aux := struct {
4166
Layout string `yaml:"layout"`

0 commit comments

Comments
 (0)