4
4
"fmt"
5
5
"os"
6
6
"path/filepath"
7
+ "reflect"
7
8
9
+ "github.com/go-viper/mapstructure/v2"
8
10
"github.com/spf13/viper"
9
11
10
12
"github.com/museslabs/kyma/internal/tui/transitions"
@@ -27,6 +29,34 @@ type presetConfig struct {
27
29
Transition transitions.Transition `mapstructure:"transition"`
28
30
}
29
31
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
+
30
60
func Load (configPath string ) error {
31
61
viper .SetConfigName (configName )
32
62
viper .SetConfigType (configType )
@@ -57,6 +87,22 @@ func Load(configPath string) error {
57
87
return fmt .Errorf ("failed to read config: %w" , err )
58
88
}
59
89
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
+
60
106
return nil
61
107
}
62
108
0 commit comments