Skip to content

Commit f526b36

Browse files
authored
Merge pull request #140 from arduino/config
Change the directory of the configuration files
2 parents 494bc94 + fc64888 commit f526b36

File tree

29 files changed

+2595
-52
lines changed

29 files changed

+2595
-52
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
/main
44
/.vscode/settings.json
55
/cmd/formatter/debug.test
6-
/.cli-config.yml
6+
/arduino-cli.yaml
77
/wiki

Gopkg.lock

+11-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Great! Now we have the Board FQBN (Fully Qualified Board Name) `arduino:samd:mkr
162162
and the Board Name look good, we are ready to compile and upload the sketch
163163

164164
#### Adding 3rd party cores
165-
To add 3rd party core packages add a link of the additional package to the file `.cli-config.yml`
165+
To add 3rd party core packages add a link of the additional package to the file `arduino-cli.yaml`
166166

167167
If you want to add the ESP8266 core, for example:
168168

@@ -295,7 +295,7 @@ Flags:
295295
-h, --help help for core
296296

297297
Global Flags:
298-
--config-file string The custom config file (if not specified ./.cli-config.yml will be used). (default "/home/megabug/Workspace/go/src/github.com/arduino/arduino-cli/.cli-config.yml")
298+
--config-file string The custom config file (if not specified the default one will be used). (example "/home/megabug/.config/arduino/arduino-cli/arduino-cli.yaml")
299299
--debug Enables debug output (super verbose, used to debug the CLI).
300300
--format string The output format, can be [text|json]. (default "text")
301301

commands/commands_test.go

+37-22
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ func executeWithArgs(t *testing.T, args ...string) (int, []byte) {
7474

7575
// This closure is here because we won't that the defer are executed after the end of the "executeWithArgs" method
7676
func() {
77+
// Create an empty config for the CLI test
78+
conf := paths.New("arduino-cli.yaml")
79+
require.False(t, conf.Exist())
80+
err := conf.WriteFile([]byte("board_manager:\n additional_urls:\n"))
81+
require.NoError(t, err)
82+
defer func() {
83+
require.NoError(t, conf.Remove())
84+
}()
85+
7786
redirect := &stdOutRedirect{}
7887
redirect.Open(t)
7988
defer func() {
@@ -512,27 +521,33 @@ func TestCompileCommands(t *testing.T) {
512521
require.True(t, paths.New(test1).Join("Test1.arduino.avr.nano.hex").Exist())
513522

514523
// Build sketch with --output path
515-
require.NoError(t, os.Chdir(tmp))
516-
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test", test1)
517-
require.Zero(t, exitCode, "exit code")
518-
require.Contains(t, string(d), "Sketch uses")
519-
require.True(t, paths.New("test.hex").Exist())
520-
521-
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test2.hex", test1)
522-
require.Zero(t, exitCode, "exit code")
523-
require.Contains(t, string(d), "Sketch uses")
524-
require.True(t, paths.New("test2.hex").Exist())
525-
require.NoError(t, paths.New(tmp, "anothertest").MkdirAll())
526-
527-
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "anothertest/test", test1)
528-
require.Zero(t, exitCode, "exit code")
529-
require.Contains(t, string(d), "Sketch uses")
530-
require.True(t, paths.New("anothertest", "test.hex").Exist())
531-
532-
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", tmp+"/anothertest/test2", test1)
533-
require.Zero(t, exitCode, "exit code")
534-
require.Contains(t, string(d), "Sketch uses")
535-
require.True(t, paths.New("anothertest", "test2.hex").Exist())
524+
{
525+
pwd, err := os.Getwd()
526+
require.NoError(t, err)
527+
defer func() { require.NoError(t, os.Chdir(pwd)) }()
528+
require.NoError(t, os.Chdir(tmp))
529+
530+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test", test1)
531+
require.Zero(t, exitCode, "exit code")
532+
require.Contains(t, string(d), "Sketch uses")
533+
require.True(t, paths.New("test.hex").Exist())
534+
535+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test2.hex", test1)
536+
require.Zero(t, exitCode, "exit code")
537+
require.Contains(t, string(d), "Sketch uses")
538+
require.True(t, paths.New("test2.hex").Exist())
539+
require.NoError(t, paths.New(tmp, "anothertest").MkdirAll())
540+
541+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "anothertest/test", test1)
542+
require.Zero(t, exitCode, "exit code")
543+
require.Contains(t, string(d), "Sketch uses")
544+
require.True(t, paths.New("anothertest", "test.hex").Exist())
545+
546+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", tmp+"/anothertest/test2", test1)
547+
require.Zero(t, exitCode, "exit code")
548+
require.Contains(t, string(d), "Sketch uses")
549+
require.True(t, paths.New("anothertest", "test2.hex").Exist())
550+
}
536551
}
537552

538553
func TestInvalidCoreURL(t *testing.T) {
@@ -544,7 +559,7 @@ func TestInvalidCoreURL(t *testing.T) {
544559
require.NoError(t, err, "making temporary dir")
545560
defer tmp.RemoveAll()
546561

547-
configFile := tmp.Join("cli-config.yml")
562+
configFile := tmp.Join("arduino-cli.yaml")
548563
configFile.WriteFile([]byte(`
549564
board_manager:
550565
additional_urls:

