github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/config/config.go (about)

     1  // This file is part of arduino-cloud-cli.
     2  //
     3  // Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published
     7  // by the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17  
    18  package config
    19  
    20  import (
    21  	"github.com/arduino/arduino-cloud-cli/arduino"
    22  	"github.com/arduino/go-paths-helper"
    23  	"github.com/sirupsen/logrus"
    24  	"github.com/spf13/viper"
    25  )
    26  
    27  // searchConfigFile looks for a configuration file in different directories in the following order:
    28  // current working directory, parents of the current working directory, arduino15 default directory.
    29  // Returns empty string and false if no config file has been found, without raising errors.
    30  // Returns an error if any problem is encountered during the file research which prevents
    31  // to understand whether a config file exists or not.
    32  func searchConfigFile(confname string) (dir string, found bool, err error) {
    33  	// Search in current directory and its parents.
    34  	cwd, err := paths.Getwd()
    35  	if err != nil {
    36  		return "", false, err
    37  	}
    38  	// Don't let bad naming mislead you, cwd.Parents()[0] is cwd itself so
    39  	// we look in the current directory first and then on its parents.
    40  	for _, path := range cwd.Parents() {
    41  		logrus.Infof("Looking for %s in %s", confname, path)
    42  		if file, found := configFileInDir(confname, path); found {
    43  			logrus.Infof("Found %s at %s", confname, file)
    44  			return file.String(), true, nil
    45  		}
    46  	}
    47  
    48  	// Search in arduino's default data directory.
    49  	arduino15, err := arduino.DataDir()
    50  	if err != nil {
    51  		return "", false, err
    52  	}
    53  	logrus.Infof("Looking for %s in %s", confname, arduino15)
    54  	if file, found := configFileInDir(confname, arduino15); found {
    55  		logrus.Infof("%s found at %s", confname, file)
    56  		return file.String(), true, nil
    57  	}
    58  
    59  	// Didn't find config file in the current directory, its parents or in arduino15"
    60  	return "", false, nil
    61  }
    62  
    63  // configFileInDir looks for a configuration file in the passed directory.
    64  // If a configuration file is found, then it is returned.
    65  // In case of multiple config files, it returns the one with the highest priority
    66  // according to viper.
    67  func configFileInDir(confname string, dir *paths.Path) (filepath *paths.Path, found bool) {
    68  	for _, ext := range viper.SupportedExts {
    69  		if filepath = dir.Join(confname + "." + ext); filepath.Exist() {
    70  			return filepath, true
    71  		}
    72  	}
    73  	return nil, false
    74  }