From 6308eebc5aba6c95f105ca7c2cda4f9b812104d7 Mon Sep 17 00:00:00 2001 From: Juergen Kellerer Date: Fri, 13 Aug 2021 21:29:38 +0200 Subject: [PATCH] Allow overriding "default-command" in profile --- config/profile.go | 1 + main.go | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/config/profile.go b/config/profile.go index a9a5dbba1..8a9fdafb4 100644 --- a/config/profile.go +++ b/config/profile.go @@ -35,6 +35,7 @@ type Profile struct { PrometheusSaveToFile string `mapstructure:"prometheus-save-to-file"` PrometheusPush string `mapstructure:"prometheus-push"` PrometheusLabels map[string]string `mapstructure:"prometheus-labels"` + DefaultCommand string `mapstructure:"default-command"` OtherFlags map[string]interface{} `mapstructure:",remain"` Environment map[string]ConfidentialValue `mapstructure:"env"` Backup *BackupSection `mapstructure:"backup"` diff --git a/main.go b/main.go index a0361d2c8..d60a76144 100644 --- a/main.go +++ b/main.go @@ -193,15 +193,30 @@ func main() { // The remaining arguments are going to be sent to the restic command line resticArguments := flags.resticArgs - resticCommand := global.DefaultCommand - if len(resticArguments) > 0 { - resticCommand = resticArguments[0] - resticArguments = resticArguments[1:] - } + resticCommand := func() func(profile string) string { + if len(resticArguments) > 0 { + command := resticArguments[0] + resticArguments = resticArguments[1:] + // Command specified in arguments list + return func(profile string) string { + return command + } + } else { + // Default command (as defined in global or profile) + return func(profile string) string { + if c.HasProfile(profile) { + if p, err := c.GetProfile(profile); err == nil && p.DefaultCommand != "" { + return p.DefaultCommand + } + } + return global.DefaultCommand + } + } + }() // resticprofile own commands (with configuration file) - if isOwnCommand(resticCommand, true) { - err = runOwnCommand(c, resticCommand, flags, resticArguments) + if isOwnCommand(resticCommand(flags.name), true) { + err = runOwnCommand(c, resticCommand(flags.name), flags, resticArguments) if err != nil { clog.Error(err) exitCode = 1 @@ -216,7 +231,7 @@ func main() { defer notifyStop() // Single profile run - err = runProfile(c, global, flags, flags.name, resticBinary, resticArguments, resticCommand, "") + err = runProfile(c, global, flags, flags.name, resticBinary, resticArguments, resticCommand(flags.name), "") if err != nil { clog.Error(err) exitCode = 1 @@ -236,7 +251,7 @@ func main() { for i, profileName := range group { clog.Debugf("[%d/%d] starting profile '%s' from group '%s'", i+1, len(group), profileName, flags.name) - err = runProfile(c, global, flags, profileName, resticBinary, resticArguments, resticCommand, flags.name) + err = runProfile(c, global, flags, profileName, resticBinary, resticArguments, resticCommand(profileName), flags.name) if err != nil { clog.Error(err) exitCode = 1