2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-24 11:55:28 +00:00

Add extended options via -o/--option

This commit is contained in:
Alexander Neumann 2017-03-25 15:33:52 +01:00
parent d0a5e86da1
commit 946b4f4b86
3 changed files with 26 additions and 7 deletions

View File

@ -27,7 +27,7 @@ func runInit(gopts GlobalOptions, args []string) error {
return errors.Fatal("Please specify repository location (-r)") return errors.Fatal("Please specify repository location (-r)")
} }
be, err := create(gopts.Repo) be, err := create(gopts.Repo, gopts.extended)
if err != nil { if err != nil {
return errors.Fatalf("create backend at %s failed: %v\n", gopts.Repo, err) return errors.Fatalf("create backend at %s failed: %v\n", gopts.Repo, err)
} }

View File

@ -17,6 +17,7 @@ import (
"restic/backend/s3" "restic/backend/s3"
"restic/backend/sftp" "restic/backend/sftp"
"restic/debug" "restic/debug"
"restic/options"
"restic/repository" "restic/repository"
"restic/errors" "restic/errors"
@ -38,6 +39,10 @@ type GlobalOptions struct {
password string password string
stdout io.Writer stdout io.Writer
stderr io.Writer stderr io.Writer
Options []string
extended options.Options
} }
var globalOptions = GlobalOptions{ 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.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.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() restoreTerminal()
} }
@ -287,7 +294,7 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) {
return nil, errors.Fatal("Please specify repository location (-r)") return nil, errors.Fatal("Please specify repository location (-r)")
} }
be, err := open(opts.Repo) be, err := open(opts.Repo, opts.extended)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -310,7 +317,7 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) {
} }
// Open the backend specified by a location config. // 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) debug.Log("parsing location %v", s)
loc, err := location.Parse(s) loc, err := location.Parse(s)
if err != nil { if err != nil {
@ -352,7 +359,7 @@ func open(s string) (restic.Backend, error) {
} }
// Create the backend specified by URI. // 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) debug.Log("parsing location %v", s)
loc, err := location.Parse(s) loc, err := location.Parse(s)
if err != nil { if err != nil {

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"restic" "restic"
"restic/debug" "restic/debug"
"restic/options"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -22,10 +23,21 @@ directories in an encrypted repository stored on different backends.
SilenceErrors: true, SilenceErrors: true,
SilenceUsage: true, SilenceUsage: true,
// run the debug functions for all subcommands (if build tag "debug" is
// enabled)
PersistentPreRunE: func(*cobra.Command, []string) error { 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) { PersistentPostRun: func(*cobra.Command, []string) {
shutdownDebug() shutdownDebug()