File: disconnect.go

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (54 lines) | stat: -rw-r--r-- 1,333 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package commands

import (
	"context"
	"fmt"

	"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{
		Name:        "disconnect",
		HelpMessage: "disconnect a client from a buildx server. Specific session ID can be specified an arg",
		HelpMessageLong: fmt.Sprintf(`
Usage:
  disconnect [ID]

ID is for a session (visible via list command). Default is %q.
`, cm.m.AttachedSessionID()),
	}
}

func (cm *DisconnectCmd) Exec(ctx context.Context, args []string) error {
	target := cm.m.AttachedSessionID()
	if len(args) >= 2 {
		target = args[1]
	} else if target == "" {
		return errors.Errorf("no attaching session")
	}
	isProcess, err := isProcessID(ctx, cm.m, target)
	if err == nil && isProcess {
		sid := cm.m.AttachedSessionID()
		if sid == "" {
			return errors.Errorf("no attaching session")
		}
		if err := cm.m.DisconnectProcess(ctx, sid, 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
}