Skip to content

Commit c49e745

Browse files
committed
plumb prefixes to restic pkg correctly
1 parent c0ff21f commit c49e745

File tree

4 files changed

+20
-30
lines changed

4 files changed

+20
-30
lines changed

internal/orchestrator/logging/logging.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func ContextWithWriter(ctx context.Context, logger io.Writer) context.Context {
2626
return context.WithValue(ctx, contextKeyLogWriter, logger)
2727
}
2828

29-
// LoggerFromContext returns a logger from the context, or the global logger if none is found.
29+
// Logger returns a logger from the context, or the global logger if none is found.
3030
// this is somewhat expensive, it should be called once per task.
3131
func Logger(ctx context.Context) *zap.Logger {
3232
writer := WriterFromContext(ctx)

internal/orchestrator/repo/command_prefix.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ func resolveCommandPrefix(prefix *v1.CommandPrefix) ([]restic.GenericOption, err
2525
if prefix.GetCpuNice() != v1.CommandPrefix_CPU_DEFAULT {
2626
if !niceAvailable() {
2727
return nil, errors.New("nice not available, cpu_nice cannot be used")
28-
} else {
29-
switch prefix.GetCpuNice() {
30-
case v1.CommandPrefix_CPU_HIGH:
31-
opts = append(opts, restic.WithPrefixCommand("nice", "-n", "10"))
32-
case v1.CommandPrefix_CPU_LOW:
33-
opts = append(opts, restic.WithPrefixCommand("nice", "-n", "-10"))
34-
}
28+
}
29+
switch prefix.GetCpuNice() {
30+
case v1.CommandPrefix_CPU_HIGH:
31+
opts = append(opts, restic.WithPrefixCommand("nice", "-n", "-10"))
32+
case v1.CommandPrefix_CPU_LOW:
33+
opts = append(opts, restic.WithPrefixCommand("nice", "-n", "10"))
3534
}
3635
}
3736

internal/orchestrator/repo/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func NewRepoOrchestrator(config *v1.Config, repoConfig *v1.Repo, resticPath stri
5959

6060
// Resolve command prefix
6161
if extraOpts, err := resolveCommandPrefix(repoConfig.GetCommandPrefix()); err != nil {
62-
return nil, fmt.Errorf(" resolve command prefix: %w", err)
62+
return nil, fmt.Errorf("resolve command prefix: %w", err)
6363
} else {
6464
opts = append(opts, extraOpts...)
6565
}

pkg/restic/restic.go

+12-21
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ type Repo struct {
2626
cmd string
2727
uri string
2828

29-
extraArgs []string
30-
extraEnv []string
29+
opts []GenericOption
3130

3231
exists error
3332
checkExists sync.Once
@@ -37,35 +36,29 @@ type Repo struct {
3736

3837
// NewRepo instantiates a new repository.
3938
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))
4640

4741
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,
5245
}
5346
}
5447

5548
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)
5752

5853
fullCmd := append([]string{r.cmd}, args...)
5954

6055
if len(opt.prefixCmd) > 0 {
6156
fullCmd = append(slices.Clone(opt.prefixCmd), fullCmd...)
6257
}
6358

64-
fullCmd = append(fullCmd, r.extraArgs...)
6559
fullCmd = append(fullCmd, opt.extraArgs...)
6660

6761
cmd := exec.CommandContext(ctx, fullCmd[0], fullCmd[1:]...)
68-
cmd.Env = append(cmd.Env, r.extraEnv...)
6962
cmd.Env = append(cmd.Env, opt.extraEnv...)
7063

7164
logger := LoggerFromContext(ctx)
@@ -76,7 +69,7 @@ func (r *Repo) commandWithContext(ctx context.Context, args []string, opts ...Ge
7669
}
7770

7871
if logger := LoggerFromContext(ctx); logger != nil {
79-
fmt.Fprintf(logger, "\ncommand: %v %v\n", r.cmd, strings.Join(args, " "))
72+
fmt.Fprintf(logger, "\ncommand: %v %v\n", fullCmd[0], strings.Join(fullCmd[1:], " "))
8073
}
8174

8275
return cmd
@@ -434,12 +427,10 @@ type GenericOpts struct {
434427
prefixCmd []string
435428
}
436429

437-
func resolveOpts(opts []GenericOption) *GenericOpts {
438-
opt := &GenericOpts{}
430+
func resolveOpts(opt *GenericOpts, opts []GenericOption) {
439431
for _, o := range opts {
440432
o(opt)
441433
}
442-
return opt
443434
}
444435

445436
type GenericOption func(opts *GenericOpts)
@@ -487,8 +478,8 @@ func WithEnviron() GenericOption {
487478
return WithEnv(os.Environ()...)
488479
}
489480

490-
func WithPrefixCommand(proc string, args ...string) GenericOption {
481+
func WithPrefixCommand(args ...string) GenericOption {
491482
return func(opts *GenericOpts) {
492-
opts.prefixCmd = append([]string{proc}, args...)
483+
opts.prefixCmd = append(opts.prefixCmd, args...)
493484
}
494485
}

0 commit comments

Comments
 (0)