-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdocker.go
139 lines (120 loc) · 3.05 KB
/
docker.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"encoding/json"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
type DockerConfigData struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Host string `json:"host,omitempty"`
}
type DockerConfig struct {
data *DockerConfigData
onOk func()
}
func (c *DockerConfig) Name() string {
return c.data.Name
}
func (c *DockerConfig) Type() string {
return "docker"
}
func (c *DockerConfig) Load(s string) error {
data := &DockerConfigData{}
err := json.Unmarshal([]byte(s), data)
if err != nil {
return err
}
c.data = data
return nil
}
func (c *DockerConfig) Data() interface{} {
return c.data
}
func (c *DockerConfig) Form() *widget.Form {
nameEntry := widget.NewEntry()
hostEntry := widget.NewEntry()
data := c.data
if data != nil {
nameEntry.Text = data.Name
nameEntry.Disable()
hostEntry.Text = data.Host
}
c.onOk = func() {
if c.data == nil {
c.data = &DockerConfigData{Type: c.Type()}
}
c.data.Name = nameEntry.Text
c.data.Host = hostEntry.Text
}
return widget.NewForm([]*widget.FormItem{
widget.NewFormItem("Name", nameEntry),
widget.NewFormItem("Host", hostEntry),
}...)
}
func (c *DockerConfig) OnOk() {
c.onOk()
}
func (c *DockerConfig) Term(win *Window) {
opts := make([]client.Opt, 0)
if len(c.data.Host) > 0 {
opts = append(opts, client.WithHost(c.data.Host))
}
dockerCli, err := client.NewClientWithOpts(opts...)
if err != nil {
win.showError(err)
return
}
contList, err := dockerCli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
win.showError(err)
return
}
var dlg dialog.Dialog
list := widget.NewList(func() int {
return len(contList)
}, func() fyne.CanvasObject {
return container.NewHBox(widget.NewLabel(""), widget.NewButton("Connect", func() {
}))
}, func(id widget.ListItemID, object fyne.CanvasObject) {
box := object.(*fyne.Container)
cont := contList[id]
label := box.Objects[0].(*widget.Label)
btn := box.Objects[1].(*widget.Button)
label.SetText(cont.ID[:12])
btn.OnTapped = func() {
execId, err := dockerCli.ContainerExecCreate(context.Background(), cont.ID, types.ExecConfig{Tty: true, Detach: true, AttachStdin: true, AttachStderr: true, AttachStdout: true, Cmd: []string{"/bin/sh"}})
if err != nil {
win.showError(err)
return
}
attach, err := dockerCli.ContainerExecAttach(context.Background(), execId.ID, types.ExecStartCheck{Tty: true, Detach: false})
if err != nil {
win.showError(err)
return
}
term := NewTerm(c.Name(), c)
go func() {
defer attach.Close()
err = term.RunWithConnection(attach.Conn)
if err != nil {
win.showError(err)
return
}
}()
win.AddTermTab(term)
if dlg != nil {
dlg.Hide()
}
}
})
list.Resize(fyne.Size{Width: 400, Height: 500})
dlg = dialog.NewCustom("Select a container", "Cancel", list, win.win)
dlg.Resize(fyne.Size{Width: 400, Height: 500})
dlg.Show()
}