@@ -2,14 +2,17 @@ package root
22
33import (
44 "cmp"
5+ "context"
56 "errors"
67 "fmt"
78 "log/slog"
89 "os"
910 "os/exec"
11+ "path/filepath"
1012
1113 "github.com/docker/cli/cli"
1214 "github.com/spf13/cobra"
15+ "github.com/spf13/pflag"
1316
1417 "github.com/docker/docker-agent/pkg/config"
1518 "github.com/docker/docker-agent/pkg/environment"
@@ -20,30 +23,35 @@ import (
2023// runInSandbox delegates the current command to a Docker sandbox.
2124// It ensures a sandbox exists (creating or recreating as needed), then
2225// executes docker agent inside it via `docker sandbox exec`.
23- func runInSandbox (cmd * cobra.Command , runConfig * config.RuntimeConfig , template string ) error {
26+ func runInSandbox (ctx context. Context , cmd * cobra.Command , args [] string , runConfig * config.RuntimeConfig , template string ) error {
2427 if environment .InSandbox () {
2528 return fmt .Errorf ("already running inside a Docker sandbox (VM %s)" , os .Getenv ("SANDBOX_VM_ID" ))
2629 }
2730
28- ctx := cmd .Context ()
2931 if err := sandbox .CheckAvailable (ctx ); err != nil {
3032 return err
3133 }
3234
33- dockerAgentArgs := sandbox .BuildCagentArgs (os .Args )
34- agentRef := sandbox .AgentRefFromArgs (dockerAgentArgs )
35- configDir := paths .GetConfigDir ()
35+ var agentRef string
36+ if len (args ) > 0 {
37+ agentRef = args [0 ]
38+ }
3639
37- // Always forward config directory paths so the sandbox-side
38- // docker agent resolves it to the same host directories
39- // (which is mounted read-write by ensureSandbox).
40- dockerAgentArgs = sandbox . AppendFlagIfMissing (dockerAgentArgs , "--config-dir" , configDir )
40+ configDir := paths . GetConfigDir ()
41+ dockerAgentArgs := dockerAgentArgs ( cmd , args , configDir )
42+ dockerAgentArgs = append ( dockerAgentArgs , args ... )
43+ dockerAgentArgs = append (dockerAgentArgs , "--config-dir" , configDir )
4144
4245 stopTokenWriter := sandbox .StartTokenWriterIfNeeded (ctx , configDir , runConfig .ModelsGateway )
4346 defer stopTokenWriter ()
4447
45- // Ensure a sandbox with the right workspace mounts exists.
46- wd := cmp .Or (runConfig .WorkingDir , "." )
48+ // Resolve wd to an absolute path so that it matches the absolute
49+ // workspace paths returned by `docker sandbox ls --json`.
50+ wd , err := filepath .Abs (cmp .Or (runConfig .WorkingDir , "." ))
51+ if err != nil {
52+ return fmt .Errorf ("resolving workspace path: %w" , err )
53+ }
54+
4755 name , err := sandbox .Ensure (ctx , wd , sandbox .ExtraWorkspace (wd , agentRef ), template , configDir )
4856 if err != nil {
4957 return err
@@ -60,7 +68,7 @@ func runInSandbox(cmd *cobra.Command, runConfig *config.RuntimeConfig, template
6068 envFlags = append (envFlags , "-e" , envModelsGateway + "=" + gateway )
6169 }
6270
63- dockerCmd := sandbox .BuildExecCmd (ctx , name , dockerAgentArgs , envFlags , envVars )
71+ dockerCmd := sandbox .BuildExecCmd (ctx , name , wd , dockerAgentArgs , envFlags , envVars )
6472 slog .Debug ("Executing in sandbox" , "name" , name , "args" , dockerCmd .Args )
6573
6674 if err := dockerCmd .Run (); err != nil {
@@ -72,3 +80,31 @@ func runInSandbox(cmd *cobra.Command, runConfig *config.RuntimeConfig, template
7280
7381 return nil
7482}
83+
84+ func dockerAgentArgs (cmd * cobra.Command , args []string , configDir string ) []string {
85+ var dockerAgentArgs []string
86+ hasYolo := false
87+ cmd .Flags ().Visit (func (f * pflag.Flag ) {
88+ if f .Name == "sandbox" || f .Name == "config-dir" {
89+ return
90+ }
91+
92+ if f .Name == "yolo" {
93+ hasYolo = true
94+ }
95+
96+ if f .Value .Type () == "bool" {
97+ dockerAgentArgs = append (dockerAgentArgs , "--" + f .Name )
98+ } else {
99+ dockerAgentArgs = append (dockerAgentArgs , "--" + f .Name , f .Value .String ())
100+ }
101+ })
102+ if ! hasYolo {
103+ dockerAgentArgs = append (dockerAgentArgs , "--yolo" )
104+ }
105+
106+ dockerAgentArgs = append (dockerAgentArgs , args ... )
107+ dockerAgentArgs = append (dockerAgentArgs , "--config-dir" , configDir )
108+
109+ return dockerAgentArgs
110+ }
0 commit comments