Skip to content

Commit

Permalink
fix: use addrepo RPC to apply validations when updating repo config
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Aug 15, 2024
1 parent 48626b9 commit a67c29b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
10 changes: 9 additions & 1 deletion internal/api/backresthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path"
"reflect"
"slices"
"time"

"connectrpc.com/connect"
Expand Down Expand Up @@ -99,8 +100,15 @@ func (s *BackrestHandler) AddRepo(ctx context.Context, req *connect.Request[v1.R
return nil, fmt.Errorf("failed to get config: %w", err)
}

// Deep copy the configuration
c = proto.Clone(c).(*v1.Config)
c.Repos = append(c.Repos, req.Msg)

// Add or implicit update the repo
if idx := slices.IndexFunc(c.Repos, func(r *v1.Repo) bool { return r.Id == req.Msg.Id }); idx != -1 {
c.Repos[idx] = req.Msg
} else {
c.Repos = append(c.Repos, req.Msg)
}

if err := config.ValidateConfig(c); err != nil {
return nil, fmt.Errorf("validation error: %w", err)
Expand Down
11 changes: 2 additions & 9 deletions webui/src/views/AddRepoModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,8 @@ export const AddRepoModal = ({ template }: { template: Repo | null }) => {
});

if (template !== null) {
const configCopy = config.clone();
// We are in the edit repo flow, update the repo in the config
const idx = configCopy.repos!.findIndex((r) => r.id === template!.id);
if (idx === -1) {
alertsApi.error("Can't update repo, not found");
return;
}
configCopy.repos![idx] = repo;
setConfig(await backrestService.setConfig(configCopy));
// We are in the update repo flow, update the repo via the service
setConfig(await backrestService.addRepo(repo));
showModal(null);
alertsApi.success("Updated repo configuration " + repo.uri);
} else {
Expand Down

0 comments on commit a67c29b

Please sign in to comment.