|
| 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 | +} |
0 commit comments