@@ -13,21 +13,20 @@ import (
13
13
)
14
14
15
15
// Mockings
16
- var mockExecCommand = exec .Command
17
16
var mockCmdRun = func (c * exec.Cmd ) error {
18
17
return c .Run ()
19
18
}
20
19
21
20
// Backup contains all necessary information for executing a configured backup.
22
21
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"`
31
30
logger * log.Logger
32
31
}
33
32
@@ -81,7 +80,7 @@ func (b *Backup) PrepareRun() error {
81
80
}
82
81
writer := io .MultiWriter (logfile )
83
82
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 )
85
84
err = mockCmdRun (cmd )
86
85
if err != nil {
87
86
b .logger .Printf ("chown for backup directory failed: %s" , err )
@@ -102,15 +101,37 @@ func (b *Backup) Run() error {
102
101
return fmt .Errorf ("device %s not found" , b .TargetDevice )
103
102
}
104
103
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 ], "." ) {
106
120
//The scriptPath is a relative path, from the place of the config, so use the config as base
107
121
log .Printf ("ERROR: Script path is relative, aborting." )
108
122
return fmt .Errorf ("script path is relative, aborting" )
109
123
}
110
- cmd := mockExecCommand ("/usr/bin/sh" , b .ScriptPath )
124
+ var cmd * exec.Cmd
125
+ var args []string
111
126
if b .ExeUser != "" {
112
127
// 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 ... )
114
135
}
115
136
b .logger .Printf ("Running backup script of '%s'" , b .Name )
116
137
b .logger .Printf ("Script is: %s" , b .ScriptPath )
0 commit comments