Skip to content

Commit d57d41f

Browse files
Refactor config delete command using gRPC function call
1 parent b00cdbc commit d57d41f

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

internal/cli/config/delete.go

+6-23
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ package config
1717

1818
import (
1919
"os"
20-
"strings"
2120

21+
"github.com/arduino/arduino-cli/commands/daemon"
2222
"github.com/arduino/arduino-cli/configuration"
2323
"github.com/arduino/arduino-cli/internal/cli/feedback"
24+
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/settings/v1"
2425
"github.com/sirupsen/logrus"
2526
"github.com/spf13/cobra"
26-
"github.com/spf13/viper"
2727
)
2828

2929
func initDeleteCommand() *cobra.Command {
@@ -47,26 +47,9 @@ func runDeleteCommand(cmd *cobra.Command, args []string) {
4747
logrus.Info("Executing `arduino-cli config delete`")
4848
toDelete := args[0]
4949

50-
keys := []string{}
51-
exists := false
52-
for _, v := range configuration.Settings.AllKeys() {
53-
if !strings.HasPrefix(v, toDelete) {
54-
keys = append(keys, v)
55-
continue
56-
}
57-
exists = true
58-
}
59-
60-
if !exists {
61-
feedback.Fatal(tr("Settings key doesn't exist"), feedback.ErrGeneric)
62-
}
63-
64-
updatedSettings := viper.New()
65-
for _, k := range keys {
66-
updatedSettings.Set(k, configuration.Settings.Get(k))
67-
}
68-
69-
if err := updatedSettings.WriteConfigAs(configuration.Settings.ConfigFileUsed()); err != nil {
70-
feedback.Fatal(tr("Can't write config file: %v", err), feedback.ErrGeneric)
50+
svc := daemon.SettingsService{}
51+
_, err := svc.Delete(cmd.Context(), &settings.DeleteRequest{Key: toDelete, FilePath: configuration.Settings.ConfigFileUsed()})
52+
if err != nil {
53+
feedback.Fatal(tr("Error deleting the key %[1]s: %[2]v", toDelete, err), feedback.ErrGeneric)
7154
}
7255
}

0 commit comments

Comments
 (0)