Skip to content

Commit 553b7be

Browse files
author
Massimiliano Pippi
authored
add option --dest-dir to config init, removed noop --save-as (#575)
1 parent 28875e6 commit 553b7be

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

cli/config/init.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import (
2626
"github.com/spf13/viper"
2727
)
2828

29+
var destDir string
30+
31+
const defaultFileName = "arduino-cli.yaml"
32+
2933
func initInitCommand() *cobra.Command {
3034
initCommand := &cobra.Command{
3135
Use: "init",
@@ -37,31 +41,28 @@ func initInitCommand() *cobra.Command {
3741
Args: cobra.NoArgs,
3842
Run: runInitCommand,
3943
}
40-
initCommand.Flags().StringVar(&initFlags.location, "save-as", "",
41-
"Sets where to save the configuration file [default is ./arduino-cli.yaml].")
44+
initCommand.Flags().StringVar(&destDir, "dest-dir", "", "Sets where to save the configuration file.")
4245
return initCommand
4346
}
4447

45-
var initFlags struct {
46-
location string // The custom location of the file to create.
47-
}
48-
4948
func runInitCommand(cmd *cobra.Command, args []string) {
50-
logrus.Info("Executing `arduino config init`")
49+
if destDir == "" {
50+
destDir = viper.GetString("directories.Data")
51+
}
52+
logrus.Infof("Writing config file to: %s", destDir)
5153

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

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

65-
feedback.Print("Config file written: " + configFile)
66-
logrus.Info("Done")
65+
msg := "Config file written to: " + configFile
66+
logrus.Info(msg)
67+
feedback.Print(msg)
6768
}

test/test_config.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This file is part of arduino-cli.
2+
#
3+
# Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
#
5+
# This software is released under the GNU General Public License version 3,
6+
# which covers the main part of arduino-cli.
7+
# The terms of this license can be found at:
8+
# https://www.gnu.org/licenses/gpl-3.0.en.html
9+
#
10+
# You can be released from the requirements of the above licenses by purchasing
11+
# a commercial license. Buying such a license is mandatory if you want to modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].
15+
import os
16+
17+
18+
def test_init(run_command, data_dir, working_dir):
19+
result = run_command("config init")
20+
assert result.ok
21+
assert data_dir in result.stdout
22+
23+
24+
def test_init_dest(run_command, working_dir):
25+
dest = os.path.join(working_dir, "config", "test")
26+
result = run_command("config init --dest-dir " + dest)
27+
assert result.ok
28+
assert dest in result.stdout

0 commit comments

Comments
 (0)