Skip to content

Commit b22aa6f

Browse files
authored
Add new commands to edit config files (#1082)
1 parent 8d026ed commit b22aa6f

File tree

8 files changed

+691
-2
lines changed

8 files changed

+691
-2
lines changed

Diff for: cli/config/add.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package config
17+
18+
import (
19+
"os"
20+
"reflect"
21+
22+
"github.com/arduino/arduino-cli/cli/errorcodes"
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/arduino-cli/configuration"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
func initAddCommand() *cobra.Command {
29+
addCommand := &cobra.Command{
30+
Use: "add",
31+
Short: "Adds one or more values to a setting.",
32+
Long: "Adds one or more values to a setting.",
33+
Example: "" +
34+
" " + os.Args[0] + " config add board_manager.additional_urls https://example.com/package_example_index.json\n" +
35+
" " + os.Args[0] + " config add board_manager.additional_urls https://example.com/package_example_index.json https://another-url.com/package_another_index.json\n",
36+
Args: cobra.MinimumNArgs(2),
37+
Run: runAddCommand,
38+
}
39+
return addCommand
40+
}
41+
42+
func runAddCommand(cmd *cobra.Command, args []string) {
43+
key := args[0]
44+
kind, err := typeOf(key)
45+
if err != nil {
46+
feedback.Error(err)
47+
os.Exit(errorcodes.ErrGeneric)
48+
}
49+
50+
if kind != reflect.Slice {
51+
feedback.Errorf("The key '%v' is not a list of items, can't add to it.\nMaybe use 'config set'?", key)
52+
os.Exit(errorcodes.ErrGeneric)
53+
}
54+
55+
v := configuration.Settings.GetStringSlice(key)
56+
v = append(v, args[1:]...)
57+
configuration.Settings.Set(key, v)
58+
59+
if err := configuration.Settings.WriteConfig(); err != nil {
60+
feedback.Errorf("Can't write config file: %v", err)
61+
os.Exit(errorcodes.ErrGeneric)
62+
}
63+
}

Diff for: cli/config/config.go

+4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ func NewCommand() *cobra.Command {
2929
Example: " " + os.Args[0] + " config init",
3030
}
3131

32+
configCommand.AddCommand(initAddCommand())
33+
configCommand.AddCommand(initDeleteCommand())
3234
configCommand.AddCommand(initDumpCmd())
3335
configCommand.AddCommand(initInitCommand())
36+
configCommand.AddCommand(initRemoveCommand())
37+
configCommand.AddCommand(initSetCommand())
3438

3539
return configCommand
3640
}

Diff for: cli/config/delete.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package config
17+
18+
import (
19+
"os"
20+
"strings"
21+
22+
"github.com/arduino/arduino-cli/cli/errorcodes"
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/arduino-cli/configuration"
25+
"github.com/spf13/cobra"
26+
"github.com/spf13/viper"
27+
)
28+
29+
func initDeleteCommand() *cobra.Command {
30+
addCommand := &cobra.Command{
31+
Use: "delete",
32+
Short: "Deletes a settings key and all its sub keys.",
33+
Long: "Deletes a settings key and all its sub keys.",
34+
Example: "" +
35+
" " + os.Args[0] + " config delete board_manager\n" +
36+
" " + os.Args[0] + " config delete board_manager.additional_urls",
37+
Args: cobra.ExactArgs(1),
38+
Run: runDeleteCommand,
39+
}
40+
return addCommand
41+
}
42+
43+
func runDeleteCommand(cmd *cobra.Command, args []string) {
44+
toDelete := args[0]
45+
46+
keys := []string{}
47+
exists := false
48+
for _, v := range configuration.Settings.AllKeys() {
49+
if !strings.HasPrefix(v, toDelete) {
50+
keys = append(keys, v)
51+
continue
52+
}
53+
exists = true
54+
}
55+
56+
if !exists {
57+
feedback.Errorf("Settings key doesn't exist")
58+
os.Exit(errorcodes.ErrGeneric)
59+
}
60+
61+
updatedSettings := viper.New()
62+
for _, k := range keys {
63+
updatedSettings.Set(k, configuration.Settings.Get(k))
64+
}
65+
66+
if err := updatedSettings.WriteConfigAs(configuration.Settings.ConfigFileUsed()); err != nil {
67+
feedback.Errorf("Can't write config file: %v", err)
68+
os.Exit(errorcodes.ErrGeneric)
69+
}
70+
}

Diff for: cli/config/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func initInitCommand() *cobra.Command {
4242
Long: "Creates or updates the configuration file in the data directory or custom directory with the current configuration settings.",
4343
Example: "" +
4444
" # Writes current configuration to the configuration file in the data directory.\n" +
45-
" " + os.Args[0] + " config init" +
46-
" " + os.Args[0] + " config init --dest-dir /home/user/MyDirectory" +
45+
" " + os.Args[0] + " config init\n" +
46+
" " + os.Args[0] + " config init --dest-dir /home/user/MyDirectory\n" +
4747
" " + os.Args[0] + " config init --dest-file /home/user/MyDirectory/my_settings.yaml",
4848
Args: cobra.NoArgs,
4949
Run: runInitCommand,

Diff for: cli/config/remove.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package config
17+
18+
import (
19+
"os"
20+
"reflect"
21+
22+
"github.com/arduino/arduino-cli/cli/errorcodes"
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/arduino-cli/configuration"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
func initRemoveCommand() *cobra.Command {
29+
addCommand := &cobra.Command{
30+
Use: "remove",
31+
Short: "Removes one or more values from a setting.",
32+
Long: "Removes one or more values from a setting.",
33+
Example: "" +
34+
" " + os.Args[0] + " config remove board_manager.additional_urls https://example.com/package_example_index.json\n" +
35+
" " + os.Args[0] + " config remove board_manager.additional_urls https://example.com/package_example_index.json https://another-url.com/package_another_index.json\n",
36+
Args: cobra.MinimumNArgs(2),
37+
Run: runRemoveCommand,
38+
}
39+
return addCommand
40+
}
41+
42+
func runRemoveCommand(cmd *cobra.Command, args []string) {
43+
key := args[0]
44+
kind, err := typeOf(key)
45+
if err != nil {
46+
feedback.Error(err)
47+
os.Exit(errorcodes.ErrGeneric)
48+
}
49+
50+
if kind != reflect.Slice {
51+
feedback.Errorf("The key '%v' is not a list of items, can't remove from it.\nMaybe use 'config delete'?", key)
52+
os.Exit(errorcodes.ErrGeneric)
53+
}
54+
55+
mappedValues := map[string]bool{}
56+
for _, v := range configuration.Settings.GetStringSlice(key) {
57+
mappedValues[v] = true
58+
}
59+
for _, arg := range args[1:] {
60+
delete(mappedValues, arg)
61+
}
62+
values := []string{}
63+
for k := range mappedValues {
64+
values = append(values, k)
65+
}
66+
configuration.Settings.Set(key, values)
67+
68+
if err := configuration.Settings.WriteConfig(); err != nil {
69+
feedback.Errorf("Can't write config file: %v", err)
70+
os.Exit(errorcodes.ErrGeneric)
71+
}
72+
}

Diff for: cli/config/set.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package config
17+
18+
import (
19+
"os"
20+
"reflect"
21+
"strconv"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cli/configuration"
26+
"github.com/spf13/cobra"
27+
)
28+
29+
func initSetCommand() *cobra.Command {
30+
addCommand := &cobra.Command{
31+
Use: "set",
32+
Short: "Sets a setting value.",
33+
Long: "Sets a setting value.",
34+
Example: "" +
35+
" " + os.Args[0] + " config set logging.level trace\n" +
36+
" " + os.Args[0] + " config set logging.file my-log.txt\n" +
37+
" " + os.Args[0] + " config set sketch.always_export_binaries true\n" +
38+
" " + os.Args[0] + " config set board_manager.additional_urls https://example.com/package_example_index.json https://another-url.com/package_another_index.json",
39+
Args: cobra.MinimumNArgs(2),
40+
Run: runSetCommand,
41+
}
42+
return addCommand
43+
}
44+
45+
func runSetCommand(cmd *cobra.Command, args []string) {
46+
key := args[0]
47+
kind, err := typeOf(key)
48+
if err != nil {
49+
feedback.Error(err)
50+
os.Exit(errorcodes.ErrGeneric)
51+
}
52+
53+
if kind != reflect.Slice && len(args) > 2 {
54+
feedback.Errorf("Can't set multiple values in key %v", key)
55+
os.Exit(errorcodes.ErrGeneric)
56+
}
57+
58+
var value interface{}
59+
switch kind {
60+
case reflect.Slice:
61+
value = args[1:]
62+
case reflect.String:
63+
value = args[1]
64+
case reflect.Bool:
65+
var err error
66+
value, err = strconv.ParseBool(args[1])
67+
if err != nil {
68+
feedback.Errorf("error parsing value: %v", err)
69+
os.Exit(errorcodes.ErrGeneric)
70+
}
71+
}
72+
73+
configuration.Settings.Set(key, value)
74+
75+
if err := configuration.Settings.WriteConfig(); err != nil {
76+
feedback.Errorf("Writing config file: %v", err)
77+
os.Exit(errorcodes.ErrGeneric)
78+
}
79+
}

Diff for: cli/config/validate.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package config
17+
18+
import (
19+
"fmt"
20+
"reflect"
21+
)
22+
23+
var validMap = map[string]reflect.Kind{
24+
"board_manager.additional_urls": reflect.Slice,
25+
"daemon.port": reflect.String,
26+
"directories.data": reflect.String,
27+
"directories.downloads": reflect.String,
28+
"directories.user": reflect.String,
29+
"library.enable_unsafe_install": reflect.Bool,
30+
"logging.file": reflect.String,
31+
"logging.format": reflect.String,
32+
"logging.level": reflect.String,
33+
"sketch.always_export_binaries": reflect.Bool,
34+
"telemetry.addr": reflect.String,
35+
"telemetry.enabled": reflect.Bool,
36+
}
37+
38+
func typeOf(key string) (reflect.Kind, error) {
39+
t, ok := validMap[key]
40+
if !ok {
41+
return reflect.Invalid, fmt.Errorf("Settings key doesn't exist")
42+
}
43+
return t, nil
44+
}

0 commit comments

Comments
 (0)