Skip to content

Commit 5b092b1

Browse files
committed
Added a possibility to have scripts with arguments
1 parent 7cd0cee commit 5b092b1

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

backup.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@ import (
1313
)
1414

1515
// Mockings
16-
var mockExecCommand = exec.Command
1716
var mockCmdRun = func(c *exec.Cmd) error {
1817
return c.Run()
1918
}
2019

2120
// Backup contains all necessary information for executing a configured backup.
2221
type Backup struct {
23-
Name string `mapstructure:",omitempty"`
24-
TargetDevice string `mapstructure:"targetDevice"`
25-
TargetPath string `mapstructure:"targetPath"`
26-
SourcePath string `mapstructure:"sourcePath"`
27-
ScriptPath string `mapstructure:"scriptPath"`
28-
Frequency int `mapstructure:"frequency"`
29-
ExeUser string `mapstructure:"user,omitempty"`
30-
Label string `mapstructure:"label,omitempty"`
22+
Name string `mapstructure:",omitempty"`
23+
TargetDevice string `mapstructure:"targetDevice"`
24+
TargetPath string `mapstructure:"targetPath"`
25+
SourcePath string `mapstructure:"sourcePath"`
26+
ScriptPath interface{} `mapstructure:"scriptPath"`
27+
Frequency int `mapstructure:"frequency"`
28+
ExeUser string `mapstructure:"user,omitempty"`
29+
Label string `mapstructure:"label,omitempty"`
3130
logger *log.Logger
3231
}
3332

@@ -81,7 +80,7 @@ func (b *Backup) PrepareRun() error {
8180
}
8281
writer := io.MultiWriter(logfile)
8382
b.logger = log.New(writer, b.Name, log.LstdFlags)
84-
cmd := mockExecCommand("chown", "-R", b.ExeUser, backupPath)
83+
cmd := exec.Command("chown", "-R", b.ExeUser, backupPath)
8584
err = mockCmdRun(cmd)
8685
if err != nil {
8786
b.logger.Printf("chown for backup directory failed: %s", err)
@@ -102,15 +101,37 @@ func (b *Backup) Run() error {
102101
return fmt.Errorf("device %s not found", b.TargetDevice)
103102
}
104103
if ok && dev.IsMounted() {
105-
if !strings.ContainsAny(b.ScriptPath, "/") || strings.HasPrefix(b.ScriptPath, ".") {
104+
var scriptWArgs []string
105+
switch slice := b.ScriptPath.(type) {
106+
case []interface{}:
107+
for _, v := range slice {
108+
scriptWArgs = append(scriptWArgs, v.(string))
109+
}
110+
case []string:
111+
for _, v := range slice {
112+
scriptWArgs = append(scriptWArgs, v)
113+
}
114+
case string:
115+
scriptWArgs = append(scriptWArgs, slice)
116+
default:
117+
log.Print("Fuck, the var is nothing we predicted...")
118+
}
119+
if !strings.ContainsAny(scriptWArgs[0], "/") || strings.HasPrefix(scriptWArgs[0], ".") {
106120
//The scriptPath is a relative path, from the place of the config, so use the config as base
107121
log.Printf("ERROR: Script path is relative, aborting.")
108122
return fmt.Errorf("script path is relative, aborting")
109123
}
110-
cmd := mockExecCommand("/usr/bin/sh", b.ScriptPath)
124+
var cmd *exec.Cmd
125+
var args []string
111126
if b.ExeUser != "" {
112127
// setup script environment including user to use
113-
cmd = mockExecCommand("sudo", "-E", "-u", b.ExeUser, "/usr/bin/sh", b.ScriptPath)
128+
args = []string{"-E", "-u", b.ExeUser, "/usr/bin/sh"}
129+
args = append(args, scriptWArgs...)
130+
cmd = exec.Command("sudo", args...)
131+
} else {
132+
args = []string{}
133+
args = append(args, scriptWArgs...)
134+
cmd = exec.Command("/usr/bin/sh", args...)
114135
}
115136
b.logger.Printf("Running backup script of '%s'", b.Name)
116137
b.logger.Printf("Script is: %s", b.ScriptPath)

0 commit comments

Comments
 (0)