Skip to content

Commit

Permalink
add option --dest-dir to config init, removed noop --save-as (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
Massimiliano Pippi committed Feb 11, 2020
1 parent 28875e6 commit 553b7be
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
31 changes: 16 additions & 15 deletions cli/config/init.go
Expand Up @@ -26,6 +26,10 @@ import (
"github.com/spf13/viper"
)

var destDir string

const defaultFileName = "arduino-cli.yaml"

func initInitCommand() *cobra.Command {
initCommand := &cobra.Command{
Use: "init",
Expand All @@ -37,31 +41,28 @@ func initInitCommand() *cobra.Command {
Args: cobra.NoArgs,
Run: runInitCommand,
}
initCommand.Flags().StringVar(&initFlags.location, "save-as", "",
"Sets where to save the configuration file [default is ./arduino-cli.yaml].")
initCommand.Flags().StringVar(&destDir, "dest-dir", "", "Sets where to save the configuration file.")
return initCommand
}

var initFlags struct {
location string // The custom location of the file to create.
}

func runInitCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino config init`")
if destDir == "" {
destDir = viper.GetString("directories.Data")
}
logrus.Infof("Writing config file to: %s", destDir)

dataDir := viper.GetString("directories.Data")
if err := os.MkdirAll(dataDir, os.FileMode(0755)); err != nil {
feedback.Errorf("Cannot create data directory: %v", err)
if err := os.MkdirAll(destDir, os.FileMode(0755)); err != nil {
feedback.Errorf("Cannot create config file directory: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

configFile := filepath.Join(dataDir, "arduino-cli.yaml")
err := viper.WriteConfigAs(configFile)
if err != nil {
configFile := filepath.Join(destDir, defaultFileName)
if err := viper.WriteConfigAs(configFile); err != nil {
feedback.Errorf("Cannot create config file: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

feedback.Print("Config file written: " + configFile)
logrus.Info("Done")
msg := "Config file written to: " + configFile
logrus.Info(msg)
feedback.Print(msg)
}
28 changes: 28 additions & 0 deletions test/test_config.py
@@ -0,0 +1,28 @@
# This file is part of arduino-cli.
#
# Copyright 2020 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.
import os


def test_init(run_command, data_dir, working_dir):
result = run_command("config init")
assert result.ok
assert data_dir in result.stdout


def test_init_dest(run_command, working_dir):
dest = os.path.join(working_dir, "config", "test")
result = run_command("config init --dest-dir " + dest)
assert result.ok
assert dest in result.stdout

0 comments on commit 553b7be

Please sign in to comment.