Skip to content

Commit 706b57f

Browse files
committed
refactor, support docker container
1 parent 2efd6ea commit 706b57f

File tree

13 files changed

+1972
-263
lines changed

13 files changed

+1972
-263
lines changed

.DS_Store

6 KB
Binary file not shown.

cmd.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fyne.io/fyne/v2"
5+
"fyne.io/fyne/v2/dialog"
6+
"fyne.io/fyne/v2/widget"
7+
)
8+
9+
func (w *Window) showNewCmdDialog() {
10+
nameEntry := widget.NewEntry()
11+
textEntry := widget.NewEntry()
12+
textEntry.MultiLine = true
13+
14+
icons := make([]string, len(iconMap))
15+
i := 0
16+
for k, _ := range iconMap {
17+
icons[i] = k
18+
i++
19+
}
20+
iconSelect := widget.NewSelectEntry(icons)
21+
22+
dlg := dialog.NewForm("New Command", "OK", "Cancel", []*widget.FormItem{
23+
widget.NewFormItem("Name", nameEntry),
24+
widget.NewFormItem("Text", textEntry),
25+
widget.NewFormItem("Icon", iconSelect),
26+
}, func(b bool) {
27+
if b {
28+
cmd := &Cmd{Name: nameEntry.Text, Text: textEntry.Text, Icon: iconSelect.Text}
29+
w.AddCmd(cmd)
30+
}
31+
}, w.win)
32+
33+
dlg.Resize(fyne.NewSize(400, 300))
34+
dlg.Show()
35+
}

config.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fyne.io/fyne/v2"
6+
"fyne.io/fyne/v2/container"
7+
"fyne.io/fyne/v2/dialog"
8+
"fyne.io/fyne/v2/widget"
9+
"github.com/jinzhu/copier"
10+
"log"
11+
)
12+
13+
type Config interface {
14+
Name() string
15+
Type() string
16+
Data() interface{}
17+
Form() *widget.Form
18+
Term(*Window)
19+
OnOk()
20+
}
21+
22+
func (w *Window) showCreateConfigDialog() {
23+
sshConf := &SSHConfig{}
24+
dockerConf := &DockerConfig{}
25+
k8sConf := &K8SConfig{}
26+
27+
sshForm := sshConf.Form()
28+
dockerForm := dockerConf.Form()
29+
k8sForm := k8sConf.Form()
30+
typeSelect := widget.NewSelect([]string{"SSH", "Docker", "K8S"}, func(s string) {
31+
sshForm.Hide()
32+
dockerForm.Hide()
33+
k8sForm.Hide()
34+
switch s {
35+
case "SSH":
36+
sshForm.Show()
37+
case "Docker":
38+
dockerForm.Show()
39+
case "K8S":
40+
k8sForm.Show()
41+
default:
42+
43+
}
44+
})
45+
title := "Create Config"
46+
typeSelect.SetSelectedIndex(0)
47+
box := container.NewVBox(typeSelect, sshForm, dockerForm, k8sForm)
48+
49+
dlg := dialog.NewCustomConfirm(title, "OK", "Cancel", box, func(b bool) {
50+
if b {
51+
switch typeSelect.Selected {
52+
case "SSH":
53+
sshConf.OnOk()
54+
w.confs = append(w.confs, sshConf)
55+
case "Docker":
56+
dockerConf.OnOk()
57+
w.confs = append(w.confs, dockerConf)
58+
case "K8S":
59+
k8sConf.OnOk()
60+
w.confs = append(w.confs, k8sConf)
61+
default:
62+
return
63+
}
64+
w.save()
65+
}
66+
}, w.win)
67+
dlg.Resize(fyne.Size{Width: 300})
68+
dlg.Show()
69+
}
70+
71+
func (w *Window) showModifyConfigDialog(cfg Config) {
72+
form := cfg.Form()
73+
title := "Modify Config: " + cfg.Name()
74+
dlg := dialog.NewCustomConfirm(title, "OK", "Cancel", form, func(b bool) {
75+
if b {
76+
cfg.OnOk()
77+
w.save()
78+
}
79+
}, w.win)
80+
dlg.Resize(fyne.Size{Width: 300})
81+
dlg.Show()
82+
}
83+
84+
func (w *Window) load() {
85+
confJson := w.app.Preferences().String(APP_SESSIONS)
86+
87+
confArr := []map[string]interface{}{}
88+
err := json.Unmarshal([]byte(confJson), &confArr)
89+
if err != nil {
90+
log.Println(err)
91+
}
92+
93+
w.confs = make([]Config, 0)
94+
for _, data := range confArr {
95+
if v, e := data["type"]; e {
96+
if t, ok := v.(string); ok {
97+
var cfg Config
98+
switch t {
99+
case "ssh":
100+
cfg = &SSHConfig{}
101+
case "docker":
102+
cfg = &DockerConfig{}
103+
case "k8s":
104+
cfg = &K8SConfig{}
105+
default:
106+
continue
107+
}
108+
err := copier.Copy(cfg, data)
109+
if err != nil {
110+
continue
111+
}
112+
w.confs = append(w.confs, cfg)
113+
}
114+
}
115+
116+
}
117+
118+
cmdJson := w.app.Preferences().String(APP_COMMANDS)
119+
err = json.Unmarshal([]byte(cmdJson), &w.cmds)
120+
if err != nil {
121+
log.Println(err)
122+
}
123+
}
124+
125+
func (w *Window) save() {
126+
127+
confData := make([]interface{}, len(w.confs))
128+
for i := 0; i < len(w.confs); i++ {
129+
confData[i] = w.confs[i].Data()
130+
}
131+
132+
confJson, err := json.Marshal(confData)
133+
if err != nil {
134+
log.Println(err)
135+
return
136+
}
137+
w.app.Preferences().SetString(APP_SESSIONS, string(confJson))
138+
139+
cmdJson, err := json.Marshal(w.cmds)
140+
if err != nil {
141+
log.Println(err)
142+
return
143+
}
144+
w.app.Preferences().SetString(APP_COMMANDS, string(cmdJson))
145+
146+
}

