From 56836364a42911a85fba0f78535e60f0a326fd10 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Thu, 8 Jun 2023 13:11:34 +0200 Subject: [PATCH] backend: pass context into every backend constructor --- cmd/restic/global.go | 6 +++--- internal/backend/gs/gs.go | 2 +- internal/backend/gs/gs_test.go | 4 ++-- internal/backend/rclone/backend.go | 12 ++++++------ internal/backend/rclone/backend_test.go | 2 +- internal/backend/rclone/internal_test.go | 4 ++-- internal/backend/rest/rest.go | 4 ++-- internal/backend/rest/rest_int_test.go | 2 +- internal/backend/rest/rest_test.go | 2 +- 9 files changed, 19 insertions(+), 19 deletions(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 1b9c5b33d..3136e8990 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -584,7 +584,7 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio case "s3": be, err = s3.Open(ctx, *cfg.(*s3.Config), rt) case "gs": - be, err = gs.Open(*cfg.(*gs.Config), rt) + be, err = gs.Open(ctx, *cfg.(*gs.Config), rt) case "azure": be, err = azure.Open(ctx, *cfg.(*azure.Config), rt) case "swift": @@ -592,9 +592,9 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio case "b2": be, err = b2.Open(ctx, *cfg.(*b2.Config), rt) case "rest": - be, err = rest.Open(*cfg.(*rest.Config), rt) + be, err = rest.Open(ctx, *cfg.(*rest.Config), rt) case "rclone": - be, err = rclone.Open(*cfg.(*rclone.Config), lim) + be, err = rclone.Open(ctx, *cfg.(*rclone.Config), lim) default: return nil, errors.Fatalf("invalid backend: %q", loc.Scheme) diff --git a/internal/backend/gs/gs.go b/internal/backend/gs/gs.go index 7b5489111..022f2534a 100644 --- a/internal/backend/gs/gs.go +++ b/internal/backend/gs/gs.go @@ -117,7 +117,7 @@ func open(cfg Config, rt http.RoundTripper) (*Backend, error) { } // Open opens the gs backend at the specified bucket. -func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) { +func Open(_ context.Context, cfg Config, rt http.RoundTripper) (restic.Backend, error) { return open(cfg, rt) } diff --git a/internal/backend/gs/gs_test.go b/internal/backend/gs/gs_test.go index f96b6c62b..a73fe77fb 100644 --- a/internal/backend/gs/gs_test.go +++ b/internal/backend/gs/gs_test.go @@ -58,12 +58,12 @@ func newGSTestSuite(t testing.TB) *test.Suite[gs.Config] { // OpenFn is a function that opens a previously created temporary repository. Open: func(cfg gs.Config) (restic.Backend, error) { - return gs.Open(cfg, tr) + return gs.Open(context.TODO(), cfg, tr) }, // CleanupFn removes data created during the tests. Cleanup: func(cfg gs.Config) error { - be, err := gs.Open(cfg, tr) + be, err := gs.Open(context.TODO(), cfg, tr) if err != nil { return err } diff --git a/internal/backend/rclone/backend.go b/internal/backend/rclone/backend.go index 085c89945..881990bc3 100644 --- a/internal/backend/rclone/backend.go +++ b/internal/backend/rclone/backend.go @@ -134,7 +134,7 @@ func wrapConn(c *StdioConn, lim limiter.Limiter) *wrappedConn { } // New initializes a Backend and starts the process. -func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) { +func newBackend(ctx context.Context, cfg Config, lim limiter.Limiter) (*Backend, error) { var ( args []string err error @@ -197,7 +197,7 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) { wg: wg, } - ctx, cancel := context.WithCancel(context.Background()) + ctx, cancel := context.WithCancel(ctx) defer cancel() wg.Add(1) @@ -256,8 +256,8 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) { } // Open starts an rclone process with the given config. -func Open(cfg Config, lim limiter.Limiter) (*Backend, error) { - be, err := newBackend(cfg, lim) +func Open(ctx context.Context, cfg Config, lim limiter.Limiter) (*Backend, error) { + be, err := newBackend(ctx, cfg, lim) if err != nil { return nil, err } @@ -272,7 +272,7 @@ func Open(cfg Config, lim limiter.Limiter) (*Backend, error) { URL: url, } - restBackend, err := rest.Open(restConfig, debug.RoundTripper(be.tr)) + restBackend, err := rest.Open(ctx, restConfig, debug.RoundTripper(be.tr)) if err != nil { _ = be.Close() return nil, err @@ -284,7 +284,7 @@ func Open(cfg Config, lim limiter.Limiter) (*Backend, error) { // Create initializes a new restic repo with rclone. func Create(ctx context.Context, cfg Config) (*Backend, error) { - be, err := newBackend(cfg, nil) + be, err := newBackend(ctx, cfg, nil) if err != nil { return nil, err } diff --git a/internal/backend/rclone/backend_test.go b/internal/backend/rclone/backend_test.go index c497271f6..a562a99e6 100644 --- a/internal/backend/rclone/backend_test.go +++ b/internal/backend/rclone/backend_test.go @@ -39,7 +39,7 @@ func newTestSuite(t testing.TB) *test.Suite[rclone.Config] { // OpenFn is a function that opens a previously created temporary repository. Open: func(cfg rclone.Config) (restic.Backend, error) { t.Logf("Open()") - return rclone.Open(cfg, nil) + return rclone.Open(context.TODO(), cfg, nil) }, } } diff --git a/internal/backend/rclone/internal_test.go b/internal/backend/rclone/internal_test.go index bfec2b98c..32fe850a0 100644 --- a/internal/backend/rclone/internal_test.go +++ b/internal/backend/rclone/internal_test.go @@ -15,7 +15,7 @@ func TestRcloneExit(t *testing.T) { dir := rtest.TempDir(t) cfg := NewConfig() cfg.Remote = dir - be, err := Open(cfg, nil) + be, err := Open(context.TODO(), cfg, nil) var e *exec.Error if errors.As(err, &e) && e.Err == exec.ErrNotFound { t.Skipf("program %q not found", e.Name) @@ -45,7 +45,7 @@ func TestRcloneFailedStart(t *testing.T) { cfg := NewConfig() // exits with exit code 1 cfg.Program = "false" - _, err := Open(cfg, nil) + _, err := Open(context.TODO(), cfg, nil) var e *exec.ExitError if !errors.As(err, &e) { // unexpected error diff --git a/internal/backend/rest/rest.go b/internal/backend/rest/rest.go index 68397cd1b..0906a7f10 100644 --- a/internal/backend/rest/rest.go +++ b/internal/backend/rest/rest.go @@ -36,7 +36,7 @@ const ( ) // Open opens the REST backend with the given config. -func Open(cfg Config, rt http.RoundTripper) (*Backend, error) { +func Open(_ context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) { // use url without trailing slash for layout url := cfg.URL.String() if url[len(url)-1] == '/' { @@ -55,7 +55,7 @@ func Open(cfg Config, rt http.RoundTripper) (*Backend, error) { // Create creates a new REST on server configured in config. func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) { - be, err := Open(cfg, rt) + be, err := Open(ctx, cfg, rt) if err != nil { return nil, err } diff --git a/internal/backend/rest/rest_int_test.go b/internal/backend/rest/rest_int_test.go index 7184f5fbe..e7810c5e3 100644 --- a/internal/backend/rest/rest_int_test.go +++ b/internal/backend/rest/rest_int_test.go @@ -117,7 +117,7 @@ func TestListAPI(t *testing.T) { URL: srvURL, } - be, err := rest.Open(cfg, http.DefaultTransport) + be, err := rest.Open(context.TODO(), cfg, http.DefaultTransport) if err != nil { t.Fatal(err) } diff --git a/internal/backend/rest/rest_test.go b/internal/backend/rest/rest_test.go index 2ebd00f5e..4d069e63c 100644 --- a/internal/backend/rest/rest_test.go +++ b/internal/backend/rest/rest_test.go @@ -90,7 +90,7 @@ func newTestSuite(_ context.Context, t testing.TB, url *url.URL, minimalData boo // OpenFn is a function that opens a previously created temporary repository. Open: func(cfg rest.Config) (restic.Backend, error) { - return rest.Open(cfg, tr) + return rest.Open(context.TODO(), cfg, tr) }, // CleanupFn removes data created during the tests.