simplify options handling

This commit is contained in:
cupcakearmy 2021-10-31 23:07:12 +01:00
parent 27e82c8529
commit a68e3e426e
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
2 changed files with 15 additions and 8 deletions

View File

@ -55,6 +55,7 @@ func GetConfig() *Config {
config = &Config{} config = &Config{}
if err := viper.UnmarshalExact(config); err != nil { if err := viper.UnmarshalExact(config); err != nil {
colors.Error.Println(err)
colors.Error.Println("Could not parse config file!") colors.Error.Println("Could not parse config file!")
lock.Unlock() lock.Unlock()
os.Exit(1) os.Exit(1)
@ -273,3 +274,15 @@ func getOptions(options Options, key string) []string {
} }
return selected return selected
} }
func combineOptions(key string, l Location, b Backend) []string {
// Priority: location > backend > global
var options []string
gFlags := getOptions(GetConfig().Global, key)
bFlags := getOptions(b.Options, key)
lFlags := getOptions(l.Options, key)
options = append(options, gFlags...)
options = append(options, bFlags...)
options = append(options, lFlags...)
return options
}

View File

@ -184,11 +184,8 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
continue continue
} }
lFlags := getOptions(l.Options, "backup")
bFlags := getOptions(backend.Options, "backup")
cmd := []string{"backup"} cmd := []string{"backup"}
cmd = append(cmd, lFlags...) cmd = append(cmd, combineOptions("backup", l, backend)...)
cmd = append(cmd, bFlags...)
if cron { if cron {
cmd = append(cmd, "--tag", l.getTag("cron")) cmd = append(cmd, "--tag", l.getTag("cron"))
} }
@ -269,8 +266,6 @@ func (l Location) Forget(prune bool, dry bool) error {
options := ExecuteOptions{ options := ExecuteOptions{
Envs: env, Envs: env,
} }
lFlags := getOptions(l.Options, "forget")
bFlags := getOptions(backend.Options, "forget")
cmd := []string{"forget", "--tag", l.getLocationTag()} cmd := []string{"forget", "--tag", l.getLocationTag()}
if prune { if prune {
cmd = append(cmd, "--prune") cmd = append(cmd, "--prune")
@ -278,8 +273,7 @@ func (l Location) Forget(prune bool, dry bool) error {
if dry { if dry {
cmd = append(cmd, "--dry-run") cmd = append(cmd, "--dry-run")
} }
cmd = append(cmd, lFlags...) cmd = append(cmd, combineOptions("forget", l, backend)...)
cmd = append(cmd, bFlags...)
out, err := ExecuteResticCommand(options, cmd...) out, err := ExecuteResticCommand(options, cmd...)
if VERBOSE { if VERBOSE {
colors.Faint.Println(out) colors.Faint.Println(out)