Skip to content

Commit 820acee

Browse files
committed
Change behaviour of --config-file flag with config commands
1 parent 47aafe3 commit 820acee

File tree

3 files changed

+172
-13
lines changed

3 files changed

+172
-13
lines changed

cli/config/dump.go

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package config
1818
import (
1919
"os"
2020

21+
"github.com/arduino/arduino-cli/cli/errorcodes"
2122
"github.com/arduino/arduino-cli/cli/feedback"
2223
"github.com/sirupsen/logrus"
2324
"github.com/spf13/cobra"
@@ -59,5 +60,21 @@ func (dr dumpResult) String() string {
5960

6061
func runDumpCommand(cmd *cobra.Command, args []string) {
6162
logrus.Info("Executing `arduino config dump`")
63+
64+
configFlag := cmd.InheritedFlags().Lookup("config-file")
65+
configFile := ""
66+
if configFlag != nil {
67+
configFile = configFlag.Value.String()
68+
}
69+
70+
if configFile != "" {
71+
viper.SetConfigFile(configFile)
72+
err := viper.MergeInConfig()
73+
if err != nil {
74+
feedback.Errorf("Error reading config file: %s", configFile)
75+
os.Exit(errorcodes.ErrBadArgument)
76+
}
77+
}
78+
6279
feedback.PrintResult(dumpResult{viper.AllSettings()})
6380
}

cli/config/init.go

+42-10
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ package config
1717

1818
import (
1919
"os"
20-
"path/filepath"
2120

2221
"github.com/arduino/arduino-cli/cli/errorcodes"
2322
"github.com/arduino/arduino-cli/cli/feedback"
23+
paths "github.com/arduino/go-paths-helper"
2424
"github.com/sirupsen/logrus"
2525
"github.com/spf13/cobra"
2626
"github.com/spf13/viper"
2727
)
2828

2929
var destDir string
30+
var overwrite bool
3031

3132
const defaultFileName = "arduino-cli.yaml"
3233

@@ -42,34 +43,65 @@ func initInitCommand() *cobra.Command {
4243
Run: runInitCommand,
4344
}
4445
initCommand.Flags().StringVar(&destDir, "dest-dir", "", "Sets where to save the configuration file.")
46+
initCommand.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite existing config file.")
4547
return initCommand
4648
}
4749

4850
func runInitCommand(cmd *cobra.Command, args []string) {
49-
if destDir == "" {
51+
configFlag := cmd.InheritedFlags().Lookup("config-file")
52+
configFilePath := ""
53+
54+
if configFlag != nil {
55+
configFilePath = configFlag.Value.String()
56+
}
57+
58+
if configFilePath != "" && destDir != "" {
59+
feedback.Errorf("Can't use both --config-file and --dest-dir flags at the same time.")
60+
os.Exit(errorcodes.ErrGeneric)
61+
}
62+
63+
var configFileAbsPath *paths.Path
64+
var absPath *paths.Path
65+
var err error
66+
67+
switch {
68+
case configFilePath != "":
69+
configFileAbsPath, err = paths.New(configFilePath).Abs()
70+
if err != nil {
71+
feedback.Errorf("Cannot find absolute path: %v", err)
72+
os.Exit(errorcodes.ErrGeneric)
73+
}
74+
75+
absPath = configFileAbsPath.Parent()
76+
case destDir == "":
5077
destDir = viper.GetString("directories.Data")
78+
fallthrough
79+
default:
80+
absPath, err = paths.New(destDir).Abs()
81+
if err != nil {
82+
feedback.Errorf("Cannot find absolute path: %v", err)
83+
os.Exit(errorcodes.ErrGeneric)
84+
}
85+
configFileAbsPath = absPath.Join(defaultFileName)
5186
}
5287

53-
absPath, err := filepath.Abs(destDir)
54-
if err != nil {
55-
feedback.Errorf("Cannot find absolute path: %v", err)
88+
if !overwrite && configFileAbsPath.Exist() {
89+
feedback.Error("Config file already exists, use --overwrite to discard the existing one.")
5690
os.Exit(errorcodes.ErrGeneric)
5791
}
58-
configFileAbsPath := filepath.Join(absPath, defaultFileName)
5992

6093
logrus.Infof("Writing config file to: %s", absPath)
61-
62-
if err := os.MkdirAll(absPath, os.FileMode(0755)); err != nil {
94+
if err := absPath.MkdirAll(); err != nil {
6395
feedback.Errorf("Cannot create config file directory: %v", err)
6496
os.Exit(errorcodes.ErrGeneric)
6597
}
6698

67-
if err := viper.WriteConfigAs(configFileAbsPath); err != nil {
99+
if err := viper.WriteConfigAs(configFileAbsPath.String()); err != nil {
68100
feedback.Errorf("Cannot create config file: %v", err)
69101
os.Exit(errorcodes.ErrGeneric)
70102
}
71103

72-
msg := "Config file written to: " + configFileAbsPath
104+
msg := "Config file written to: " + configFileAbsPath.String()
73105
logrus.Info(msg)
74106
feedback.Print(msg)
75107
}

test/test_config.py

+113-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# software without disclosing the source code of your own applications. To purchase
1414
# a commercial license, send an email to [email protected].
1515
from pathlib import Path
16+
import json
1617

1718

1819
def test_init(run_command, data_dir, working_dir):
@@ -21,8 +22,117 @@ def test_init(run_command, data_dir, working_dir):
2122
assert data_dir in result.stdout
2223

2324

24-
def test_init_dest(run_command, working_dir):
25-
dest = str(Path(working_dir) / "config" / "test")
25+
def test_init_dest_absolute_path(run_command, working_dir):
26+
dest = Path(working_dir) / "config" / "test"
27+
expected_config_file = dest / "arduino-cli.yaml"
28+
assert not expected_config_file.exists()
2629
result = run_command(f'config init --dest-dir "{dest}"')
2730
assert result.ok
28-
assert dest in result.stdout
31+
assert str(expected_config_file) in result.stdout
32+
assert expected_config_file.exists()
33+
34+
35+
def test_init_dest_relative_path(run_command, working_dir):
36+
dest = Path(working_dir) / "config" / "test"
37+
expected_config_file = dest / "arduino-cli.yaml"
38+
assert not expected_config_file.exists()
39+
result = run_command(f'config init --dest-dir "config/test"')
40+
assert result.ok
41+
assert str(expected_config_file) in result.stdout
42+
assert expected_config_file.exists()
43+
44+
45+
def test_init_dest_flag_with_overwrite_flag(run_command, working_dir):
46+
dest = Path(working_dir) / "config" / "test"
47+
48+
expected_config_file = dest / "arduino-cli.yaml"
49+
assert not expected_config_file.exists()
50+
51+
result = run_command(f'config init --dest-dir "{dest}"')
52+
assert result.ok
53+
assert expected_config_file.exists()
54+
55+
result = run_command(f'config init --dest-dir "{dest}"')
56+
assert result.failed
57+
assert "Config file already exists, use --overwrite to discard the existing one." in result.stderr
58+
59+
result = run_command(f'config init --dest-dir "{dest}" --overwrite')
60+
assert result.ok
61+
assert str(expected_config_file) in result.stdout
62+
63+
64+
def test_init_dest_and_config_file_flags(run_command, working_dir):
65+
result = run_command(f'config init --config-file "some_other_path" --dest-dir "some_path"')
66+
assert result.failed
67+
assert "Can't use both --config-file and --dest-dir flags at the same time." in result.stderr
68+
69+
70+
def test_init_config_file_flag_absolute_path(run_command, working_dir):
71+
config_file = Path(working_dir) / "config" / "test" / "config.yaml"
72+
assert not config_file.exists()
73+
result = run_command(f'config init --config-file "{config_file}"')
74+
assert result.ok
75+
assert str(config_file) in result.stdout
76+
assert config_file.exists()
77+
78+
79+
def test_init_config_file_flag_relative_path(run_command, working_dir):
80+
config_file = Path(working_dir) / "config.yaml"
81+
assert not config_file.exists()
82+
result = run_command(f'config init --config-file "config.yaml"')
83+
assert result.ok
84+
assert str(config_file) in result.stdout
85+
assert config_file.exists()
86+
87+
88+
def test_init_config_file_flag_with_overwrite_flag(run_command, working_dir):
89+
config_file = Path(working_dir) / "config" / "test" / "config.yaml"
90+
assert not config_file.exists()
91+
92+
result = run_command(f'config init --config-file "{config_file}"')
93+
assert result.ok
94+
assert config_file.exists()
95+
96+
result = run_command(f'config init --config-file "{config_file}"')
97+
assert result.failed
98+
assert "Config file already exists, use --overwrite to discard the existing one." in result.stderr
99+
100+
result = run_command(f'config init --config-file "{config_file}" --overwrite')
101+
assert result.ok
102+
assert str(config_file) in result.stdout
103+
104+
105+
def test_dump(run_command, working_dir):
106+
# Create a config file first
107+
config_file = Path(working_dir) / "config" / "test" / "config.yaml"
108+
assert not config_file.exists()
109+
result = run_command(f'config init --config-file "{config_file}"')
110+
assert result.ok
111+
assert config_file.exists()
112+
113+
result = run_command("config dump --format json")
114+
assert result.ok
115+
settings_json = json.loads(result.stdout)
116+
assert [] == settings_json["board_manager"]["additional_urls"]
117+
118+
119+
def test_dump_with_config_file_flag(run_command, working_dir):
120+
# Create a config file first
121+
config_file = Path(working_dir) / "config" / "test" / "config.yaml"
122+
assert not config_file.exists()
123+
result = run_command(f'config init --config-file "{config_file}" --additional-urls=https://example.com')
124+
assert result.ok
125+
assert config_file.exists()
126+
127+
result = run_command(f'config dump --config-file "{config_file}" --format json')
128+
assert result.ok
129+
settings_json = json.loads(result.stdout)
130+
assert ["https://example.com"] == settings_json["board_manager"]["additional_urls"]
131+
132+
result = run_command(
133+
f'config dump --config-file "{config_file}" --additional-urls=https://another-url.com --format json'
134+
)
135+
assert result.ok
136+
settings_json = json.loads(result.stdout)
137+
assert ["https://another-url.com"] == settings_json["board_manager"]["additional_urls"]
138+

0 commit comments

Comments
 (0)