diff --git a/cli/config/init.go b/cli/config/init.go index 8c8a2395ee4..49b98905ae7 100644 --- a/cli/config/init.go +++ b/cli/config/init.go @@ -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", @@ -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) } diff --git a/test/test_config.py b/test/test_config.py new file mode 100644 index 00000000000..5c333e2376e --- /dev/null +++ b/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