Skip to content

Change the directory of the configuration files #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7112bc2
Change the directory of the configuration files
matteosuppo Jan 31, 2019
f5d7eae
Stub new function configs.Navigate
matteosuppo Feb 1, 2019
4a73a8e
Navigate returns the default configuration
matteosuppo Feb 11, 2019
74f9f6a
Search for config file in local directory
matteosuppo Feb 11, 2019
f785e1c
Inherit configuration from parent folders
matteosuppo Feb 11, 2019
4712662
Make Navigate a method of Configuration
matteosuppo Feb 11, 2019
da73a11
Use config.Navigate in command root
matteosuppo Feb 11, 2019
5e22ab2
Forgot deps
matteosuppo Feb 11, 2019
8f0150b
Fix bugs in tests
matteosuppo Feb 11, 2019
5f9cff0
Read default config file (in .arduino15)
matteosuppo Feb 13, 2019
8659dd2
Rename configuration file
matteosuppo Feb 13, 2019
f9bf3a8
Remove configdir
matteosuppo Feb 13, 2019
392f7cf
Consistent usage of paths helper
matteosuppo Feb 13, 2019
b2e6f47
Add license where missing
matteosuppo Feb 13, 2019
11be6bd
Don't show the default package index
matteosuppo Feb 13, 2019
930b89a
Make linter happy
matteosuppo Feb 13, 2019
2453397
Fix navigate tests
matteosuppo Feb 13, 2019
0173160
Return an error message when there's a old configuration file
matteosuppo Feb 13, 2019
4705594
Fix path of default config
matteosuppo Feb 13, 2019
6145d74
Correct order of preferences loading. Added more logging.
cmaglie Feb 14, 2019
3d25175
Updated go-paths-helper
cmaglie Feb 14, 2019
5c48c84
Correctly handle error when looking for configuration on parent folders
cmaglie Feb 14, 2019
66ce6f8
configs: Correctly override board manager additional urls
cmaglie Feb 14, 2019
de5ac23
UploadsTests: go back to previous working directory after test
cmaglie Feb 14, 2019
ebb0de9
Refactored duplicated code
cmaglie Feb 14, 2019
fc64888
configs: if cwd could not be determined try to open 'arduino-cli.yaml…
cmaglie Feb 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
/main
/.vscode/settings.json
/cmd/formatter/debug.test
/.cli-config.yml
/arduino-cli.yaml
/wiki
13 changes: 11 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Great! Now we have the Board FQBN (Fully Qualified Board Name) `arduino:samd:mkr
and the Board Name look good, we are ready to compile and upload the sketch

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

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

Expand Down Expand Up @@ -295,7 +295,7 @@ Flags:
-h, --help help for core

Global Flags:
--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")
--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")
--debug Enables debug output (super verbose, used to debug the CLI).
--format string The output format, can be [text|json]. (default "text")

Expand Down
59 changes: 37 additions & 22 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ func executeWithArgs(t *testing.T, args ...string) (int, []byte) {

// This closure is here because we won't that the defer are executed after the end of the "executeWithArgs" method
func() {
// Create an empty config for the CLI test
conf := paths.New("arduino-cli.yaml")
require.False(t, conf.Exist())
err := conf.WriteFile([]byte("board_manager:\n additional_urls:\n"))
require.NoError(t, err)
defer func() {
require.NoError(t, conf.Remove())
}()

redirect := &stdOutRedirect{}
redirect.Open(t)
defer func() {
Expand Down Expand Up @@ -512,27 +521,33 @@ func TestCompileCommands(t *testing.T) {
require.True(t, paths.New(test1).Join("Test1.arduino.avr.nano.hex").Exist())

// Build sketch with --output path
require.NoError(t, os.Chdir(tmp))
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test", test1)
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), "Sketch uses")
require.True(t, paths.New("test.hex").Exist())

exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test2.hex", test1)
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), "Sketch uses")
require.True(t, paths.New("test2.hex").Exist())
require.NoError(t, paths.New(tmp, "anothertest").MkdirAll())

exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "anothertest/test", test1)
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), "Sketch uses")
require.True(t, paths.New("anothertest", "test.hex").Exist())

exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", tmp+"/anothertest/test2", test1)
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), "Sketch uses")
require.True(t, paths.New("anothertest", "test2.hex").Exist())
{
pwd, err := os.Getwd()
require.NoError(t, err)
defer func() { require.NoError(t, os.Chdir(pwd)) }()
require.NoError(t, os.Chdir(tmp))

exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test", test1)
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), "Sketch uses")
require.True(t, paths.New("test.hex").Exist())

exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test2.hex", test1)
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), "Sketch uses")
require.True(t, paths.New("test2.hex").Exist())
require.NoError(t, paths.New(tmp, "anothertest").MkdirAll())

exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "anothertest/test", test1)
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), "Sketch uses")
require.True(t, paths.New("anothertest", "test.hex").Exist())

exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", tmp+"/anothertest/test2", test1)
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), "Sketch uses")
require.True(t, paths.New("anothertest", "test2.hex").Exist())
}
}

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

configFile := tmp.Join("cli-config.yml")
configFile := tmp.Join("arduino-cli.yaml")
configFile.WriteFile([]byte(`
board_manager:
additional_urls:
Expand Down
11 changes: 9 additions & 2 deletions commands/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func initInitCommand() *cobra.Command {
initCommand.Flags().BoolVar(&initFlags._default, "default", false,
"If omitted, ask questions to the user about setting configuration properties, otherwise use default configuration.")
initCommand.Flags().StringVar(&initFlags.location, "save-as", "",
"Sets where to save the configuration file [default is ./.cli-config.yml].")
"Sets where to save the configuration file [default is ./arduino-cli.yaml].")
return initCommand
}

