@@ -26,8 +26,7 @@ type Repo struct {
26
26
cmd string
27
27
uri string
28
28
29
- extraArgs []string
30
- extraEnv []string
29
+ opts []GenericOption
31
30
32
31
exists error
33
32
checkExists sync.Once
@@ -37,35 +36,29 @@ type Repo struct {
37
36
38
37
// NewRepo instantiates a new repository.
39
38
func NewRepo (resticBin string , uri string , opts ... GenericOption ) * Repo {
40
- opt := & GenericOpts {}
41
- for _ , o := range opts {
42
- o (opt )
43
- }
44
-
45
- opt .extraEnv = append (opt .extraEnv , "RESTIC_REPOSITORY=" + uri )
39
+ opts = append (opts , WithEnv ("RESTIC_REPOSITORY=" + uri ))
46
40
47
41
return & Repo {
48
- cmd : resticBin , // TODO: configurable binary path
49
- uri : uri ,
50
- extraArgs : opt .extraArgs ,
51
- extraEnv : opt .extraEnv ,
42
+ cmd : resticBin ,
43
+ uri : uri ,
44
+ opts : opts ,
52
45
}
53
46
}
54
47
55
48
func (r * Repo ) commandWithContext (ctx context.Context , args []string , opts ... GenericOption ) * exec.Cmd {
56
- opt := resolveOpts (opts )
49
+ opt := & GenericOpts {}
50
+ resolveOpts (opt , r .opts )
51
+ resolveOpts (opt , opts )
57
52
58
53
fullCmd := append ([]string {r .cmd }, args ... )
59
54
60
55
if len (opt .prefixCmd ) > 0 {
61
56
fullCmd = append (slices .Clone (opt .prefixCmd ), fullCmd ... )
62
57
}
63
58
64
- fullCmd = append (fullCmd , r .extraArgs ... )
65
59
fullCmd = append (fullCmd , opt .extraArgs ... )
66
60
67
61
cmd := exec .CommandContext (ctx , fullCmd [0 ], fullCmd [1 :]... )
68
- cmd .Env = append (cmd .Env , r .extraEnv ... )
69
62
cmd .Env = append (cmd .Env , opt .extraEnv ... )
70
63
71
64
logger := LoggerFromContext (ctx )
@@ -76,7 +69,7 @@ func (r *Repo) commandWithContext(ctx context.Context, args []string, opts ...Ge
76
69
}
77
70
78
71
if logger := LoggerFromContext (ctx ); logger != nil {
79
- fmt .Fprintf (logger , "\n command: %v %v\n " , r . cmd , strings .Join (args , " " ))
72
+ fmt .Fprintf (logger , "\n command: %v %v\n " , fullCmd [ 0 ] , strings .Join (fullCmd [ 1 :] , " " ))
80
73
}
81
74
82
75
return cmd
@@ -434,12 +427,10 @@ type GenericOpts struct {
434
427
prefixCmd []string
435
428
}
436
429
437
- func resolveOpts (opts []GenericOption ) * GenericOpts {
438
- opt := & GenericOpts {}
430
+ func resolveOpts (opt * GenericOpts , opts []GenericOption ) {
439
431
for _ , o := range opts {
440
432
o (opt )
441
433
}
442
- return opt
443
434
}
444
435
445
436
type GenericOption func (opts * GenericOpts )
@@ -487,8 +478,8 @@ func WithEnviron() GenericOption {
487
478
return WithEnv (os .Environ ()... )
488
479
}
489
480
490
- func WithPrefixCommand (proc string , args ... string ) GenericOption {
481
+ func WithPrefixCommand (args ... string ) GenericOption {
491
482
return func (opts * GenericOpts ) {
492
- opts .prefixCmd = append ([] string { proc } , args ... )
483
+ opts .prefixCmd = append (opts . prefixCmd , args ... )
493
484
}
494
485
}
0 commit comments