Kohei Tokunaga 437fe55104
monitor: improve error messages
Print more understandable messages on error:

- When ps fails because the monitor doesn't attach to any session, print "no
  attaching session" instead of "unknown ref".
- Avoid disconnect silently fails when the monitor doesn't attach to any
  session. Print "no attaching session" error instead.
- Fix error message of "attach"'s arguments. ("server name must be passed" ->
  "ID of session or process must be passed")

Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
2023-05-08 15:42:37 +09:00

43 lines
1020 B
Go

package commands
import (
"context"
"fmt"
"io"
"text/tabwriter"
"github.com/docker/buildx/monitor/types"
"github.com/pkg/errors"
)
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 {
ref := cm.m.AttachedSessionID()
if ref == "" {
return errors.Errorf("no attaching session")
}
plist, err := cm.m.ListProcesses(ctx, ref)
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
}