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:
Kohei Tokunaga
2023-04-18 21:27:23 +09:00
parent 75ed3e296b
commit 0a7a2b1882
10 changed files with 624 additions and 206 deletions

View File

@ -0,0 +1,38 @@
package commands
import (
"context"
"github.com/docker/buildx/monitor/types"
"github.com/pkg/errors"
)
type DisconnectCmd struct {
m types.Monitor
}
func NewDisconnectCmd(m types.Monitor) types.Command {
return &DisconnectCmd{m}
}
func (cm *DisconnectCmd) Info() types.CommandInfo {
return types.CommandInfo{HelpMessage: "disconnect a client from a buildx server. Specific session ID can be specified an arg"}
}
func (cm *DisconnectCmd) Exec(ctx context.Context, args []string) error {
target := cm.m.AttachedSessionID()
if len(args) >= 2 {
target = args[1]
}
isProcess, err := isProcessID(ctx, cm.m, target)
if err == nil && isProcess {
if err := cm.m.DisconnectProcess(ctx, target); err != nil {
return errors.Errorf("disconnecting from process failed %v", target)
}
return nil
}
if err := cm.m.DisconnectSession(ctx, target); err != nil {
return errors.Errorf("disconnecting from session failed: %v", err)
}
return nil
}