commands/config/init.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func initInitCommand() *cobra.Command {
4242
initCommand.Flags().BoolVar(&initFlags._default, "default", false,
4343
"If omitted, ask questions to the user about setting configuration properties, otherwise use default configuration.")
4444
initCommand.Flags().StringVar(&initFlags.location, "save-as", "",
45-
"Sets where to save the configuration file [default is ./.cli-config.yml].")
45+
"Sets where to save the configuration file [default is ./arduino-cli.yaml].")
4646
return initCommand
4747
}
4848

@@ -65,7 +65,14 @@ func runInitCommand(cmd *cobra.Command, args []string) {
6565
if filepath == "" {
6666
filepath = commands.Config.ConfigFile.String()
6767
}
68-
err := commands.Config.SaveToYAML(filepath)
68+
69+
err := commands.Config.ConfigFile.Parent().MkdirAll()
70+
if err != nil {
71+
formatter.PrintError(err, "Cannot create config file.")
72+
os.Exit(commands.ErrGeneric)
73+
}
74+
75+
err = commands.Config.SaveToYAML(filepath)
6976
if err != nil {
7077
formatter.PrintError(err, "Cannot create config file.")
7178
os.Exit(commands.ErrGeneric)

commands/root/root.go

+44-9
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
package root
1919

2020
import (
21+
"fmt"
2122
"io/ioutil"
2223
"os"
2324

2425
"github.com/arduino/arduino-cli/output"
2526

2627
"golang.org/x/crypto/ssh/terminal"
2728

28-
"github.com/mattn/go-colorable"
29+
colorable "github.com/mattn/go-colorable"
2930

30-
"github.com/arduino/go-paths-helper"
31+
paths "github.com/arduino/go-paths-helper"
3132

3233
"github.com/arduino/arduino-cli/commands"
3334
"github.com/arduino/arduino-cli/commands/board"
@@ -56,7 +57,7 @@ func Init() *cobra.Command {
5657
}
5758
command.PersistentFlags().BoolVar(&commands.GlobalFlags.Debug, "debug", false, "Enables debug output (super verbose, used to debug the CLI).")
5859
command.PersistentFlags().StringVar(&commands.GlobalFlags.Format, "format", "text", "The output format, can be [text|json].")
59-
command.PersistentFlags().StringVar(&yamlConfigFile, "config-file", "", "The custom config file (if not specified ./.cli-config.yml will be used).")
60+
command.PersistentFlags().StringVar(&yamlConfigFile, "config-file", "", "The custom config file (if not specified the default will be used).")
6061
command.AddCommand(board.InitCommand())
6162
command.AddCommand(compile.InitCommand())
6263
command.AddCommand(config.InitCommand())
@@ -115,6 +116,17 @@ func preRun(cmd *cobra.Command, args []string) {
115116

116117
// initConfigs initializes the configuration from the specified file.
117118
func initConfigs() {
119+
// Return error if an old configuration file is found
120+
if old := paths.New(".cli-config.yml"); old.Exist() {
121+
logrus.Errorf("Old configuration file detected: %s.", old)
122+
logrus.Info("The name of this file has been changed to `arduino-cli.yaml`, please rename the file fix it.")
123+
formatter.PrintError(
124+
fmt.Errorf("old configuration file detected: %s", old),
125+
"The name of this file has been changed to `arduino-cli.yaml`, please rename the file fix it.")
126+
os.Exit(commands.ErrGeneric)
127+
}
128+
129+
// Start with default configuration
118130
if conf, err := configs.NewConfiguration(); err != nil {
119131
logrus.WithError(err).Error("Error creating default configuration")
120132
formatter.PrintError(err, "Error creating default configuration")
@@ -123,14 +135,11 @@ func initConfigs() {
123135
commands.Config = conf
124136
}
125137

126-
if yamlConfigFile != "" {
127-
commands.Config.ConfigFile = paths.New(yamlConfigFile)
138+
// Read configuration from global config file
139+
if commands.Config.ConfigFile.Exist() {
140+
readConfigFrom(commands.Config.ConfigFile)
128141
}
129142

130-
logrus.Info("Initiating configuration")
131-
if err := commands.Config.LoadFromYAML(commands.Config.ConfigFile); err != nil {
132-
logrus.WithError(err).Warn("Did not manage to get config file, using default configuration")
133-
}
134143
if commands.Config.IsBundledInDesktopIDE() {
135144
logrus.Info("CLI is bundled into the IDE")
136145
err := commands.Config.LoadFromDesktopIDEPreferences()
@@ -140,6 +149,32 @@ func initConfigs() {
140149
} else {
141150
logrus.Info("CLI is not bundled into the IDE")
142151
}
152+
153+
// Read configuration from parent folders (project config)
154+
if pwd, err := paths.Getwd(); err != nil {
155+
logrus.WithError(err).Warn("Did not manage to find current path")
156+
if path := paths.New("arduino-cli.yaml"); path.Exist() {
157+
readConfigFrom(path)
158+
}
159+
} else {
160+
commands.Config.Navigate("/", pwd.String())
161+
}
162+
163+
// Read configuration from environment vars
143164
commands.Config.LoadFromEnv()
165+
166+
// Read configuration from user specified file
167+
if yamlConfigFile != "" {
168+
commands.Config.ConfigFile = paths.New(yamlConfigFile)
169+
readConfigFrom(commands.Config.ConfigFile)
170+
}
171+
144172
logrus.Info("Configuration set")
145173
}
174+
175+
func readConfigFrom(path *paths.Path) {
176+
logrus.Infof("Reading configuration from %s", path)
177+
if err := commands.Config.LoadFromYAML(path); err != nil {
178+
logrus.WithError(err).Warnf("Could not read configuration from %s", path)
179+
}
180+
}

configs/directories.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,37 @@ package configs
1919

2020
import (
2121
"fmt"
22-
"os"
2322
"os/user"
2423
"runtime"
2524

2625
"github.com/arduino/go-paths-helper"
27-
2826
"github.com/arduino/go-win32-utils"
2927
)
3028

31-
// getDefaultConfigFilePath returns the default path for .cli-config.yml,
32-
// this is the directory where the arduino-cli executable resides.
29+
// getDefaultConfigFilePath returns the default path for arduino-cli.yaml
3330
func getDefaultConfigFilePath() *paths.Path {
34-
executablePath, err := os.Executable()
31+
usr, err := user.Current()
3532
if err != nil {
36-
executablePath = "."
33+
panic(fmt.Errorf("retrieving user home dir: %s", err))
3734
}
38-
return paths.New(executablePath).Parent().Join(".cli-config.yml")
35+
arduinoDataDir := paths.New(usr.HomeDir)
36+
37+
switch runtime.GOOS {
38+
case "linux":
39+
arduinoDataDir = arduinoDataDir.Join(".arduino15")
40+
case "darwin":
41+
arduinoDataDir = arduinoDataDir.Join("Library", "arduino15")
42+
case "windows":
43+
localAppDataPath, err := win32.GetLocalAppDataFolder()
44+
if err != nil {
45+
panic(err)
46+
}
47+
arduinoDataDir = paths.New(localAppDataPath).Join("Arduino15")
48+
default:
49+
panic(fmt.Errorf("unsupported OS: %s", runtime.GOOS))
50+
}
51+
52+
return arduinoDataDir.Join("arduino-cli.yaml")
3953
}
4054

4155
func getDefaultArduinoDataDir() (*paths.Path, error) {

configs/navigate.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to [email protected].
16+
*/
17+
18+
package configs
19+
20+
import (
21+
"path/filepath"
22+
"strings"
23+
24+
paths "github.com/arduino/go-paths-helper"
25+
)
26+
27+
func (c *Configuration) Navigate(root, pwd string) {
28+
relativePath, err := filepath.Rel(root, pwd)
29+
if err != nil {
30+
return
31+
}
32+
33+
// From the root to the current folder, search for arduino-cli.yaml files
34+
parts := strings.Split(relativePath, string(filepath.Separator))
35+
for i := range parts {
36+
path := paths.New(root)
37+
path = path.Join(parts[:i+1]...)
38+
path = path.Join("arduino-cli.yaml")
39+
_ = c.LoadFromYAML(path)
40+
}
41+
}

0 commit comments

Comments
 (0)