mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-10 05:27:07 +08:00
monitor: Move commands to a separated package
This commit moves monitor commands to `monior/commands` package. Commands still need access to the `monitor` object and buildx controller so this commit enables this via `Monitor` interface stored in `monitor/types`. Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
This commit is contained in:
44
monitor/commands/exec.go
Normal file
44
monitor/commands/exec.go
Normal file
@ -0,0 +1,44 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
controllerapi "github.com/docker/buildx/controller/pb"
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type ExecCmd struct {
|
||||
m types.Monitor
|
||||
|
||||
invokeConfig controllerapi.InvokeConfig
|
||||
stdout io.WriteCloser
|
||||
}
|
||||
|
||||
func NewExecCmd(m types.Monitor, invokeConfig controllerapi.InvokeConfig, stdout io.WriteCloser) types.Command {
|
||||
return &ExecCmd{m, invokeConfig, stdout}
|
||||
}
|
||||
|
||||
func (cm *ExecCmd) Info() types.CommandInfo {
|
||||
return types.CommandInfo{HelpMessage: "execute a process in the interactive container"}
|
||||
}
|
||||
|
||||
func (cm *ExecCmd) Exec(ctx context.Context, args []string) error {
|
||||
if len(args) < 2 {
|
||||
return errors.Errorf("command must be passed")
|
||||
}
|
||||
cfg := controllerapi.InvokeConfig{
|
||||
Entrypoint: []string{args[1]},
|
||||
Cmd: args[2:],
|
||||
// TODO: support other options as well via flags
|
||||
Env: cm.invokeConfig.Env,
|
||||
User: cm.invokeConfig.User,
|
||||
Cwd: cm.invokeConfig.Cwd,
|
||||
Tty: true,
|
||||
}
|
||||
pid := cm.m.Exec(ctx, cfg)
|
||||
fmt.Fprintf(cm.stdout, "Process %q started. Press Ctrl-a-c to switch to that process.\n", pid)
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user