Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Delete command to settings
  • Loading branch information
MatteoPologruto committed Jun 27, 2023
commit 85956f033cc2a6467af19a021d1e5a379fb775e3
33 changes: 33 additions & 0 deletions commands/daemon/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,36 @@ func (s *SettingsService) Write(ctx context.Context, req *rpc.WriteRequest) (*rp
}
return &rpc.WriteResponse{}, nil
}

// Delete removes a key from the config file
func (s *SettingsService) Delete(ctx context.Context, req *rpc.DeleteRequest) (*rpc.DeleteResponse, error) {
toDelete := req.GetKey()

// Check if settings key actually existing, we don't use Viper.InConfig()
// since that doesn't check for keys formatted like daemon.port or those set
// with Viper.Set(). This way we check for all existing settings for sure.
keyExists := false
keys := []string{}
for _, k := range configuration.Settings.AllKeys() {
if !strings.HasPrefix(k, toDelete) {
keys = append(keys, k)
continue
}
keyExists = true
}

if !keyExists {
return nil, errors.New(tr("key not found in settings"))
}

// Override current settings to delete the key
updatedSettings := configuration.Init("")
for _, k := range keys {
updatedSettings.Set(k, configuration.Settings.Get(k))
}
configPath := configuration.Settings.ConfigFileUsed()
updatedSettings.SetConfigFile(configPath)
configuration.Settings = updatedSettings

return &rpc.DeleteResponse{}, nil
}