Skip to content

Commit

Permalink
Merge pull request #4776 from MichaelEischer/cleanup-backend-open
Browse files Browse the repository at this point in the history
unify backend open and create
  • Loading branch information
MichaelEischer committed Apr 24, 2024
2 parents b15d867 + 5f26375 commit 3f9d508
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions cmd/restic/global.go
Expand Up @@ -570,16 +570,13 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro
return cfg, nil
}

// Open the backend specified by a location config.
func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Options) (backend.Backend, error) {
func innerOpen(ctx context.Context, s string, gopts GlobalOptions, opts options.Options, create bool) (backend.Backend, error) {
debug.Log("parsing location %v", location.StripPassword(gopts.backends, s))
loc, err := location.Parse(gopts.backends, s)
if err != nil {
return nil, errors.Fatalf("parsing repository location failed: %v", err)
}

var be backend.Backend

cfg, err := parseConfig(loc, opts)
if err != nil {
return nil, err
Expand All @@ -599,7 +596,13 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio
return nil, errors.Fatalf("invalid backend: %q", loc.Scheme)
}

be, err = factory.Open(ctx, cfg, rt, lim)
var be backend.Backend
if create {
be, err = factory.Create(ctx, cfg, rt, lim)
} else {
be, err = factory.Open(ctx, cfg, rt, lim)
}

if err != nil {
return nil, errors.Fatalf("unable to open repository at %v: %v", location.StripPassword(gopts.backends, s), err)
}
Expand All @@ -615,6 +618,17 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio
}
}

return be, nil
}

// Open the backend specified by a location config.
func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Options) (backend.Backend, error) {

be, err := innerOpen(ctx, s, gopts, opts, false)
if err != nil {
return nil, err
}

// check if config is there
fi, err := be.Stat(ctx, backend.Handle{Type: restic.ConfigFile})
if err != nil {
Expand All @@ -630,31 +644,5 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio

// Create the backend specified by URI.
func create(ctx context.Context, s string, gopts GlobalOptions, opts options.Options) (backend.Backend, error) {
debug.Log("parsing location %v", location.StripPassword(gopts.backends, s))
loc, err := location.Parse(gopts.backends, s)
if err != nil {
return nil, err
}

cfg, err := parseConfig(loc, opts)
if err != nil {
return nil, err
}

rt, err := backend.Transport(globalOptions.TransportOptions)
if err != nil {
return nil, errors.Fatal(err.Error())
}

factory := gopts.backends.Lookup(loc.Scheme)
if factory == nil {
return nil, errors.Fatalf("invalid backend: %q", loc.Scheme)
}

be, err := factory.Create(ctx, cfg, rt, nil)
if err != nil {
return nil, err
}

return logger.New(sema.NewBackend(be)), nil
return innerOpen(ctx, s, gopts, opts, true)
}

0 comments on commit 3f9d508

Please sign in to comment.