Skip to content

Commit

Permalink
refactors ostree api errors (#39)
Browse files Browse the repository at this point in the history
* gofumpt
refactors ostree api errors

* Update internal/plugins/ostree/pkg/ostreerepository/api.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update internal/plugins/ostree/pkg/ostreerepository/api.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: kishie <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 6, 2024
1 parent 2fbfcb6 commit 98866e2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
6 changes: 4 additions & 2 deletions build/mage/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package mage

import (
"context"
"dagger.io/dagger"
"fmt"
"github.com/magefile/mage/mg"
"strings"

"dagger.io/dagger"

"github.com/magefile/mage/mg"
)

type Test mg.Namespace
Expand Down
1 change: 0 additions & 1 deletion internal/plugins/mirror/pkg/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func TestGenerate(t *testing.T) {

// Call the function under test
result, err := Generate(c)

// Check the result
if err != nil {
t.Errorf("Generate() returned an error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/plugins/ostree/pkg/libostree/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
log.Fatalln("testdata/repo does not exist: please run ./generate-testdata.sh")
}

if err := os.MkdirAll("/tmp/libostree-pull_test", 0755); err != nil {
if err := os.MkdirAll("/tmp/libostree-pull_test", 0o755); err != nil {
log.Fatalf("failed to create test directory: %s", err.Error())
}

Expand Down
33 changes: 17 additions & 16 deletions internal/plugins/ostree/pkg/ostreerepository/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"path/filepath"
"time"

"go.ciq.dev/beskar/cmd/beskarctl/ctl"
"github.com/RussellLuo/kun/pkg/werror"
"github.com/RussellLuo/kun/pkg/werror/gcode"
"go.ciq.dev/beskar/internal/plugins/ostree/pkg/libostree"
"go.ciq.dev/beskar/pkg/orasostree"
apiv1 "go.ciq.dev/beskar/pkg/plugins/ostree/api/v1"
Expand All @@ -22,12 +23,12 @@ func (h *Handler) CreateRepository(ctx context.Context, properties *apiv1.OSTree
h.logger.Debug("creating repository", "repository", h.Repository)
// Validate request
if len(properties.Remotes) == 0 {
return ctl.Errf("at least one remote is required")
return werror.Wrap(gcode.ErrInvalidArgument, fmt.Errorf("remotes are required"))
}

// Check if repo already exists
if h.checkRepoExists(ctx) {
return ctl.Err("repository already exists")
return werror.Wrap(gcode.ErrAlreadyExists, fmt.Errorf("repository already exists"))
}

// Transition to provisioning state
Expand All @@ -45,12 +46,12 @@ func (h *Handler) CreateRepository(ctx context.Context, properties *apiv1.OSTree
opts = append(opts, libostree.NoGPGVerify())
}
if err := repo.AddRemote(remote.Name, remote.RemoteURL, opts...); err != nil {
return false, ctl.Errf("adding remote to ostree repository %s: %s", remote.Name, err)
return false, werror.Wrap(gcode.ErrInternal, fmt.Errorf("adding remote to ostree repository %s: %w", remote.Name, err))
}
}

if err := repo.RegenerateSummary(); err != nil {
return false, ctl.Errf("regenerating summary for ostree repository %s: %s", h.repoDir, err)
return false, werror.Wrap(gcode.ErrInternal, fmt.Errorf("regenerating summary for ostree repository %s: %w", h.repoDir, err))
}

return true, nil
Expand All @@ -69,7 +70,7 @@ func (h *Handler) DeleteRepository(ctx context.Context) (err error) {
// Check if repo already exists
if !h.checkRepoExists(ctx) {
defer h.clearState()
return ctl.Err("repository does not exist")
return werror.Wrap(gcode.ErrNotFound, fmt.Errorf("repository does not exist"))
}

go func() {
Expand Down Expand Up @@ -143,20 +144,20 @@ func (h *Handler) ListRepositoryRefs(ctx context.Context) (refs []apiv1.OSTreeRe
defer h.clearState()

if !h.checkRepoExists(ctx) {
return nil, ctl.Errf("repository does not exist")
return nil, werror.Wrap(gcode.ErrNotFound, fmt.Errorf("repository does not exist"))
}

err = h.BeginLocalRepoTransaction(ctx, func(ctx context.Context, repo *libostree.Repo) (bool, error) {
// Get the refs from the local repo
loRefs, err := repo.ListRefsExt(libostree.ListRefsExtFlagsNone)
if err != nil {
return false, ctl.Errf("listing refs from ostree repository: %s", err)
return false, werror.Wrap(gcode.ErrInternal, fmt.Errorf("listing refs from ostree repository: %w", err))
}

// Convert the refs to the API type
for _, loRef := range loRefs {
if loRef.Name == "" || loRef.Checksum == "" {
return false, ctl.Errf("invalid ref data encountered")
return false, werror.Wrap(gcode.ErrNotFound, fmt.Errorf("encountered ref with empty name or checksum"))
}
refs = append(refs, apiv1.OSTreeRef{
Name: loRef.Name,
Expand All @@ -178,7 +179,7 @@ func (h *Handler) AddRemote(ctx context.Context, remote *apiv1.OSTreeRemotePrope
defer h.clearState()

if !h.checkRepoExists(ctx) {
return ctl.Errf("repository does not exist")
return werror.Wrap(gcode.ErrNotFound, fmt.Errorf("repository does not exist"))
}

return h.BeginLocalRepoTransaction(ctx, func(ctx context.Context, repo *libostree.Repo) (bool, error) {
Expand All @@ -204,7 +205,7 @@ func (h *Handler) UpdateRemote(ctx context.Context, remoteName string, remote *a
defer h.clearState()

if !h.checkRepoExists(ctx) {
return ctl.Errf("repository does not exist")
return werror.Wrap(gcode.ErrNotFound, fmt.Errorf("repository does not exist"))
}

return h.BeginLocalRepoTransaction(ctx, func(ctx context.Context, repo *libostree.Repo) (bool, error) {
Expand Down Expand Up @@ -236,7 +237,7 @@ func (h *Handler) DeleteRemote(ctx context.Context, remoteName string) (err erro
defer h.clearState()

if !h.checkRepoExists(ctx) {
return ctl.Errf("repository does not exist")
return werror.Wrap(gcode.ErrNotFound, fmt.Errorf("repository does not exist"))
}

return h.BeginLocalRepoTransaction(ctx, func(ctx context.Context, repo *libostree.Repo) (bool, error) {
Expand Down Expand Up @@ -313,17 +314,17 @@ func (h *Handler) SyncRepository(_ context.Context, properties *apiv1.OSTreeRepo

// pull remote content into local repo
if err := repo.Pull(ctx, remoteName, opts...); err != nil {
return false, ctl.Errf("pulling ostree repository: %s", err)
return false, werror.Wrap(gcode.ErrInternal, fmt.Errorf("pulling ostree repository: %w", err))
}

if err := repo.RegenerateSummary(); err != nil {
return false, ctl.Errf("regenerating summary for ostree repository %s: %s", h.repoDir, err)
return false, werror.Wrap(gcode.ErrInternal, fmt.Errorf("regenerating summary for ostree repository %s: %w", h.repoDir, err))
}

// List the refs in the repository and store in the repoSync
loRefs, err := repo.ListRefsExt(libostree.ListRefsExtFlagsNone)
if err != nil {
return false, ctl.Errf("listing refs from ostree repository: %s", err)
return false, werror.Wrap(gcode.ErrInternal, fmt.Errorf("listing refs from ostree repository: %w", err))
}
repoSync := *h.repoSync.Load()
repoSync.SyncedRefs = loRefs
Expand All @@ -339,7 +340,7 @@ func (h *Handler) SyncRepository(_ context.Context, properties *apiv1.OSTreeRepo
func (h *Handler) GetRepositorySyncStatus(_ context.Context) (syncStatus *apiv1.SyncStatus, err error) {
repoSync := h.repoSync.Load()
if repoSync == nil {
return nil, ctl.Errf("repository sync status not available")
return nil, werror.Wrap(gcode.ErrNotFound, fmt.Errorf("repository sync status not available"))
}

var refs []apiv1.OSTreeRef
Expand Down

0 comments on commit 98866e2

Please sign in to comment.