docker.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fyne.io/fyne/v2"
6+
"fyne.io/fyne/v2/container"
7+
"fyne.io/fyne/v2/dialog"
8+
"fyne.io/fyne/v2/widget"
9+
"github.com/docker/docker/api/types"
10+
"github.com/docker/docker/client"
11+
"github.com/fyne-io/terminal"
12+
)
13+
14+
type DockerConfigData struct {
15+
Name string
16+
Type string
17+
Host string
18+
}
19+
20+
type DockerConfig struct {
21+
data *DockerConfigData
22+
onOk func()
23+
}
24+
25+
func (c *DockerConfig) Name() string {
26+
return c.data.Name
27+
}
28+
29+
func (c *DockerConfig) Type() string {
30+
return "ssh"
31+
}
32+
33+
func (c *DockerConfig) Data() interface{} {
34+
return c.data
35+
}
36+
37+
func (c *DockerConfig) Form() *widget.Form {
38+
39+
nameEntry := widget.NewEntry()
40+
hostEntry := widget.NewEntry()
41+
42+
data := c.data
43+
if data != nil {
44+
nameEntry.Text = data.Name
45+
nameEntry.Disable()
46+
hostEntry.Text = data.Host
47+
}
48+
c.onOk = func() {
49+
if c.data == nil {
50+
c.data = &DockerConfigData{Type: c.Type()}
51+
}
52+
c.data.Name = nameEntry.Text
53+
c.data.Host = hostEntry.Text
54+
}
55+
return widget.NewForm([]*widget.FormItem{
56+
widget.NewFormItem("Name", nameEntry),
57+
widget.NewFormItem("Host", hostEntry),
58+
}...)
59+
}
60+
61+
func (c *DockerConfig) OnOk() {
62+
c.onOk()
63+
}
64+
func (c *DockerConfig) Term(win *Window) {
65+
opts := make([]client.Opt, 0)
66+
if len(c.data.Host) > 0 {
67+
opts = append(opts, client.WithHost(c.data.Host))
68+
}
69+
dockerCli, err := client.NewClientWithOpts(opts...)
70+
if err != nil {
71+
win.showError(err)
72+
return
73+
}
74+
contList, err := dockerCli.ContainerList(context.Background(), types.ContainerListOptions{})
75+
if err != nil {
76+
win.showError(err)
77+
return
78+
}
79+
var dlg dialog.Dialog
80+
list := widget.NewList(func() int {
81+
return len(contList)
82+
}, func() fyne.CanvasObject {
83+
return container.NewHBox(widget.NewLabel(""), widget.NewButton("Connect", func() {
84+
}))
85+
}, func(id widget.ListItemID, object fyne.CanvasObject) {
86+
box := object.(*fyne.Container)
87+
cont := contList[id]
88+
89+
label := box.Objects[0].(*widget.Label)
90+
btn := box.Objects[1].(*widget.Button)
91+
label.SetText(cont.ID[:12])
92+
btn.OnTapped = func() {
93+
94+
execId, err := dockerCli.ContainerExecCreate(context.Background(), cont.ID, types.ExecConfig{Tty: true, Detach: true, AttachStdin: true, AttachStderr: true, AttachStdout: true, Cmd: []string{"/bin/sh"}})
95+
if err != nil {
96+
win.showError(err)
97+
return
98+
}
99+
100+
attach, err := dockerCli.ContainerExecAttach(context.Background(), execId.ID, types.ExecStartCheck{Tty: true, Detach: false})
101+
if err != nil {
102+
win.showError(err)
103+
return
104+
}
105+
term := terminal.New()
106+
go func() {
107+
defer attach.Close()
108+
err = term.RunWithConnection(attach.Conn, attach.Reader)
109+
if err != nil {
110+
win.showError(err)
111+
return
112+
}
113+
}()
114+
115+
tab := &Term{name: c.Name(), term: term}
116+
win.AddTermTab(tab)
117+
if dlg != nil {
118+
dlg.Hide()
119+
}
120+
}
121+
})
122+
123+
list.Resize(fyne.Size{Width: 400, Height: 500})
124+
125+
dlg = dialog.NewCustom("Select a container", "Cancel", list, win.win)
126+
dlg.Resize(fyne.Size{Width: 400, Height: 500})
127+
dlg.Show()
128+
}

go.mod

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ go 1.16
44

55
require (
66
fyne.io/fyne/v2 v2.0.2-0.20210408205458-4c1581be8086
7+
github.com/Microsoft/go-winio v0.5.2 // indirect
8+
github.com/containerd/containerd v1.6.8 // indirect
9+
github.com/docker/distribution v2.8.1+incompatible // indirect
10+
github.com/docker/docker v17.12.0-ce-rc1.0.20201201034508-7d75c1d40d88+incompatible
11+
github.com/docker/go-units v0.5.0 // indirect
712
github.com/fyne-io/terminal v0.0.0-20210419192104-b6a609f9c1bd
13+
github.com/gorilla/mux v1.8.0 // indirect
14+
github.com/jinzhu/copier v0.3.5
15+
github.com/sirupsen/logrus v1.9.0 // indirect
816
github.com/srwiley/oksvg v0.0.0-20210320200257-875f767ac39a // indirect
9-
golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc
17+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
18+
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
19+
google.golang.org/grpc v1.49.0 // indirect
1020
)

0 commit comments

Comments
 (0)