export repoExists error from repository (#1335)

## Description

Intercept the lower-level RepoAlreadyExists error, and field a repository package version of the same error for sdk consumers to identify when initializing corso repos.

## Type of change

- [x] 🌻 Feature

## Issue(s)

* #1136

## Test Plan

- [x] 💪 Manual
- [x] 💚 E2E
This commit is contained in:
Keepers 2022-10-26 08:49:13 -06:00 committed by GitHub
parent 8ddbc16077
commit 8559c0530b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View File

@ -9,7 +9,6 @@ import (
"github.com/alcionai/corso/src/cli/options"
. "github.com/alcionai/corso/src/cli/print"
"github.com/alcionai/corso/src/cli/utils"
"github.com/alcionai/corso/src/internal/kopia"
"github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/repository"
"github.com/alcionai/corso/src/pkg/storage"
@ -121,7 +120,7 @@ func initS3Cmd(cmd *cobra.Command, args []string) error {
r, err := repository.Initialize(ctx, a, s, options.Control())
if err != nil {
if succeedIfExists && kopia.IsRepoAlreadyExistsError(err) {
if succeedIfExists && errors.Is(err, repository.ErrorRepoAlreadyExists) {
return nil
}

View File

@ -76,6 +76,11 @@ func (suite *S3IntegrationSuite) TestInitS3Cmd() {
// run the command
require.NoError(t, cmd.ExecuteContext(ctx))
// a second initialization should result in an error
err = cmd.ExecuteContext(ctx)
assert.Error(t, err)
assert.ErrorIs(t, err, repository.ErrorRepoAlreadyExists)
})
}
}

View File

@ -2,6 +2,7 @@ package repository
import (
"context"
"errors"
"time"
"github.com/google/uuid"
@ -20,6 +21,8 @@ import (
"github.com/alcionai/corso/src/pkg/store"
)
var ErrorRepoAlreadyExists = errors.New("a repository was already initialized with that configuration")
// BackupGetter deals with retrieving metadata about backups from the
// repository.
type BackupGetter interface {
@ -78,6 +81,11 @@ func Initialize(
) (Repository, error) {
kopiaRef := kopia.NewConn(s)
if err := kopiaRef.Initialize(ctx); err != nil {
// replace common internal errors so that sdk users can check results with errors.Is()
if kopia.IsRepoAlreadyExistsError(err) {
return nil, ErrorRepoAlreadyExists
}
return nil, err
}
// kopiaRef comes with a count of 1 and NewWrapper/NewModelStore bumps it again so safe