Skip to content

Commit bb254ea

Browse files
committed
Allow overriding "default-command" in profile
1 parent d59a1ff commit bb254ea

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

config/profile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Profile struct {
3131
PrometheusSaveToFile string `mapstructure:"prometheus-save-to-file"`
3232
PrometheusPush string `mapstructure:"prometheus-push"`
3333
PrometheusLabels map[string]string `mapstructure:"prometheus-labels"`
34+
DefaultCommand string `mapstructure:"default-command"`
3435
OtherFlags map[string]interface{} `mapstructure:",remain"`
3536
Environment map[string]ConfidentialValue `mapstructure:"env"`
3637
Backup *BackupSection `mapstructure:"backup"`

main.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,30 @@ func main() {
194194

195195
// The remaining arguments are going to be sent to the restic command line
196196
resticArguments := flags.resticArgs
197-
resticCommand := global.DefaultCommand
198-
if len(resticArguments) > 0 {
199-
resticCommand = resticArguments[0]
200-
resticArguments = resticArguments[1:]
201-
}
197+
resticCommand := func() func(profile string) string {
198+
if len(resticArguments) > 0 {
199+
command := resticArguments[0]
200+
resticArguments = resticArguments[1:]
201+
// Command specified in arguments list
202+
return func(profile string) string {
203+
return command
204+
}
205+
} else {
206+
// Default command (as defined in global or profile)
207+
return func(profile string) string {
208+
if c.HasProfile(profile) {
209+
if p, err := c.GetProfile(profile); err == nil && p.DefaultCommand != "" {
210+
return p.DefaultCommand
211+
}
212+
}
213+
return global.DefaultCommand
214+
}
215+
}
216+
}()
202217

203218
// resticprofile own commands (with configuration file)
204-
if isOwnCommand(resticCommand, true) {
205-
err = runOwnCommand(c, resticCommand, flags, resticArguments)
219+
if isOwnCommand(resticCommand(flags.name), true) {
220+
err = runOwnCommand(c, resticCommand(flags.name), flags, resticArguments)
206221
if err != nil {
207222
clog.Error(err)
208223
exitCode = 1
@@ -217,7 +232,7 @@ func main() {
217232
defer notifyStop()
218233

219234
// Single profile run
220-
err = runProfile(c, global, flags, flags.name, resticBinary, resticArguments, resticCommand, "")
235+
err = runProfile(c, global, flags, flags.name, resticBinary, resticArguments, resticCommand(flags.name), "")
221236
if err != nil {
222237
clog.Error(err)
223238
exitCode = 1
@@ -237,7 +252,7 @@ func main() {
237252

238253
for i, profileName := range group {
239254
clog.Debugf("[%d/%d] starting profile '%s' from group '%s'", i+1, len(group), profileName, flags.name)
240-
err = runProfile(c, global, flags, profileName, resticBinary, resticArguments, resticCommand, flags.name)
255+
err = runProfile(c, global, flags, profileName, resticBinary, resticArguments, resticCommand(profileName), flags.name)
241256
if err != nil {
242257
clog.Error(err)
243258
exitCode = 1

0 commit comments

Comments
 (0)