From 1695c8ed55a220e960a8a27c36ede3b33d9d31f3 Mon Sep 17 00:00:00 2001 From: George Armhold Date: Wed, 6 Dec 2017 07:02:55 -0500 Subject: [PATCH] use global context for check, debug, dump, find, forget, init, key, list, mount, tag, unlock commands gh-1434 --- cmd/restic/cmd_check.go | 9 ++++----- cmd/restic/cmd_debug.go | 2 +- cmd/restic/cmd_dump.go | 2 +- cmd/restic/cmd_find.go | 14 +++++++------- cmd/restic/cmd_forget.go | 4 ++-- cmd/restic/cmd_init.go | 4 +--- cmd/restic/cmd_key.go | 12 ++++++------ cmd/restic/cmd_list.go | 5 ++--- cmd/restic/cmd_mount.go | 5 ++--- cmd/restic/cmd_tag.go | 10 +++++----- cmd/restic/cmd_unlock.go | 4 +--- cmd/restic/global.go | 2 +- internal/backend/b2/b2.go | 2 +- 13 files changed, 34 insertions(+), 41 deletions(-) diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index e1fd1208f..9be7c2ee2 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -1,7 +1,6 @@ package main import ( - "context" "fmt" "os" "time" @@ -104,7 +103,7 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { chkr := checker.New(repo) Verbosef("load indexes\n") - hints, errs := chkr.LoadIndex(context.TODO()) + hints, errs := chkr.LoadIndex(gopts.ctx) dupFound := false for _, hint := range hints { @@ -129,7 +128,7 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { errChan := make(chan error) Verbosef("check all packs\n") - go chkr.Packs(context.TODO(), errChan) + go chkr.Packs(gopts.ctx, errChan) for err := range errChan { errorsFound = true @@ -138,7 +137,7 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { Verbosef("check snapshots, trees and blobs\n") errChan = make(chan error) - go chkr.Structure(context.TODO(), errChan) + go chkr.Structure(gopts.ctx, errChan) for err := range errChan { errorsFound = true @@ -165,7 +164,7 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { p := newReadProgress(gopts, restic.Stat{Blobs: chkr.CountPacks()}) errChan := make(chan error) - go chkr.ReadData(context.TODO(), p, errChan) + go chkr.ReadData(gopts.ctx, p, errChan) for err := range errChan { errorsFound = true diff --git a/cmd/restic/cmd_debug.go b/cmd/restic/cmd_debug.go index 6a06e96ed..9ac7abad3 100644 --- a/cmd/restic/cmd_debug.go +++ b/cmd/restic/cmd_debug.go @@ -183,7 +183,7 @@ func runDebugDump(gopts GlobalOptions, args []string) error { } } - err = repo.LoadIndex(context.TODO()) + err = repo.LoadIndex(gopts.ctx) if err != nil { return err } diff --git a/cmd/restic/cmd_dump.go b/cmd/restic/cmd_dump.go index 7d07552d2..eb8250979 100644 --- a/cmd/restic/cmd_dump.go +++ b/cmd/restic/cmd_dump.go @@ -162,7 +162,7 @@ func runDump(opts DumpOptions, gopts GlobalOptions, args []string) error { } } - sn, err := restic.LoadSnapshot(context.TODO(), repo, id) + sn, err := restic.LoadSnapshot(gopts.ctx, repo, id) if err != nil { Exitf(2, "loading snapshot %q failed: %v", snapshotIDString, err) } diff --git a/cmd/restic/cmd_find.go b/cmd/restic/cmd_find.go index 16e631ef9..6ec8f116f 100644 --- a/cmd/restic/cmd_find.go +++ b/cmd/restic/cmd_find.go @@ -180,7 +180,7 @@ type Finder struct { notfound restic.IDSet } -func (f *Finder) findInTree(treeID restic.ID, prefix string) error { +func (f *Finder) findInTree(ctx context.Context, treeID restic.ID, prefix string) error { if f.notfound.Has(treeID) { debug.Log("%v skipping tree %v, has already been checked", prefix, treeID.Str()) return nil @@ -188,7 +188,7 @@ func (f *Finder) findInTree(treeID restic.ID, prefix string) error { debug.Log("%v checking tree %v\n", prefix, treeID.Str()) - tree, err := f.repo.LoadTree(context.TODO(), treeID) + tree, err := f.repo.LoadTree(ctx, treeID) if err != nil { return err } @@ -224,7 +224,7 @@ func (f *Finder) findInTree(treeID restic.ID, prefix string) error { } if node.Type == "dir" { - if err := f.findInTree(*node.Subtree, filepath.Join(prefix, node.Name)); err != nil { + if err := f.findInTree(ctx, *node.Subtree, filepath.Join(prefix, node.Name)); err != nil { return err } } @@ -237,11 +237,11 @@ func (f *Finder) findInTree(treeID restic.ID, prefix string) error { return nil } -func (f *Finder) findInSnapshot(sn *restic.Snapshot) error { +func (f *Finder) findInSnapshot(ctx context.Context, sn *restic.Snapshot) error { debug.Log("searching in snapshot %s\n for entries within [%s %s]", sn.ID(), f.pat.oldest, f.pat.newest) f.out.newsn = sn - if err := f.findInTree(*sn.Tree, string(filepath.Separator)); err != nil { + if err := f.findInTree(ctx, *sn.Tree, string(filepath.Separator)); err != nil { return err } return nil @@ -284,7 +284,7 @@ func runFind(opts FindOptions, gopts GlobalOptions, args []string) error { } } - if err = repo.LoadIndex(context.TODO()); err != nil { + if err = repo.LoadIndex(gopts.ctx); err != nil { return err } @@ -298,7 +298,7 @@ func runFind(opts FindOptions, gopts GlobalOptions, args []string) error { notfound: restic.NewIDSet(), } for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, opts.Snapshots) { - if err = f.findInSnapshot(sn); err != nil { + if err = f.findInSnapshot(ctx, sn); err != nil { return err } } diff --git a/cmd/restic/cmd_forget.go b/cmd/restic/cmd_forget.go index 128b73905..00b573a0f 100644 --- a/cmd/restic/cmd_forget.go +++ b/cmd/restic/cmd_forget.go @@ -125,7 +125,7 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error { // When explicit snapshots args are given, remove them immediately. if !opts.DryRun { h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()} - if err = repo.Backend().Remove(context.TODO(), h); err != nil { + if err = repo.Backend().Remove(gopts.ctx, h); err != nil { return err } Verbosef("removed snapshot %v\n", sn.ID().Str()) @@ -223,7 +223,7 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error { if !opts.DryRun { for _, sn := range remove { h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()} - err = repo.Backend().Remove(context.TODO(), h) + err = repo.Backend().Remove(gopts.ctx, h) if err != nil { return err } diff --git a/cmd/restic/cmd_init.go b/cmd/restic/cmd_init.go index d4bcd9a0e..ed349bb71 100644 --- a/cmd/restic/cmd_init.go +++ b/cmd/restic/cmd_init.go @@ -1,8 +1,6 @@ package main import ( - "context" - "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/repository" @@ -44,7 +42,7 @@ func runInit(gopts GlobalOptions, args []string) error { s := repository.New(be) - err = s.Init(context.TODO(), gopts.password) + err = s.Init(gopts.ctx, gopts.password) if err != nil { return errors.Fatalf("create key in backend at %s failed: %v\n", gopts.Repo, err) } diff --git a/cmd/restic/cmd_key.go b/cmd/restic/cmd_key.go index 1fbbd0f3f..e89a69b63 100644 --- a/cmd/restic/cmd_key.go +++ b/cmd/restic/cmd_key.go @@ -76,7 +76,7 @@ func addKey(gopts GlobalOptions, repo *repository.Repository) error { return err } - id, err := repository.AddKey(context.TODO(), repo, pw, repo.Key()) + id, err := repository.AddKey(gopts.ctx, repo, pw, repo.Key()) if err != nil { return errors.Fatalf("creating new key failed: %v\n", err) } @@ -86,13 +86,13 @@ func addKey(gopts GlobalOptions, repo *repository.Repository) error { return nil } -func deleteKey(repo *repository.Repository, name string) error { +func deleteKey(ctx context.Context, repo *repository.Repository, name string) error { if name == repo.KeyName() { return errors.Fatal("refusing to remove key currently used to access repository") } h := restic.Handle{Type: restic.KeyFile, Name: name} - err := repo.Backend().Remove(context.TODO(), h) + err := repo.Backend().Remove(ctx, h) if err != nil { return err } @@ -107,13 +107,13 @@ func changePassword(gopts GlobalOptions, repo *repository.Repository) error { return err } - id, err := repository.AddKey(context.TODO(), repo, pw, repo.Key()) + id, err := repository.AddKey(gopts.ctx, repo, pw, repo.Key()) if err != nil { return errors.Fatalf("creating new key failed: %v\n", err) } h := restic.Handle{Type: restic.KeyFile, Name: repo.KeyName()} - err = repo.Backend().Remove(context.TODO(), h) + err = repo.Backend().Remove(gopts.ctx, h) if err != nil { return err } @@ -165,7 +165,7 @@ func runKey(gopts GlobalOptions, args []string) error { return err } - return deleteKey(repo, id) + return deleteKey(gopts.ctx, repo, id) case "passwd": lock, err := lockRepoExclusive(repo) defer unlockRepo(lock) diff --git a/cmd/restic/cmd_list.go b/cmd/restic/cmd_list.go index ea8fb3876..0a7e9ca01 100644 --- a/cmd/restic/cmd_list.go +++ b/cmd/restic/cmd_list.go @@ -1,7 +1,6 @@ package main import ( - "context" "fmt" "github.com/restic/restic/internal/errors" @@ -58,7 +57,7 @@ func runList(opts GlobalOptions, args []string) error { case "locks": t = restic.LockFile case "blobs": - idx, err := index.Load(context.TODO(), repo, nil) + idx, err := index.Load(opts.ctx, repo, nil) if err != nil { return err } @@ -74,7 +73,7 @@ func runList(opts GlobalOptions, args []string) error { return errors.Fatal("invalid type") } - for id := range repo.List(context.TODO(), t) { + for id := range repo.List(opts.ctx, t) { Printf("%s\n", id) } diff --git a/cmd/restic/cmd_mount.go b/cmd/restic/cmd_mount.go index b38d13cdb..5f57efa9c 100644 --- a/cmd/restic/cmd_mount.go +++ b/cmd/restic/cmd_mount.go @@ -4,7 +4,6 @@ package main import ( - "context" "os" "github.com/spf13/cobra" @@ -73,7 +72,7 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error { return err } - err = repo.LoadIndex(context.TODO()) + err = repo.LoadIndex(gopts.ctx) if err != nil { return err } @@ -114,7 +113,7 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error { Tags: opts.Tags, Paths: opts.Paths, } - root, err := fuse.NewRoot(context.TODO(), repo, cfg) + root, err := fuse.NewRoot(gopts.ctx, repo, cfg) if err != nil { return err } diff --git a/cmd/restic/cmd_tag.go b/cmd/restic/cmd_tag.go index cb098d61e..e38125cd1 100644 --- a/cmd/restic/cmd_tag.go +++ b/cmd/restic/cmd_tag.go @@ -53,7 +53,7 @@ func init() { tagFlags.StringArrayVar(&tagOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot-ID is given") } -func changeTags(repo *repository.Repository, sn *restic.Snapshot, setTags, addTags, removeTags []string) (bool, error) { +func changeTags(ctx context.Context, repo *repository.Repository, sn *restic.Snapshot, setTags, addTags, removeTags []string) (bool, error) { var changed bool if len(setTags) != 0 { @@ -77,20 +77,20 @@ func changeTags(repo *repository.Repository, sn *restic.Snapshot, setTags, addTa } // Save the new snapshot. - id, err := repo.SaveJSONUnpacked(globalOptions.ctx, restic.SnapshotFile, sn) + id, err := repo.SaveJSONUnpacked(ctx, restic.SnapshotFile, sn) if err != nil { return false, err } debug.Log("new snapshot saved as %v", id.Str()) - if err = repo.Flush(globalOptions.ctx); err != nil { + if err = repo.Flush(ctx); err != nil { return false, err } // Remove the old snapshot. h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()} - if err = repo.Backend().Remove(context.TODO(), h); err != nil { + if err = repo.Backend().Remove(ctx, h); err != nil { return false, err } @@ -125,7 +125,7 @@ func runTag(opts TagOptions, gopts GlobalOptions, args []string) error { ctx, cancel := context.WithCancel(gopts.ctx) defer cancel() for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, args) { - changed, err := changeTags(repo, sn, opts.SetTags, opts.AddTags, opts.RemoveTags) + changed, err := changeTags(ctx, repo, sn, opts.SetTags, opts.AddTags, opts.RemoveTags) if err != nil { Warnf("unable to modify the tags for snapshot ID %q, ignoring: %v\n", sn.ID(), err) continue diff --git a/cmd/restic/cmd_unlock.go b/cmd/restic/cmd_unlock.go index 5297b0e4e..4b216905d 100644 --- a/cmd/restic/cmd_unlock.go +++ b/cmd/restic/cmd_unlock.go @@ -1,8 +1,6 @@ package main import ( - "context" - "github.com/restic/restic/internal/restic" "github.com/spf13/cobra" ) @@ -43,7 +41,7 @@ func runUnlock(opts UnlockOptions, gopts GlobalOptions) error { fn = restic.RemoveAllLocks } - err = fn(context.TODO(), repo) + err = fn(gopts.ctx, repo) if err != nil { return err } diff --git a/cmd/restic/global.go b/cmd/restic/global.go index b2d7fcb1f..f5f78e279 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -344,7 +344,7 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) { return nil, err } - err = s.SearchKey(context.TODO(), opts.password, maxKeys) + err = s.SearchKey(opts.ctx, opts.password, maxKeys) if err != nil { return nil, err } diff --git a/internal/backend/b2/b2.go b/internal/backend/b2/b2.go index 3e0c28c71..f24b8ec98 100644 --- a/internal/backend/b2/b2.go +++ b/internal/backend/b2/b2.go @@ -115,7 +115,7 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backe sem: sem, } - present, err := be.Test(context.TODO(), restic.Handle{Type: restic.ConfigFile}) + present, err := be.Test(ctx, restic.Handle{Type: restic.ConfigFile}) if err != nil { return nil, err }