/
workspace_connection.go
75 lines (61 loc) · 1.94 KB
/
workspace_connection.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package docker
import (
"context"
"fmt"
"io"
"github.com/docker/docker/api/types"
"github.com/mazrean/separated-webshell/domain"
"github.com/mazrean/separated-webshell/domain/values"
)
var (
createOpts = types.ExecConfig{
User: imageUser,
WorkingDir: fmt.Sprintf("/home/%s", imageUser),
Cmd: []string{imageCmd},
Tty: true,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
}
attachOpts = types.ExecStartCheck{
Tty: true,
}
)
type WorkspaceConnection struct{}
func NewWorkspaceConnection() *WorkspaceConnection {
return &WorkspaceConnection{}
}
func (wc *WorkspaceConnection) Connect(ctx context.Context, workspace *domain.Workspace) (*domain.WorkspaceConnection, error) {
idRes, err := cli.ContainerExecCreate(ctx, string(workspace.ID()), createOpts)
if err != nil {
return nil, fmt.Errorf("failed to create exec: %w", err)
}
stream, err := cli.ContainerExecAttach(ctx, idRes.ID, attachOpts)
if err != nil {
return nil, fmt.Errorf("failed to attach container: %w", err)
}
connectionID := values.NewWorkspaceConnectionID(idRes.ID)
connectionIO := values.NewWorkspaceIO(stream.Conn, io.NopCloser(stream.Reader))
return domain.NewWorkspaceConnection(connectionID, connectionIO), nil
}
func (wc *WorkspaceConnection) Disconnect(ctx context.Context, connection *domain.WorkspaceConnection) error {
err := connection.ReadCloser().Close()
if err != nil {
return fmt.Errorf("failed to close reader: %w", err)
}
err = connection.WriteCloser().Close()
if err != nil {
return fmt.Errorf("failed to close writer: %w", err)
}
return nil
}
func (wc *WorkspaceConnection) Resize(ctx context.Context, connection *domain.WorkspaceConnection, window *values.Window) error {
err := cli.ContainerExecResize(ctx, string(connection.ID()), types.ResizeOptions{
Height: window.Height(),
Width: window.Width(),
})
if err != nil {
return fmt.Errorf("failed to resize: %w", err)
}
return nil
}