From a394b675b0c1c231b29eb0bd769dbce6946b01e9 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Mon, 2 Jan 2017 11:14:22 -0700 Subject: [PATCH 1/2] CLI options now override env vars --- src/cmds/restic/global.go | 5 ++--- src/cmds/restic/main.go | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index 8b4cdd81d..d3382d625 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -10,8 +10,6 @@ import ( "strings" "syscall" - "github.com/spf13/cobra" - "restic/backend/local" "restic/backend/rest" "restic/backend/s3" @@ -27,7 +25,7 @@ import ( var version = "compiled manually" -func parseEnvironment(cmd *cobra.Command, args []string) { +func parseEnvironment() { repo := os.Getenv("RESTIC_REPOSITORY") if repo != "" { globalOptions.Repo = repo @@ -57,6 +55,7 @@ var globalOptions = GlobalOptions{ } func init() { + parseEnvironment() f := cmdRoot.PersistentFlags() f.StringVarP(&globalOptions.Repo, "repo", "r", "", "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/main.go b/src/cmds/restic/main.go index 2ad9ff260..fed711b9a 100644 --- a/src/cmds/restic/main.go +++ b/src/cmds/restic/main.go @@ -20,9 +20,8 @@ var cmdRoot = &cobra.Command{ restic is a backup program which allows saving multiple revisions of files and directories in an encrypted repository stored on different backends. `, - SilenceErrors: true, - SilenceUsage: true, - PersistentPreRun: parseEnvironment, + SilenceErrors: true, + SilenceUsage: true, } func init() { From 0a34a2d5d84c4fcb0d790186c72dfc9449a6216d Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Mon, 2 Jan 2017 12:21:30 -0700 Subject: [PATCH 2/2] Consider the environment --- src/cmds/restic/global.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index d3382d625..fdedeee9d 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -25,18 +25,6 @@ import ( var version = "compiled manually" -func parseEnvironment() { - repo := os.Getenv("RESTIC_REPOSITORY") - if repo != "" { - globalOptions.Repo = repo - } - - pw := os.Getenv("RESTIC_PASSWORD") - if pw != "" { - globalOptions.password = pw - } -} - // GlobalOptions hold all global options for restic. type GlobalOptions struct { Repo string @@ -55,9 +43,13 @@ var globalOptions = GlobalOptions{ } func init() { - parseEnvironment() + pw := os.Getenv("RESTIC_PASSWORD") + if pw != "" { + globalOptions.password = pw + } + f := cmdRoot.PersistentFlags() - f.StringVarP(&globalOptions.Repo, "repo", "r", "", "repository to backup to or restore from (default: $RESTIC_REPOSITORY)") + 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") f.BoolVarP(&globalOptions.Quiet, "quiet", "q", false, "do not outputcomprehensive progress report") f.BoolVar(&globalOptions.NoLock, "no-lock", false, "do not lock the repo, this allows some operations on read-only repos")