From a1440c819bd798c4d86b725bb3f77e743e128891 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Fri, 13 Nov 2015 12:33:59 +0100 Subject: [PATCH] Make NoLock a global option --- cmd/restic/cmd_check.go | 3 +-- cmd/restic/cmd_list.go | 4 +--- cmd/restic/cmd_restore.go | 3 +-- cmd/restic/global.go | 1 + cmd/restic/integration_test.go | 27 ++++----------------------- 5 files changed, 8 insertions(+), 30 deletions(-) diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index 3e437b315..c3ba1dd83 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -11,7 +11,6 @@ import ( type CmdCheck struct { ReadData bool `long:"read-data" default:"false" description:"Read data blobs"` CheckUnused bool `long:"check-unused" default:"false" description:"Check for unused blobs"` - NoLock bool `long:"no-lock" default:"false" description:"Do not lock repository, this allows checking read-only"` global *GlobalOptions } @@ -40,7 +39,7 @@ func (cmd CmdCheck) Execute(args []string) error { return err } - if !cmd.NoLock { + if !cmd.global.NoLock { cmd.global.Verbosef("Create exclusive lock for repository\n") lock, err := lockRepoExclusive(repo) defer unlockRepo(lock) diff --git a/cmd/restic/cmd_list.go b/cmd/restic/cmd_list.go index 6883eae61..fc13ff5a1 100644 --- a/cmd/restic/cmd_list.go +++ b/cmd/restic/cmd_list.go @@ -8,8 +8,6 @@ import ( ) type CmdList struct { - NoLock bool `long:"no-lock" default:"false" description:"Do not lock repository, this allows listing a read-only repo"` - global *GlobalOptions } @@ -37,7 +35,7 @@ func (cmd CmdList) Execute(args []string) error { return err } - if !cmd.NoLock { + if !cmd.global.NoLock { lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { diff --git a/cmd/restic/cmd_restore.go b/cmd/restic/cmd_restore.go index 389d0b5cc..0daf74966 100644 --- a/cmd/restic/cmd_restore.go +++ b/cmd/restic/cmd_restore.go @@ -13,7 +13,6 @@ type CmdRestore struct { Exclude []string `short:"e" long:"exclude" description:"Exclude a pattern (can be specified multiple times)"` Include []string `short:"i" long:"include" description:"Include a pattern, exclude everything else (can be specified multiple times)"` Target string `short:"t" long:"target" description:"Directory to restore to"` - NoLock bool ` long:"no-lock" default:"false" description:"Do not lock repository, this allows restoring a read-only repo"` global *GlobalOptions } @@ -54,7 +53,7 @@ func (cmd CmdRestore) Execute(args []string) error { return err } - if !cmd.NoLock { + if !cmd.global.NoLock { lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 7b59de9ed..7de028c3c 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -24,6 +24,7 @@ type GlobalOptions struct { Repo string `short:"r" long:"repo" description:"Repository directory to backup to/restore from"` CacheDir string ` long:"cache-dir" description:"Directory to use as a local cache"` Quiet bool `short:"q" long:"quiet" default:"false" description:"Do not output comprehensive progress report"` + NoLock bool ` long:"no-lock" default:"false" description:"Do not lock the repo, this allows some operations on read-only repos."` password string stdout io.Writer diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 21e118d60..bc734bf0a 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -73,20 +73,10 @@ func executeAndParseIDs(t testing.TB, cmd *CmdList, args ...string) backend.IDs return parseIDsFromReader(t, buf) } -func cmdListNoLock(t testing.TB, global GlobalOptions, tpe string) backend.IDs { - cmd := &CmdList{global: &global, NoLock: true} - return executeAndParseIDs(t, cmd, tpe) -} - func cmdRestore(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID) { cmdRestoreExcludes(t, global, dir, snapshotID, nil) } -func cmdRestoreNoLock(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID) { - cmd := &CmdRestore{global: &global, Target: dir, NoLock: true} - OK(t, cmd.Execute([]string{snapshotID.String()})) -} - func cmdRestoreExcludes(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID, excludes []string) { cmd := &CmdRestore{global: &global, Target: dir, Exclude: excludes} OK(t, cmd.Execute([]string{snapshotID.String()})) @@ -114,16 +104,6 @@ func cmdCheckOutput(t testing.TB, global GlobalOptions) string { return string(buf.Bytes()) } -func cmdCheckNoLock(t testing.TB, global GlobalOptions) { - cmd := &CmdCheck{ - global: &global, - ReadData: true, - CheckUnused: true, - NoLock: true, - } - OK(t, cmd.Execute(nil)) -} - func cmdRebuildIndex(t testing.TB, global GlobalOptions) { global.stdout = ioutil.Discard cmd := &CmdRebuildIndex{global: &global} @@ -823,13 +803,14 @@ func TestCheckRestoreNoLock(t *testing.T) { }) OK(t, err) - cmdCheckNoLock(t, global) + global.NoLock = true + cmdCheck(t, global) - snapshotIDs := cmdListNoLock(t, global, "snapshots") + snapshotIDs := cmdList(t, global, "snapshots") if len(snapshotIDs) == 0 { t.Fatalf("found no snapshots") } - cmdRestoreNoLock(t, global, filepath.Join(env.base, "restore"), snapshotIDs[0]) + cmdRestore(t, global, filepath.Join(env.base, "restore"), snapshotIDs[0]) }) }