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:
37
monitor/commands/ps.go
Normal file
37
monitor/commands/ps.go
Normal file
@ -0,0 +1,37 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
)
|
||||
|
||||
type PsCmd struct {
|
||||
m types.Monitor
|
||||
stdout io.WriteCloser
|
||||
}
|
||||
|
||||
func NewPsCmd(m types.Monitor, stdout io.WriteCloser) types.Command {
|
||||
return &PsCmd{m, stdout}
|
||||
}
|
||||
|
||||
func (cm *PsCmd) Info() types.CommandInfo {
|
||||
return types.CommandInfo{HelpMessage: `list processes invoked by "exec". Use "attach" to attach IO to that process`}
|
||||
}
|
||||
|
||||
func (cm *PsCmd) Exec(ctx context.Context, args []string) error {
|
||||
plist, err := cm.m.ListProcesses(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tw := tabwriter.NewWriter(cm.stdout, 1, 8, 1, '\t', 0)
|
||||
fmt.Fprintln(tw, "PID\tCURRENT_SESSION\tCOMMAND")
|
||||
for _, p := range plist {
|
||||
fmt.Fprintf(tw, "%-20s\t%v\t%v\n", p.ProcessID, p.ProcessID == cm.m.AttachedPID(), append(p.InvokeConfig.Entrypoint, p.InvokeConfig.Cmd...))
|
||||
}
|
||||
tw.Flush()
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user