Skip to content

Commit 9a4d50e

Browse files
committed
refactor config validation to remove some code duplication
1 parent 417d61f commit 9a4d50e

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

cli/config/add.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ func initAddCommand() *cobra.Command {
4444

4545
func runAddCommand(cmd *cobra.Command, args []string) {
4646
key := args[0]
47-
kind, err := typeOf(key)
48-
if err != nil {
49-
feedback.Error(err)
50-
os.Exit(errorcodes.ErrGeneric)
51-
}
47+
kind := validateKey(key)
5248

5349
if kind != reflect.Slice {
5450
feedback.Errorf(tr("The key '%[1]v' is not a list of items, can't add to it.\nMaybe use '%[2]s'?"), key, "config set")

cli/config/remove.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ func initRemoveCommand() *cobra.Command {
4444

4545
func runRemoveCommand(cmd *cobra.Command, args []string) {
4646
key := args[0]
47-
kind, err := typeOf(key)
48-
if err != nil {
49-
feedback.Error(err)
50-
os.Exit(errorcodes.ErrGeneric)
51-
}
47+
kind := validateKey(key)
5248

5349
if kind != reflect.Slice {
5450
feedback.Errorf(tr("The key '%[1]v' is not a list of items, can't remove from it.\nMaybe use '%[2]s'?"), key, "config delete")

cli/config/set.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ func initSetCommand() *cobra.Command {
4747

4848
func runSetCommand(cmd *cobra.Command, args []string) {
4949
key := args[0]
50-
kind, err := typeOf(key)
51-
if err != nil {
52-
feedback.Error(err)
53-
os.Exit(errorcodes.ErrGeneric)
54-
}
50+
kind := validateKey(key)
5551

5652
if kind != reflect.Slice && len(args) > 2 {
5753
feedback.Errorf(tr("Can't set multiple values in key %v"), key)

cli/config/validate.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ package config
1717

1818
import (
1919
"fmt"
20+
"os"
2021
"reflect"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
2125
)
2226

2327
var validMap = map[string]reflect.Kind{
@@ -46,3 +50,12 @@ func typeOf(key string) (reflect.Kind, error) {
4650
}
4751
return t, nil
4852
}
53+
54+
func validateKey(key string) reflect.Kind {
55+
kind, err := typeOf(key)
56+
if err != nil {
57+
feedback.Error(err)
58+
os.Exit(errorcodes.ErrGeneric)
59+
}
60+
return kind
61+
}

i18n/data/en.po

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,17 @@ msgstr "Can't find dependencies for platform %s"
268268
msgid "Can't open sketch"
269269
msgstr "Can't open sketch"
270270

271-
#: cli/config/set.go:57
271+
#: cli/config/set.go:53
272272
msgid "Can't set multiple values in key %v"
273273
msgstr "Can't set multiple values in key %v"
274274

275275
#: cli/config/init.go:59
276276
msgid "Can't use both --dest-file and --dest-dir flags at the same time."
277277
msgstr "Can't use both --dest-file and --dest-dir flags at the same time."
278278

279-
#: cli/config/add.go:63
279+
#: cli/config/add.go:59
280280
#: cli/config/delete.go:70
281-
#: cli/config/remove.go:72
281+
#: cli/config/remove.go:68
282282
msgid "Can't write config file: %v"
283283
msgstr "Can't write config file: %v"
284284

@@ -1879,7 +1879,7 @@ msgid "Setting"
18791879
msgstr "Setting"
18801880

18811881
#: cli/config/delete.go:60
1882-
#: cli/config/validate.go:45
1882+
#: cli/config/validate.go:49
18831883
msgid "Settings key doesn't exist"
18841884
msgstr "Settings key doesn't exist"
18851885

@@ -2046,13 +2046,13 @@ msgstr "The custom config file (if not specified the default will be used)."
20462046
msgid "The flags --run-post-install and --skip-post-install can't be both set at the same time."
20472047
msgstr "The flags --run-post-install and --skip-post-install can't be both set at the same time."
20482048

2049-
#: cli/config/add.go:54
2049+
#: cli/config/add.go:50
20502050
msgid "The key '%[1]v' is not a list of items, can't add to it.\n"
20512051
"Maybe use '%[2]s'?"
20522052
msgstr "The key '%[1]v' is not a list of items, can't add to it.\n"
20532053
"Maybe use '%[2]s'?"
20542054

2055-
#: cli/config/remove.go:54
2055+
#: cli/config/remove.go:50
20562056
msgid "The key '%[1]v' is not a list of items, can't remove from it.\n"
20572057
"Maybe use '%[2]s'?"
20582058
msgstr "The key '%[1]v' is not a list of items, can't remove from it.\n"
@@ -2409,7 +2409,7 @@ msgstr "Writes current configuration to a configuration file."
24092409
msgid "Writes current configuration to the configuration file in the data directory."
24102410
msgstr "Writes current configuration to the configuration file in the data directory."
24112411

2412-
#: cli/config/set.go:79
2412+
#: cli/config/set.go:75
24132413
msgid "Writing config file: %v"
24142414
msgstr "Writing config file: %v"
24152415

@@ -2630,7 +2630,7 @@ msgstr "encoding sketch metadata: %s"
26302630
msgid "error opening serial monitor"
26312631
msgstr "error opening serial monitor"
26322632

2633-
#: cli/config/set.go:71
2633+
#: cli/config/set.go:67
26342634
msgid "error parsing value: %v"
26352635
msgstr "error parsing value: %v"
26362636

0 commit comments

Comments
 (0)