forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigs.go
94 lines (82 loc) · 3.21 KB
/
configs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// This file is part of arduino-cli.
//
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to [email protected].
package globals
import (
"fmt"
"os"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/configs"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
)
// InitConfigs initializes the configuration from the specified file.
func InitConfigs() {
// Start with default configuration
if conf, err := configs.NewConfiguration(); err != nil {
logrus.WithError(err).Error("Error creating default configuration")
feedback.Errorf("Error creating default configuration: %v", err)
os.Exit(errorcodes.ErrGeneric)
} else {
Config = conf
}
// Read configuration from global config file
logrus.Info("Checking for config file in: " + Config.ConfigFile.String())
if Config.ConfigFile.Exist() {
readConfigFrom(Config.ConfigFile)
}
if Config.IsBundledInDesktopIDE() {
logrus.Info("CLI is bundled into the IDE")
err := Config.LoadFromDesktopIDEPreferences()
if err != nil {
logrus.WithError(err).Warn("Did not manage to get config file of IDE, using default configuration")
}
} else {
logrus.Info("CLI is not bundled into the IDE")
}
// Read configuration from parent folders (project config)
if pwd, err := paths.Getwd(); err != nil {
logrus.WithError(err).Warn("Did not manage to find current path")
if path := paths.New("arduino-yaml"); path.Exist() {
readConfigFrom(path)
}
} else {
Config.Navigate(pwd)
}
// Read configuration from old configuration file if found, but output a warning.
if old := paths.New(".cli-config.yml"); old.Exist() {
logrus.Errorf("Old configuration file detected: %s.", old)
logrus.Info("The name of this file has been changed to `arduino-cli.yaml`, please rename the file fix it.")
feedback.Error(
fmt.Errorf("WARNING: Old configuration file detected: %s", old),
"The name of this file has been changed to `arduino-yaml`, in a future release we will not support"+
"the old name `.cli-config.yml` anymore. Please rename the file to `arduino-cli.yaml` to silence this warning.")
readConfigFrom(old)
}
// Read configuration from environment vars
Config.LoadFromEnv()
// Read configuration from user specified file
if YAMLConfigFile != "" {
Config.ConfigFile = paths.New(YAMLConfigFile)
readConfigFrom(Config.ConfigFile)
}
logrus.Info("Configuration set")
}
func readConfigFrom(path *paths.Path) {
logrus.Infof("Reading configuration from %s", path)
if err := Config.LoadFromYAML(path); err != nil {
logrus.WithError(err).Warnf("Could not read configuration from %s", path)
}
}