Skip to content

Commit

Permalink
plumb prefixes to restic pkg correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Jun 1, 2024
1 parent c0ff21f commit c49e745
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 30 deletions.
2 changes: 1 addition & 1 deletion internal/orchestrator/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func ContextWithWriter(ctx context.Context, logger io.Writer) context.Context {
return context.WithValue(ctx, contextKeyLogWriter, logger)
}

// LoggerFromContext returns a logger from the context, or the global logger if none is found.
// Logger returns a logger from the context, or the global logger if none is found.
// this is somewhat expensive, it should be called once per task.
func Logger(ctx context.Context) *zap.Logger {
writer := WriterFromContext(ctx)
Expand Down
13 changes: 6 additions & 7 deletions internal/orchestrator/repo/command_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ func resolveCommandPrefix(prefix *v1.CommandPrefix) ([]restic.GenericOption, err
if prefix.GetCpuNice() != v1.CommandPrefix_CPU_DEFAULT {
if !niceAvailable() {
return nil, errors.New("nice not available, cpu_nice cannot be used")
} else {
switch prefix.GetCpuNice() {
case v1.CommandPrefix_CPU_HIGH:
opts = append(opts, restic.WithPrefixCommand("nice", "-n", "10"))
case v1.CommandPrefix_CPU_LOW:
opts = append(opts, restic.WithPrefixCommand("nice", "-n", "-10"))
}
}
switch prefix.GetCpuNice() {
case v1.CommandPrefix_CPU_HIGH:
opts = append(opts, restic.WithPrefixCommand("nice", "-n", "-10"))
case v1.CommandPrefix_CPU_LOW:
opts = append(opts, restic.WithPrefixCommand("nice", "-n", "10"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/orchestrator/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewRepoOrchestrator(config *v1.Config, repoConfig *v1.Repo, resticPath stri

// Resolve command prefix
if extraOpts, err := resolveCommandPrefix(repoConfig.GetCommandPrefix()); err != nil {
return nil, fmt.Errorf(" resolve command prefix: %w", err)
return nil, fmt.Errorf("resolve command prefix: %w", err)
} else {
opts = append(opts, extraOpts...)
}
Expand Down
33 changes: 12 additions & 21 deletions pkg/restic/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ type Repo struct {
cmd string
uri string

extraArgs []string
extraEnv []string
opts []GenericOption

exists error
checkExists sync.Once
Expand All @@ -37,35 +36,29 @@ type Repo struct {

// NewRepo instantiates a new repository.
func NewRepo(resticBin string, uri string, opts ...GenericOption) *Repo {
opt := &GenericOpts{}
for _, o := range opts {
o(opt)
}

opt.extraEnv = append(opt.extraEnv, "RESTIC_REPOSITORY="+uri)
opts = append(opts, WithEnv("RESTIC_REPOSITORY="+uri))

return &Repo{
cmd: resticBin, // TODO: configurable binary path
uri: uri,
extraArgs: opt.extraArgs,
extraEnv: opt.extraEnv,
cmd: resticBin,
uri: uri,
opts: opts,
}
}

func (r *Repo) commandWithContext(ctx context.Context, args []string, opts ...GenericOption) *exec.Cmd {
opt := resolveOpts(opts)
opt := &GenericOpts{}
resolveOpts(opt, r.opts)
resolveOpts(opt, opts)

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

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

fullCmd = append(fullCmd, r.extraArgs...)
fullCmd = append(fullCmd, opt.extraArgs...)

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

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

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

return cmd
Expand Down Expand Up @@ -434,12 +427,10 @@ type GenericOpts struct {
prefixCmd []string
}

func resolveOpts(opts []GenericOption) *GenericOpts {
opt := &GenericOpts{}
func resolveOpts(opt *GenericOpts, opts []GenericOption) {
for _, o := range opts {
o(opt)
}
return opt
}

type GenericOption func(opts *GenericOpts)
Expand Down Expand Up @@ -487,8 +478,8 @@ func WithEnviron() GenericOption {
return WithEnv(os.Environ()...)
}

func WithPrefixCommand(proc string, args ...string) GenericOption {
func WithPrefixCommand(args ...string) GenericOption {
return func(opts *GenericOpts) {
opts.prefixCmd = append([]string{proc}, args...)
opts.prefixCmd = append(opts.prefixCmd, args...)
}
}

0 comments on commit c49e745

Please sign in to comment.