7
7
"os"
8
8
"os/exec"
9
9
"path/filepath"
10
+ "sort"
10
11
"strings"
11
12
"time"
12
13
@@ -206,12 +207,57 @@ func (r *resticWrapper) runProfile() error {
206
207
return nil
207
208
}
208
209
209
- func (r *resticWrapper) prepareCommand(command string, args *shell.Args) shellCommandDefinition {
210
+ var commonResticArgsList = []string{
211
+ "--cacert",
212
+ "--cache-dir",
213
+ "--cleanup-cache",
214
+ "-h", "--help",
215
+ "--insecure-tls",
216
+ "--json",
217
+ "--key-hint",
218
+ "--limit-download",
219
+ "--limit-upload",
220
+ "--no-cache",
221
+ "-o", "--option",
222
+ "--password-command",
223
+ "-p", "--password-file",
224
+ "-q", "--quiet",
225
+ "-r", "--repo",
226
+ "--repository-file",
227
+ "--tls-client-cert",
228
+ "-v", "--verbose",
229
+ }
230
+
231
+ // commonResticArgs turns args into commonArgs containing only those args that all restic commands understand
232
+ func (r *resticWrapper) commonResticArgs(args []string) (commonArgs []string) {
233
+ if !sort.StringsAreSorted(commonResticArgsList) {
234
+ sort.Strings(commonResticArgsList)
235
+ }
236
+ skipValue := true
237
+
238
+ for _, arg := range args {
239
+ if strings.HasPrefix(arg, "-") {
240
+ lookup := strings.TrimSpace(strings.Split(arg, "=")[0])
241
+ index := sort.SearchStrings(commonResticArgsList, lookup)
242
+ if index < len(commonResticArgsList) && commonResticArgsList[index] == lookup {
243
+ commonArgs = append(commonArgs, arg)
244
+ skipValue = strings.Contains(arg, "=")
245
+ continue
246
+ }
247
+ } else if !skipValue {
248
+ commonArgs = append(commonArgs, arg)
249
+ }
250
+ skipValue = true
251
+ }
252
+ return
253
+ }
254
+
255
+ func (r *resticWrapper) prepareCommand(command string, args *shell.Args, moreArgs ...string) shellCommandDefinition {
210
256
// Create local instance to allow modification
211
257
args = args.Clone()
212
258
213
- if r.moreArgs != nil && len(r. moreArgs) > 0 {
214
- args.AddArgs(r. moreArgs, shell.ArgCommandLineEscape)
259
+ if len(moreArgs) > 0 {
260
+ args.AddArgs(moreArgs, shell.ArgCommandLineEscape)
215
261
}
216
262
217
263
// Special case for backup command
@@ -245,7 +291,7 @@ func (r *resticWrapper) prepareCommand(command string, args *shell.Args) shellCo
245
291
func (r *resticWrapper) runInitialize() error {
246
292
clog.Infof("profile '%s': initializing repository (if not existing)", r.profile.Name)
247
293
args := r.profile.GetCommandFlags(constants.CommandInit)
248
- rCommand := r.prepareCommand(constants.CommandInit, args)
294
+ rCommand := r.prepareCommand(constants.CommandInit, args, r.commonResticArgs(r.moreArgs)... )
249
295
// don't display any error
250
296
rCommand.stderr = nil
251
297
_, stderr, err := runShellCommand(rCommand)
@@ -262,7 +308,7 @@ func (r *resticWrapper) runInitializeCopy() error {
262
308
// the copy command adds a 2 behind each flag about the secondary repository
263
309
// in the case of init, we want to promote the secondary repository as primary
264
310
args.PromoteSecondaryToPrimary()
265
- rCommand := r.prepareCommand(constants.CommandInit, args)
311
+ rCommand := r.prepareCommand(constants.CommandInit, args, r.commonResticArgs(r.moreArgs)... )
266
312
// don't display any error
267
313
rCommand.stderr = nil
268
314
_, stderr, err := runShellCommand(rCommand)
@@ -276,7 +322,7 @@ func (r *resticWrapper) runCheck() error {
276
322
clog.Infof("profile '%s': checking repository consistency", r.profile.Name)
277
323
args := r.profile.GetCommandFlags(constants.CommandCheck)
278
324
for {
279
- rCommand := r.prepareCommand(constants.CommandCheck, args)
325
+ rCommand := r.prepareCommand(constants.CommandCheck, args, r.commonResticArgs(r.moreArgs)... )
280
326
summary, stderr, err := runShellCommand(rCommand)
281
327
r.executionTime += summary.Duration
282
328
r.summary(constants.CommandCheck, summary, stderr, err)
@@ -294,7 +340,7 @@ func (r *resticWrapper) runRetention() error {
294
340
clog.Infof("profile '%s': cleaning up repository using retention information", r.profile.Name)
295
341
args := r.profile.GetRetentionFlags()
296
342
for {
297
- rCommand := r.prepareCommand(constants.CommandForget, args)
343
+ rCommand := r.prepareCommand(constants.CommandForget, args, r.commonResticArgs(r.moreArgs)... )
298
344
summary, stderr, err := runShellCommand(rCommand)
299
345
r.executionTime += summary.Duration
300
346
r.summary(constants.SectionConfigurationRetention, summary, stderr, err)
@@ -312,7 +358,7 @@ func (r *resticWrapper) runCommand(command string) error {
312
358
clog.Infof("profile '%s': starting '%s'", r.profile.Name, command)
313
359
args := r.profile.GetCommandFlags(command)
314
360
for {
315
- rCommand := r.prepareCommand(command, args)
361
+ rCommand := r.prepareCommand(command, args, r.moreArgs... )
316
362
317
363
if command == constants.CommandBackup && len(r.progress) > 0 {
318
364
if r.profile.Backup != nil {
@@ -344,7 +390,7 @@ func (r *resticWrapper) runCommand(command string) error {
344
390
func (r *resticWrapper) runUnlock() error {
345
391
clog.Infof("profile '%s': unlock stale locks", r.profile.Name)
346
392
args := r.profile.GetCommandFlags(constants.CommandUnlock)
347
- rCommand := r.prepareCommand(constants.CommandUnlock, args)
393
+ rCommand := r.prepareCommand(constants.CommandUnlock, args, r.commonResticArgs(r.moreArgs)... )
348
394
summary, stderr, err := runShellCommand(rCommand)
349
395
r.executionTime += summary.Duration
350
396
r.summary(constants.CommandUnlock, summary, stderr, err)
0 commit comments