@@ -22,6 +22,7 @@ import (
2222
2323 "github.com/arduino/arduino-cloud-cli/arduino"
2424 "github.com/arduino/go-paths-helper"
25+ "github.com/sirupsen/logrus"
2526 "github.com/spf13/viper"
2627)
2728
@@ -73,15 +74,18 @@ func (c *Config) IsEmpty() bool {
7374// Returns error if no config is found.
7475func Retrieve () (* Config , error ) {
7576 // Config extracted from environment has highest priority
77+ logrus .Info ("Looking for configuration in environment variables" )
7678 c , err := fromEnv ()
7779 if err != nil {
7880 return nil , fmt .Errorf ("reading config from environment variables: %w" , err )
7981 }
8082 // Return the config only if it has been found
8183 if c != nil {
84+ logrus .Info ("Configuration found in environment variables" )
8285 return c , nil
8386 }
8487
88+ logrus .Info ("Looking for configuration in file system" )
8589 c , err = fromFile ()
8690 if err != nil {
8791 return nil , fmt .Errorf ("reading config from file: %w" , err )
@@ -185,7 +189,9 @@ func searchConfigDir() (*string, error) {
185189 // Don't let bad naming mislead you, cwd.Parents()[0] is cwd itself so
186190 // we look in the current directory first and then on its parents.
187191 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 )
189195 p := path .String ()
190196 return & p , nil
191197 }
@@ -196,11 +202,26 @@ func searchConfigDir() (*string, error) {
196202 if err != nil {
197203 return nil , err
198204 }
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 )
200208 p := arduino15 .String ()
201209 return & p , nil
202210 }
203211
204212 // Didn't find config file in the current directory, its parents or in arduino15"
205213 return nil , nil
206214}
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