@@ -22,6 +22,7 @@ import (
22
22
23
23
"github.com/arduino/arduino-cloud-cli/arduino"
24
24
"github.com/arduino/go-paths-helper"
25
+ "github.com/sirupsen/logrus"
25
26
"github.com/spf13/viper"
26
27
)
27
28
@@ -73,15 +74,18 @@ func (c *Config) IsEmpty() bool {
73
74
// Returns error if no config is found.
74
75
func Retrieve () (* Config , error ) {
75
76
// Config extracted from environment has highest priority
77
+ logrus .Info ("Looking for configuration in environment variables" )
76
78
c , err := fromEnv ()
77
79
if err != nil {
78
80
return nil , fmt .Errorf ("reading config from environment variables: %w" , err )
79
81
}
80
82
// Return the config only if it has been found
81
83
if c != nil {
84
+ logrus .Info ("Configuration found in environment variables" )
82
85
return c , nil
83
86
}
84
87
88
+ logrus .Info ("Looking for configuration in file system" )
85
89
c , err = fromFile ()
86
90
if err != nil {
87
91
return nil , fmt .Errorf ("reading config from file: %w" , err )
@@ -185,7 +189,9 @@ func searchConfigDir() (*string, error) {
185
189
// Don't let bad naming mislead you, cwd.Parents()[0] is cwd itself so
186
190
// we look in the current directory first and then on its parents.
187
191
for _ , path := range cwd .Parents () {
188
- if path .Join (Filename + ".yaml" ).Exist () || path .Join (Filename + ".json" ).Exist () {
192
+ logrus .Infof ("Looking for configuration in %s" , path )
193
+ if file , found := configFileInDir (path ); found {
194
+ logrus .Infof ("Configuration found at %s" , file )
189
195
p := path .String ()
190
196
return & p , nil
191
197
}
@@ -196,11 +202,26 @@ func searchConfigDir() (*string, error) {
196
202
if err != nil {
197
203
return nil , err
198
204
}
199
- if arduino15 .Join (Filename + ".yaml" ).Exist () || arduino15 .Join (Filename + ".json" ).Exist () {
205
+ logrus .Infof ("Looking for configuration in %s" , arduino15 )
206
+ if file , found := configFileInDir (arduino15 ); found {
207
+ logrus .Infof ("Configuration found at %s" , file )
200
208
p := arduino15 .String ()
201
209
return & p , nil
202
210
}
203
211
204
212
// Didn't find config file in the current directory, its parents or in arduino15"
205
213
return nil , nil
206
214
}
215
+
216
+ // configFileInDir looks for a configuration file in the passed directory.
217
+ // If a configuration file is found, then it is returned.
218
+ // In case of multiple config files, it returns the one with the highest priority
219
+ // according to viper.
220
+ func configFileInDir (dir * paths.Path ) (filepath * paths.Path , found bool ) {
221
+ for _ , ext := range viper .SupportedExts {
222
+ if filepath = dir .Join (Filename + "." + ext ); filepath .Exist () {
223
+ return filepath , true
224
+ }
225
+ }
226
+ return nil , false
227
+ }
0 commit comments