Expand All @@ -65,7 +65,14 @@ func runInitCommand(cmd *cobra.Command, args []string) {
if filepath == "" {
filepath = commands.Config.ConfigFile.String()
}
err := commands.Config.SaveToYAML(filepath)

err := commands.Config.ConfigFile.Parent().MkdirAll()
if err != nil {
formatter.PrintError(err, "Cannot create config file.")
os.Exit(commands.ErrGeneric)
}

err = commands.Config.SaveToYAML(filepath)
if err != nil {
formatter.PrintError(err, "Cannot create config file.")
os.Exit(commands.ErrGeneric)
Expand Down
53 changes: 44 additions & 9 deletions commands/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
package root

import (
"fmt"
"io/ioutil"
"os"

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

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

"github.com/mattn/go-colorable"
colorable "github.com/mattn/go-colorable"

"github.com/arduino/go-paths-helper"
paths "github.com/arduino/go-paths-helper"

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

// initConfigs initializes the configuration from the specified file.
func initConfigs() {
// Return error if an old configuration file is found
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.")
formatter.PrintError(
fmt.Errorf("old configuration file detected: %s", old),
"The name of this file has been changed to `arduino-cli.yaml`, please rename the file fix it.")
os.Exit(commands.ErrGeneric)
}

// Start with default configuration
if conf, err := configs.NewConfiguration(); err != nil {
logrus.WithError(err).Error("Error creating default configuration")
formatter.PrintError(err, "Error creating default configuration")
Expand All @@ -123,14 +135,11 @@ func initConfigs() {
commands.Config = conf
}

if yamlConfigFile != "" {
commands.Config.ConfigFile = paths.New(yamlConfigFile)
// Read configuration from global config file
if commands.Config.ConfigFile.Exist() {
readConfigFrom(commands.Config.ConfigFile)
}

logrus.Info("Initiating configuration")
if err := commands.Config.LoadFromYAML(commands.Config.ConfigFile); err != nil {
logrus.WithError(err).Warn("Did not manage to get config file, using default configuration")
}
if commands.Config.IsBundledInDesktopIDE() {
logrus.Info("CLI is bundled into the IDE")
err := commands.Config.LoadFromDesktopIDEPreferences()
Expand All @@ -140,6 +149,32 @@ func initConfigs() {
} 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-cli.yaml"); path.Exist() {
readConfigFrom(path)
}
} else {
commands.Config.Navigate("/", pwd.String())
}

// Read configuration from environment vars
commands.Config.LoadFromEnv()

// Read configuration from user specified file
if yamlConfigFile != "" {
commands.Config.ConfigFile = paths.New(yamlConfigFile)
readConfigFrom(commands.Config.ConfigFile)
}

logrus.Info("Configuration set")
}

func readConfigFrom(path *paths.Path) {
logrus.Infof("Reading configuration from %s", path)
if err := commands.Config.LoadFromYAML(path); err != nil {
logrus.WithError(err).Warnf("Could not read configuration from %s", path)
}
}
28 changes: 21 additions & 7 deletions configs/directories.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,37 @@ package configs

import (
"fmt"
"os"
"os/user"
"runtime"

"github.com/arduino/go-paths-helper"

"github.com/arduino/go-win32-utils"
)

// getDefaultConfigFilePath returns the default path for .cli-config.yml,
// this is the directory where the arduino-cli executable resides.
// getDefaultConfigFilePath returns the default path for arduino-cli.yaml
func getDefaultConfigFilePath() *paths.Path {
executablePath, err := os.Executable()
usr, err := user.Current()
if err != nil {
executablePath = "."
panic(fmt.Errorf("retrieving user home dir: %s", err))
}
return paths.New(executablePath).Parent().Join(".cli-config.yml")
arduinoDataDir := paths.New(usr.HomeDir)

switch runtime.GOOS {
case "linux":
arduinoDataDir = arduinoDataDir.Join(".arduino15")
case "darwin":
arduinoDataDir = arduinoDataDir.Join("Library", "arduino15")
case "windows":
localAppDataPath, err := win32.GetLocalAppDataFolder()
if err != nil {
panic(err)
}
arduinoDataDir = paths.New(localAppDataPath).Join("Arduino15")
default:
panic(fmt.Errorf("unsupported OS: %s", runtime.GOOS))
}

return arduinoDataDir.Join("arduino-cli.yaml")
}

func getDefaultArduinoDataDir() (*paths.Path, error) {
Expand Down
41 changes: 41 additions & 0 deletions configs/navigate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of arduino-cli.
*
* Copyright 2018 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 license@arduino.cc.
*/

package configs

import (
"path/filepath"
"strings"

paths "github.com/arduino/go-paths-helper"
)

func (c *Configuration) Navigate(root, pwd string) {
relativePath, err := filepath.Rel(root, pwd)
if err != nil {
return
}

// From the root to the current folder, search for arduino-cli.yaml files
parts := strings.Split(relativePath, string(filepath.Separator))
for i := range parts {
path := paths.New(root)
path = path.Join(parts[:i+1]...)
path = path.Join("arduino-cli.yaml")
_ = c.LoadFromYAML(path)
}
}
Loading