sandbox: override underlying command function
All checks were successful
Test / Create distribution (push) Successful in 25s
Test / Fortify (push) Successful in 2m30s
Test / Data race detector (push) Successful in 2m21s
Test / Fpkg (push) Successful in 2m59s
Test / Flake checks (push) Successful in 56s

Mostly useful for test instrumentation or otherwise nifty environments.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-03-15 03:39:37 +09:00
parent 4230281194
commit 989019af10
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q

View File

@ -60,6 +60,8 @@ type (
InitParams
// Custom [exec.Cmd] initialisation function.
CommandContext func(ctx context.Context) (cmd *exec.Cmd)
// Underlying [exec.CommandContext] function called by CommandContext.
RawCommandContext func(ctx context.Context, name string, args ...string) (cmd *exec.Cmd)
// param encoder for shim and init
setup *gob.Encoder
@ -220,12 +222,14 @@ func (p *Container) String() string {
}
func New(ctx context.Context, name string, args ...string) *Container {
return &Container{name: name, ctx: ctx,
InitParams: InitParams{Args: append([]string{name}, args...), Dir: "/", Ops: new(Ops)},
CommandContext: func(ctx context.Context) (cmd *exec.Cmd) {
cmd = exec.CommandContext(ctx, internal.MustExecutable())
cmd.Args = []string{"init"}
return
},
container := &Container{name: name, ctx: ctx,
InitParams: InitParams{Args: append([]string{name}, args...), Dir: "/", Ops: new(Ops)},
RawCommandContext: exec.CommandContext,
}
container.CommandContext = func(ctx context.Context) (cmd *exec.Cmd) {
cmd = container.RawCommandContext(ctx, internal.MustExecutable())
cmd.Args = []string{"init"}
return
}
return container
}