From 61673bd88b482f7534ba57a1dd785091d1f7c8d4 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 17 May 2021 22:34:14 +0200 Subject: [PATCH] allow options for backend --- internal/backend.go | 13 +++++++------ internal/config.go | 12 +++++++++++- internal/location.go | 23 ++++++++--------------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index 574b85e..18b635e 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -17,12 +17,13 @@ type BackendRest struct { } type Backend struct { - name string - Type string `yaml:"type,omitempty"` - Path string `yaml:"path,omitempty"` - Key string `yaml:"key,omitempty"` - Env map[string]string `yaml:"env,omitempty"` - Rest BackendRest `yaml:"rest,omitempty"` + name string + Type string `yaml:"type,omitempty"` + Path string `yaml:"path,omitempty"` + Key string `yaml:"key,omitempty"` + Env map[string]string `yaml:"env,omitempty"` + Rest BackendRest `yaml:"rest,omitempty"` + Options Options `yaml:"options,omitempty"` } func GetBackend(name string) (Backend, bool) { diff --git a/internal/config.go b/internal/config.go index 9823a48..3e74bf9 100644 --- a/internal/config.go +++ b/internal/config.go @@ -12,7 +12,7 @@ import ( "github.com/spf13/viper" ) -const VERSION = "1.1.0" +const VERSION = "1.1.1" var CI bool = false var VERBOSE bool = false @@ -220,3 +220,13 @@ func (c *Config) SaveConfig() error { return viper.WriteConfig() } + +func getOptions(options Options, key string) []string { + var selected []string + for k, values := range options[key] { + for _, value := range values { + selected = append(selected, fmt.Sprintf("--%s", k), value) + } + } + return selected +} diff --git a/internal/location.go b/internal/location.go index 2d16295..ab8c4f3 100644 --- a/internal/location.go +++ b/internal/location.go @@ -78,17 +78,6 @@ func (l Location) validate(c *Config) error { return nil } -func (l Location) getOptions(key string) []string { - var options []string - saved := l.Options[key] - for k, values := range saved { - for _, value := range values { - options = append(options, fmt.Sprintf("--%s", k), value) - } - } - return options -} - func ExecuteHooks(commands []string, options ExecuteOptions) error { if len(commands) == 0 { return nil @@ -164,9 +153,11 @@ func (l Location) Backup(cron bool) []error { continue } - flags := l.getOptions("backup") + lFlags := getOptions(l.Options, "backup") + bFlags := getOptions(backend.Options, "backup") cmd := []string{"backup"} - cmd = append(cmd, flags...) + cmd = append(cmd, lFlags...) + cmd = append(cmd, bFlags...) if cron { cmd = append(cmd, "--tag", "cron") } @@ -232,7 +223,8 @@ func (l Location) Forget(prune bool, dry bool) error { options := ExecuteOptions{ Envs: env, } - flags := l.getOptions("forget") + lFlags := getOptions(l.Options, "forget") + bFlags := getOptions(backend.Options, "forget") cmd := []string{"forget", "--path", path} if prune { cmd = append(cmd, "--prune") @@ -240,7 +232,8 @@ func (l Location) Forget(prune bool, dry bool) error { if dry { cmd = append(cmd, "--dry-run") } - cmd = append(cmd, flags...) + cmd = append(cmd, lFlags...) + cmd = append(cmd, bFlags...) out, err := ExecuteResticCommand(options, cmd...) if VERBOSE { colors.Faint.Println(out)