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 15 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
9 changes: 9 additions & 0 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
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
20 changes: 16 additions & 4 deletions commands/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package root
import (
"io/ioutil"
"os"
"path/filepath"

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

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,7 @@ func preRun(cmd *cobra.Command, args []string) {

// 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")
formatter.PrintError(err, "Error creating default configuration")
Expand All @@ -123,14 +125,24 @@ func initConfigs() {
commands.Config = conf
}

// Navigate through folders
pwd, err := filepath.Abs(".")
if err != nil {
logrus.WithError(err).Warn("Did not manage to find current path")
}

commands.Config.Navigate("/", pwd)
commands.Config.LoadFromYAML(commands.Config.ConfigFile)

if yamlConfigFile != "" {
commands.Config.ConfigFile = paths.New(yamlConfigFile)
if err := commands.Config.LoadFromYAML(commands.Config.ConfigFile); err != nil {
logrus.WithError(err).Warn("Did not manage to get config file, using default configuration")
}
}

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 Down
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
}

func getDefaultArduinoDataDir() (*paths.Path, error) {
Expand Down
40 changes: 40 additions & 0 deletions configs/navigate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 [email protected].
*/
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)
}
}
71 changes: 71 additions & 0 deletions configs/navigate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 [email protected].
*/
package configs_test

import (
"io/ioutil"
"path/filepath"
"strings"
"testing"

"github.com/arduino/arduino-cli/configs"
homedir "github.com/mitchellh/go-homedir"
"github.com/sergi/go-diff/diffmatchpatch"
)

func TestNavigate(t *testing.T) {
tests := []string{
"noconfig",
"local",
"inheritance",
}
for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
root := filepath.Join("testdata", "navigate", tt)
pwd := filepath.Join("testdata", "navigate", tt, "first", "second")
golden := filepath.Join("testdata", "navigate", tt, "golden.yaml")

config, _ := configs.NewConfiguration()

config.Navigate(root, pwd)
data, _ := config.SerializeToYAML()

diff(t, data, golden)
})
}
}

func diff(t *testing.T, data []byte, goldenFile string) {
golden, err := ioutil.ReadFile(goldenFile)
if err != nil {
t.Error(err)
return
}

dataStr := strings.TrimSpace(string(data))
goldenStr := strings.TrimSpace(string(golden))

// Substitute home folder
homedir, _ := homedir.Dir()
dataStr = strings.Replace(dataStr, homedir, "$HOME", -1)

if dataStr != goldenStr {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(goldenStr, dataStr, false)
t.Errorf(dmp.DiffPrettyText(diffs))
}
}
5 changes: 5 additions & 0 deletions configs/testdata/navigate/inheritance/first/arduino-cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sketchbook_path: /tmp
board_manager:
additional_urls:
- https://downloads.arduino.cc/package_index_mraa.json

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
board_manager:
additional_urls:
- https://downloads.arduino.cc/package_index_mraa.json

7 changes: 7 additions & 0 deletions configs/testdata/navigate/inheritance/golden.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
proxy_type: auto
sketchbook_path: /tmp
arduino_data: $HOME/.arduino15
board_manager:
additional_urls:
- https://downloads.arduino.cc/packages/package_index.json
- https://downloads.arduino.cc/package_index_mraa.json
Empty file.
4 changes: 4 additions & 0 deletions configs/testdata/navigate/local/first/second/arduino-cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
board_manager:
additional_urls:
- https://downloads.arduino.cc/package_index_mraa.json

7 changes: 7 additions & 0 deletions configs/testdata/navigate/local/golden.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
proxy_type: auto
sketchbook_path: $HOME/Arduino
arduino_data: $HOME/.arduino15
board_manager:
additional_urls:
- https://downloads.arduino.cc/packages/package_index.json
- https://downloads.arduino.cc/package_index_mraa.json
Empty file.
6 changes: 6 additions & 0 deletions configs/testdata/navigate/noconfig/golden.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
proxy_type: auto
sketchbook_path: $HOME/Arduino
arduino_data: $HOME/.arduino15
board_manager:
additional_urls:
- https://downloads.arduino.cc/packages/package_index.json
19 changes: 12 additions & 7 deletions configs/yaml_serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net/url"

paths "github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)

Expand All @@ -48,16 +47,13 @@ type yamlProxyConfig struct {

// LoadFromYAML loads the configs from a yaml file.
func (config *Configuration) LoadFromYAML(path *paths.Path) error {
logrus.Info("Unserializing configurations from ", path)
content, err := path.ReadFile()
if err != nil {
logrus.WithError(err).Warn("Error reading config, using default configuration")
return err
}
var ret yamlConfig
err = yaml.Unmarshal(content, &ret)
if err != nil {
logrus.WithError(err).Warn("Error parsing config, using default configuration")
return err
}

Expand All @@ -84,12 +80,12 @@ func (config *Configuration) LoadFromYAML(path *paths.Path) error {
for _, rawurl := range ret.BoardsManager.AdditionalURLS {
url, err := url.Parse(rawurl)
if err != nil {
logrus.WithError(err).Warn("Error parsing config")
continue
}
config.BoardManagerAdditionalUrls = append(config.BoardManagerAdditionalUrls, url)
}
}

return nil
}

Expand All @@ -113,10 +109,10 @@ func (config *Configuration) SerializeToYAML() ([]byte, error) {
Password: config.ProxyPassword,
}
}
c.BoardsManager = &yamlBoardsManagerConfig{AdditionalURLS: []string{}}
if len(config.BoardManagerAdditionalUrls) > 1 {
c.BoardsManager = &yamlBoardsManagerConfig{AdditionalURLS: []string{}}
for _, URL := range config.BoardManagerAdditionalUrls[1:] {
c.BoardsManager.AdditionalURLS = append(c.BoardsManager.AdditionalURLS, URL.String())
c.BoardsManager.AdditionalURLS = appendIfMissing(c.BoardsManager.AdditionalURLS, URL.String())
}
}
return yaml.Marshal(c)
Expand All @@ -134,3 +130,12 @@ func (config *Configuration) SaveToYAML(path string) error {
}
return nil
}

func appendIfMissing(slice []string, i string) []string {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}
Loading