From 8559c0530bc5c3c4bf441e219a9ce4ab4d3a549b Mon Sep 17 00:00:00 2001 From: Keepers Date: Wed, 26 Oct 2022 08:49:13 -0600 Subject: [PATCH] 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] :sunflower: Feature ## Issue(s) * #1136 ## Test Plan - [x] :muscle: Manual - [x] :green_heart: E2E --- src/cli/repo/s3.go | 3 +-- src/cli/repo/s3_integration_test.go | 5 +++++ src/pkg/repository/repository.go | 8 ++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cli/repo/s3.go b/src/cli/repo/s3.go index 92bb8d46a..e906c921e 100644 --- a/src/cli/repo/s3.go +++ b/src/cli/repo/s3.go @@ -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 } diff --git a/src/cli/repo/s3_integration_test.go b/src/cli/repo/s3_integration_test.go index ac45c6960..08d5e7c8b 100644 --- a/src/cli/repo/s3_integration_test.go +++ b/src/cli/repo/s3_integration_test.go @@ -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) }) } } diff --git a/src/pkg/repository/repository.go b/src/pkg/repository/repository.go index 065d0d31a..96e20b572 100644 --- a/src/pkg/repository/repository.go +++ b/src/pkg/repository/repository.go @@ -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