Skip to content

Commit 6308eeb

Browse files
committed
Allow overriding "default-command" in profile
1 parent 245a440 commit 6308eeb

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
@@ -35,6 +35,7 @@ type Profile struct {
3535
PrometheusSaveToFile string `mapstructure:"prometheus-save-to-file"`
3636
PrometheusPush string `mapstructure:"prometheus-push"`
3737
PrometheusLabels map[string]string `mapstructure:"prometheus-labels"`
38+
DefaultCommand string `mapstructure:"default-command"`
3839
OtherFlags map[string]interface{} `mapstructure:",remain"`
3940
Environment map[string]ConfidentialValue `mapstructure:"env"`
4041
Backup *BackupSection `mapstructure:"backup"`

main.go

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

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

202217
// resticprofile own commands (with configuration file)
203-
if isOwnCommand(resticCommand, true) {
204-
err = runOwnCommand(c, resticCommand, flags, resticArguments)
218+
if isOwnCommand(resticCommand(flags.name), true) {
219+
err = runOwnCommand(c, resticCommand(flags.name), flags, resticArguments)
205220
if err != nil {
206221
clog.Error(err)
207222
exitCode = 1
@@ -216,7 +231,7 @@ func main() {
216231
defer notifyStop()
217232

218233
// Single profile run
219-
err = runProfile(c, global, flags, flags.name, resticBinary, resticArguments, resticCommand, "")
234+
err = runProfile(c, global, flags, flags.name, resticBinary, resticArguments, resticCommand(flags.name), "")
220235
if err != nil {
221236
clog.Error(err)
222237
exitCode = 1
@@ -236,7 +251,7 @@ func main() {
236251

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

0 commit comments

Comments
 (0)