@@ -24,6 +24,7 @@ import (
2424
2525 "github.com/arduino/arduino-cli/configuration"
2626 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/settings/v1"
27+ "github.com/spf13/viper"
2728)
2829
2930// SettingsService implements the `Settings` service
@@ -144,3 +145,38 @@ func (s *SettingsService) Write(ctx context.Context, req *rpc.WriteRequest) (*rp
144145 }
145146 return & rpc.WriteResponse {}, nil
146147}
148+
149+ // Delete removes a key from the config file
150+ func (s * SettingsService ) Delete (ctx context.Context , req * rpc.DeleteRequest ) (* rpc.DeleteResponse , error ) {
151+ toDelete := req .GetKey ()
152+
153+ // Check if settings key actually existing, we don't use Viper.InConfig()
154+ // since that doesn't check for keys formatted like daemon.port or those set
155+ // with Viper.Set(). This way we check for all existing settings for sure.
156+ keyExists := false
157+ keys := []string {}
158+ for _ , k := range configuration .Settings .AllKeys () {
159+ if ! strings .HasPrefix (k , toDelete ) {
160+ keys = append (keys , k )
161+ continue
162+ }
163+ keyExists = true
164+ }
165+
166+ if ! keyExists {
167+ return nil , errors .New (tr ("key not found in settings" ))
168+ }
169+
170+ updatedSettings := viper .New ()
171+ for _ , k := range keys {
172+ updatedSettings .Set (k , configuration .Settings .Get (k ))
173+ }
174+
175+ // Override current settings to delete the key
176+ configuration .Settings = updatedSettings
177+
178+ if _ , err := s .Write (ctx , & rpc.WriteRequest {FilePath : req .FilePath }); err != nil {
179+ return nil , err
180+ }
181+ return & rpc.DeleteResponse {}, nil
182+ }
0 commit comments