Skip to content

Add option --dest-dir to config init, removed noop --save-as #575

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 1 commit into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 16 additions & 15 deletions cli/config/init.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 [email protected].
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