github.com/pdmccormick/importable-docker-buildx@v0.0.0-20240426161518-e47091289030/monitor/commands/exec.go (about) 1 package commands 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 8 controllerapi "github.com/docker/buildx/controller/pb" 9 "github.com/docker/buildx/monitor/types" 10 "github.com/pkg/errors" 11 ) 12 13 type ExecCmd struct { 14 m types.Monitor 15 16 invokeConfig controllerapi.InvokeConfig 17 stdout io.WriteCloser 18 } 19 20 func NewExecCmd(m types.Monitor, invokeConfig controllerapi.InvokeConfig, stdout io.WriteCloser) types.Command { 21 return &ExecCmd{m, invokeConfig, stdout} 22 } 23 24 func (cm *ExecCmd) Info() types.CommandInfo { 25 return types.CommandInfo{ 26 Name: "exec", 27 HelpMessage: "execute a process in the interactive container", 28 HelpMessageLong: ` 29 Usage: 30 exec COMMAND [ARG...] 31 32 COMMAND and ARG... will be executed in the container. 33 `, 34 } 35 } 36 37 func (cm *ExecCmd) Exec(ctx context.Context, args []string) error { 38 if ref := cm.m.AttachedSessionID(); ref == "" { 39 return errors.Errorf("no attaching session") 40 } 41 if len(args) < 2 { 42 return errors.Errorf("command must be passed") 43 } 44 cfg := controllerapi.InvokeConfig{ 45 Entrypoint: []string{args[1]}, 46 Cmd: args[2:], 47 NoCmd: false, 48 // TODO: support other options as well via flags 49 Env: cm.invokeConfig.Env, 50 User: cm.invokeConfig.User, 51 Cwd: cm.invokeConfig.Cwd, 52 Tty: true, 53 } 54 pid := cm.m.Exec(ctx, cfg) 55 fmt.Fprintf(cm.stdout, "Process %q started. Press Ctrl-a-c to switch to that process.\n", pid) 56 return nil 57 }