@@ -5,12 +5,13 @@ import (
55 "os"
66
77 "encoding/json"
8- "errors"
9- "github.com/google/uuid"
10- "github.com/spf13/viper"
118 "io"
129 "path/filepath"
1310 "reflect"
11+
12+ "github.com/go-viper/encoding/hcl"
13+ "github.com/google/uuid"
14+ "github.com/spf13/viper"
1415)
1516
1617const (
@@ -38,12 +39,18 @@ func NewLocalCloudEnv() CloudEnv {
3839}
3940func NewLocalCloudEnvFromReader (r io.Reader , configType string ) CloudEnv {
4041 cloudEnv := & LocalCloudEnv {}
41- viper .SetConfigType (configType )
42- err := viper .ReadConfig (r )
42+ codecRegistry := viper .NewCodecRegistry ()
43+ err := codecRegistry .RegisterCodec ("hcl" , hcl.Codec {})
44+ if err != nil {
45+ panic (fmt .Errorf ("fatal error on registering codec: %s" , err ))
46+ }
47+ v := viper .NewWithOptions (viper .WithCodecRegistry (codecRegistry ))
48+ v .SetConfigType (configType )
49+ err = v .ReadConfig (r )
4350 if err != nil {
44- panic (fmt .Errorf ("Fatal error on reading cloud file: %s \n " , err ))
51+ panic (fmt .Errorf ("fatal error on reading cloud file: %s" , err ))
4552 }
46- cloudEnv .loadServices (viper .Get (SERVICES_CONFIG_KEY ))
53+ cloudEnv .loadServices (v .Get (SERVICES_CONFIG_KEY ))
4754 cloudEnv .loadAppName ()
4855 return cloudEnv
4956}
@@ -73,16 +80,22 @@ func (c *LocalCloudEnv) loadConfigFile() error {
7380 })
7481 return nil
7582 }
76- viper .SetConfigType (filepath .Ext (confPath )[1 :])
77- viper .SetConfigFile (confPath )
78- err = viper .ReadInConfig ()
83+ codecRegistry := viper .NewCodecRegistry ()
84+ err = codecRegistry .RegisterCodec ("hcl" , hcl.Codec {})
7985 if err != nil {
80- return errors .New (fmt .Sprintf ("Fatal error on reading config file: %s \n " , err .Error ()))
86+ panic (fmt .Errorf ("fatal error on registering codec: %s" , err ))
87+ }
88+ v := viper .NewWithOptions (viper .WithCodecRegistry (codecRegistry ))
89+ v .SetConfigType (filepath .Ext (confPath )[1 :])
90+ v .SetConfigFile (confPath )
91+ err = v .ReadInConfig ()
92+ if err != nil {
93+ return fmt .Errorf ("fatal error on reading config file: %s" , err .Error ())
8194 }
8295 var creds map [interface {}]interface {}
83- err = viper .Unmarshal (& creds )
96+ err = v .Unmarshal (& creds )
8497 if err != nil {
85- return errors . New ( fmt .Sprintf ( "Fatal error when unmarshaling config file: %s \n " , err .Error () ))
98+ return fmt .Errorf ( "fatal error when unmarshaling config file: %s" , err .Error ())
8699 }
87100 finalCreds := c .convertMapInterface (creds ).(map [string ]interface {})
88101 c .servicesLocal = append (c .servicesLocal , ServiceLocal {
@@ -93,15 +106,21 @@ func (c *LocalCloudEnv) loadConfigFile() error {
93106 return nil
94107}
95108func (c * LocalCloudEnv ) loadCloudFile () error {
96- viper .SetConfigType (filepath .Ext (os .Getenv (LOCAL_ENV_KEY ))[1 :])
97- viper .SetConfigFile (os .Getenv (LOCAL_ENV_KEY ))
98- err := viper .ReadInConfig ()
109+ codecRegistry := viper .NewCodecRegistry ()
110+ err := codecRegistry .RegisterCodec ("hcl" , hcl.Codec {})
111+ if err != nil {
112+ panic (fmt .Errorf ("fatal error on registering codec: %s" , err ))
113+ }
114+ v := viper .NewWithOptions (viper .WithCodecRegistry (codecRegistry ))
115+ v .SetConfigType (filepath .Ext (os .Getenv (LOCAL_ENV_KEY ))[1 :])
116+ v .SetConfigFile (os .Getenv (LOCAL_ENV_KEY ))
117+ err = v .ReadInConfig ()
99118 if err != nil {
100- return errors . New ( fmt .Sprintf ( "Fatal error on reading cloud file: %s \n " , err .Error () ))
119+ return fmt .Errorf ( "fatal error on reading cloud file: %s" , err .Error ())
101120 }
102- services := viper .Get (SERVICES_CONFIG_KEY )
121+ services := v .Get (SERVICES_CONFIG_KEY )
103122 if services != nil {
104- c .loadServices (viper .Get (SERVICES_CONFIG_KEY ))
123+ c .loadServices (v .Get (SERVICES_CONFIG_KEY ))
105124 } else {
106125 c .servicesLocal = make ([]ServiceLocal , 0 )
107126 }
@@ -110,7 +129,13 @@ func (c *LocalCloudEnv) loadCloudFile() error {
110129
111130func (c * LocalCloudEnv ) loadAppName () {
112131 c .appName = "<unknown>"
113- appName := viper .Get ("app_name" )
132+ codecRegistry := viper .NewCodecRegistry ()
133+ err := codecRegistry .RegisterCodec ("hcl" , hcl.Codec {})
134+ if err != nil {
135+ panic (fmt .Errorf ("fatal error on registering codec: %s" , err ))
136+ }
137+ v := viper .NewWithOptions (viper .WithCodecRegistry (codecRegistry ))
138+ appName := v .Get ("app_name" )
114139 if appName != nil {
115140 c .appName = appName .(string )
116141 }
@@ -207,12 +232,12 @@ func (c *LocalCloudEnv) loadServices(v interface{}) {
207232 }
208233 b , err := json .Marshal (dataFinal )
209234 if err != nil {
210- panic (fmt .Errorf ("Fatal error during loading cloud file: %s \n " , err ))
235+ panic (fmt .Errorf ("fatal error during loading cloud file: %s" , err ))
211236 }
212237 var services []ServiceLocal
213238 err = json .Unmarshal (b , & services )
214239 if err != nil {
215- panic (fmt .Errorf ("Fatal error during loading cloud file: %s \n " , err ))
240+ panic (fmt .Errorf ("fatal error during loading cloud file: %s" , err ))
216241 }
217242 c .servicesLocal = services
218243}
0 commit comments