From 8a4589c25a176e7d25267f23ff4c5d0d11fd979d Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sat, 15 Mar 2025 03:39:37 +0900 Subject: [PATCH] sandbox: override underlying command function Mostly useful for test instrumentation or otherwise nifty environments. Signed-off-by: Ophestra --- internal/sandbox/container.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/internal/sandbox/container.go b/internal/sandbox/container.go index 6676a2a..22b231c 100644 --- a/internal/sandbox/container.go +++ b/internal/sandbox/container.go @@ -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,15 @@ 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 - }, + commandContext := exec.CommandContext + container := &Container{name: name, ctx: ctx, + InitParams: InitParams{Args: append([]string{name}, args...), Dir: "/", Ops: new(Ops)}, + RawCommandContext: &commandContext, } + container.CommandContext = func(ctx context.Context) (cmd *exec.Cmd) { + cmd = (*container.RawCommandContext)(ctx, internal.MustExecutable()) + cmd.Args = []string{"init"} + return + } + return container }