Skip to content

Commit 85956f0

Browse files
Add Delete command to settings
1 parent 7a04d89 commit 85956f0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

commands/daemon/settings.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,36 @@ func (s *SettingsService) Write(ctx context.Context, req *rpc.WriteRequest) (*rp
144144
}
145145
return &rpc.WriteResponse{}, nil
146146
}
147+
148+
// Delete removes a key from the config file
149+
func (s *SettingsService) Delete(ctx context.Context, req *rpc.DeleteRequest) (*rpc.DeleteResponse, error) {
150+
toDelete := req.GetKey()
151+
152+
// Check if settings key actually existing, we don't use Viper.InConfig()
153+
// since that doesn't check for keys formatted like daemon.port or those set
154+
// with Viper.Set(). This way we check for all existing settings for sure.
155+
keyExists := false
156+
keys := []string{}
157+
for _, k := range configuration.Settings.AllKeys() {
158+
if !strings.HasPrefix(k, toDelete) {
159+
keys = append(keys, k)
160+
continue
161+
}
162+
keyExists = true
163+
}
164+
165+
if !keyExists {
166+
return nil, errors.New(tr("key not found in settings"))
167+
}
168+
169+
// Override current settings to delete the key
170+
updatedSettings := configuration.Init("")
171+
for _, k := range keys {
172+
updatedSettings.Set(k, configuration.Settings.Get(k))
173+
}
174+
configPath := configuration.Settings.ConfigFileUsed()
175+
updatedSettings.SetConfigFile(configPath)
176+
configuration.Settings = updatedSettings
177+
178+
return &rpc.DeleteResponse{}, nil
179+
}

0 commit comments

Comments
 (0)