From 946b4f4b86319b802d2c4f68df88400253a6fa59 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 25 Mar 2017 15:33:52 +0100 Subject: [PATCH] Add extended options via -o/--option --- src/cmds/restic/cmd_init.go | 2 +- src/cmds/restic/global.go | 13 ++++++++++--- src/cmds/restic/main.go | 18 +++++++++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/cmds/restic/cmd_init.go b/src/cmds/restic/cmd_init.go index d134e9376..ffa9cf272 100644 --- a/src/cmds/restic/cmd_init.go +++ b/src/cmds/restic/cmd_init.go @@ -27,7 +27,7 @@ func runInit(gopts GlobalOptions, args []string) error { return errors.Fatal("Please specify repository location (-r)") } - be, err := create(gopts.Repo) + be, err := create(gopts.Repo, gopts.extended) if err != nil { return errors.Fatalf("create backend at %s failed: %v\n", gopts.Repo, err) } diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index 8cfcedce7..d4ccfb5e7 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -17,6 +17,7 @@ import ( "restic/backend/s3" "restic/backend/sftp" "restic/debug" + "restic/options" "restic/repository" "restic/errors" @@ -38,6 +39,10 @@ type GlobalOptions struct { password string stdout io.Writer stderr io.Writer + + Options []string + + extended options.Options } var globalOptions = GlobalOptions{ @@ -65,6 +70,8 @@ func init() { f.BoolVar(&globalOptions.NoLock, "no-lock", false, "do not lock the repo, this allows some operations on read-only repos") f.BoolVarP(&globalOptions.JSON, "json", "", false, "set output mode to JSON for commands that support it") + f.StringSliceVarP(&globalOptions.Options, "option", "o", []string{}, "set extended option (`key=value`, can be specified multiple times)") + restoreTerminal() } @@ -287,7 +294,7 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) { return nil, errors.Fatal("Please specify repository location (-r)") } - be, err := open(opts.Repo) + be, err := open(opts.Repo, opts.extended) if err != nil { return nil, err } @@ -310,7 +317,7 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) { } // Open the backend specified by a location config. -func open(s string) (restic.Backend, error) { +func open(s string, opts options.Options) (restic.Backend, error) { debug.Log("parsing location %v", s) loc, err := location.Parse(s) if err != nil { @@ -352,7 +359,7 @@ func open(s string) (restic.Backend, error) { } // Create the backend specified by URI. -func create(s string) (restic.Backend, error) { +func create(s string, opts options.Options) (restic.Backend, error) { debug.Log("parsing location %v", s) loc, err := location.Parse(s) if err != nil { diff --git a/src/cmds/restic/main.go b/src/cmds/restic/main.go index 03f7f9035..96a508da9 100644 --- a/src/cmds/restic/main.go +++ b/src/cmds/restic/main.go @@ -5,6 +5,7 @@ import ( "os" "restic" "restic/debug" + "restic/options" "github.com/spf13/cobra" @@ -22,10 +23,21 @@ directories in an encrypted repository stored on different backends. SilenceErrors: true, SilenceUsage: true, - // run the debug functions for all subcommands (if build tag "debug" is - // enabled) PersistentPreRunE: func(*cobra.Command, []string) error { - return runDebug() + // parse extended options + opts, err := options.Parse(globalOptions.Options) + if err != nil { + return err + } + globalOptions.extended = opts + + // run the debug functions for all subcommands (if build tag "debug" is + // enabled) + if err := runDebug(); err != nil { + return err + } + + return nil }, PersistentPostRun: func(*cobra.Command, []string) { shutdownDebug()