Skip to content

Commit

Permalink
Allow overriding "default-command" in profile
Browse files Browse the repository at this point in the history
  • Loading branch information
jkellerer committed Aug 13, 2021
1 parent d59a1ff commit bb254ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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"`
Expand Down
33 changes: 24 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,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
Expand All @@ -217,7 +232,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
Expand All @@ -237,7 +252,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
Expand Down

0 comments on commit bb254ea

Please sign in to comment.