From b4526c4e6e987a665b098fc18ced2e76077a1379 Mon Sep 17 00:00:00 2001 From: Pauline Middelink Date: Wed, 8 Mar 2017 20:12:16 +0100 Subject: [PATCH] Enable the use of `context` in restic Set up a cancelble context in global options, hook it into the ctrl-C handler for proper cancel propegation. Bump up minimal requirement for Go to version 1.7 in documentation and test-build files. --- .travis.yml | 3 --- CONTRIBUTING.md | 2 +- README.md | 2 +- Vagrantfile | 2 +- doc/Manual.md | 2 +- src/cmds/restic/global.go | 9 +++++++++ src/cmds/restic/integration_helpers_test.go | 2 ++ 7 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index f2d6b6bb9..570423172 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: go sudo: false go: - - 1.6.4 - 1.7.5 - 1.8 - tip @@ -17,8 +16,6 @@ env: matrix: exclude: - - os: osx - go: 1.6.4 - os: osx go: 1.7.5 - os: osx diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5529ea7f7..5ffdc40d5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -77,7 +77,7 @@ Just clone the repository, `cd` to it and run `gb build` to build the binary: [...] $ bin/restic version restic compiled manually - compiled at unknown time with go1.6 + compiled at unknown time with go1.7 The following commands can be used to run all the tests: diff --git a/README.md b/README.md index cc90f7b6c..3fe4c0edb 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ You can download the latest pre-compiled binary from the [restic release page](h Build restic ============ -Install Go/Golang (at least version 1.6), then run `go run build.go`, +Install Go/Golang (at least version 1.7), then run `go run build.go`, afterwards you'll find the binary in the current directory: $ go run build.go diff --git a/Vagrantfile b/Vagrantfile index a26aa6b61..ee872b3f4 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,7 +1,7 @@ # -*- mode: ruby -*- # vi: set ft=ruby : -GO_VERSION = '1.6' +GO_VERSION = '1.7' def packages_freebsd return <<-EOF diff --git a/doc/Manual.md b/doc/Manual.md index 513ef612c..5f2a470ff 100644 --- a/doc/Manual.md +++ b/doc/Manual.md @@ -27,7 +27,7 @@ $ pacaur -S restic-git # Building restic -restic is written in the Go programming language and you need at least Go version 1.6. +restic is written in the Go programming language and you need at least Go version 1.7. Building restic may also work with older versions of Go, but that's not supported. See the [Getting started](https://golang.org/doc/install) guide of the Go project for instructions how to install Go. diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index 02a2e9b52..4acd79069 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "io" "io/ioutil" @@ -33,6 +34,7 @@ type GlobalOptions struct { NoLock bool JSON bool + ctx context.Context password string stdout io.Writer stderr io.Writer @@ -49,6 +51,13 @@ func init() { globalOptions.password = pw } + var cancel context.CancelFunc + globalOptions.ctx, cancel = context.WithCancel(context.Background()) + AddCleanupHandler(func() error { + cancel() + return nil + }) + f := cmdRoot.PersistentFlags() f.StringVarP(&globalOptions.Repo, "repo", "r", os.Getenv("RESTIC_REPOSITORY"), "repository to backup to or restore from (default: $RESTIC_REPOSITORY)") f.StringVarP(&globalOptions.PasswordFile, "password-file", "p", "", "read the repository password from a file") diff --git a/src/cmds/restic/integration_helpers_test.go b/src/cmds/restic/integration_helpers_test.go index c71b12067..ad6acc8a1 100644 --- a/src/cmds/restic/integration_helpers_test.go +++ b/src/cmds/restic/integration_helpers_test.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "io/ioutil" "os" @@ -194,6 +195,7 @@ func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions)) gopts := GlobalOptions{ Repo: env.repo, Quiet: true, + ctx: context.Background(), password: TestPassword, stdout: os.Stdout, stderr: os.Stderr,