-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker.go
157 lines (129 loc) · 3.61 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package dkl
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/manifoldco/promptui"
)
func show() (*types.Container, error) {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
if len(containers) == 0 {
return nil, fmt.Errorf("running container is not found.")
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U0001F40B {{ index .Names 0 | cyan }} ({{ .Image | red }}, {{ .ImageID }})",
Inactive: " {{ index .Names 0 | cyan }} ({{ .Image | red }}, {{ .ImageID }})",
Selected: "\U0001F40B {{ index .Names 0 | red }}",
Details: `
--------- Image ----------
{{ "Name:" | faint }} {{ printf "%q" .Names }}
{{ "Image:" | faint }} {{ .Image }}
{{ "ImageID:" | faint }} {{ .ImageID }}`,
}
searcher := func(input string, index int) bool {
container := containers[index]
n := strings.Join(container.Names, "")
ni := n + container.Image
lni := strings.ReplaceAll(strings.ToLower(ni), " ", "")
input = strings.ReplaceAll(strings.ToLower(input), " ", "")
return strings.Contains(lni, input)
}
prompt := promptui.Select{
Label: "Docker running...",
Items: containers,
Templates: templates,
Size: 10,
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return nil, err
}
return &containers[i], nil
}
// Port from moby/moby/integration/internal/container/exec.go
// ExecResult represents a result returned from Exec()
type ExecResult struct {
ExitCode int
outBuffer *bytes.Buffer
errBuffer *bytes.Buffer
}
// Stdout returns stdout output of a command run by Exec()
func (res *ExecResult) Stdout() string {
return res.outBuffer.String()
}
// Stderr returns stderr output of a command run by Exec()
func (res *ExecResult) Stderr() string {
return res.errBuffer.String()
}
// Combined returns combined stdout and stderr output of a command run by Exec()
func (res *ExecResult) Combined() string {
return res.outBuffer.String() + res.errBuffer.String()
}
func execDocker(cid string, cmd []string, ins io.Reader) (ExecResult, error) {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
ctx := context.Background()
econf := types.ExecConfig{
Tty: true,
AttachStdin: true,
AttachStderr: true,
AttachStdout: true,
Cmd: cmd,
}
cresp, err := cli.ContainerExecCreate(ctx, cid, econf)
if err != nil {
return ExecResult{}, err
}
execID := cresp.ID
// run it, with stdout/stderr attached
aresp, err := cli.ContainerExecAttach(ctx, execID, types.ExecStartCheck{})
if err != nil {
return ExecResult{}, err
}
defer aresp.Close()
// read the output
var outBuf, errBuf bytes.Buffer
outputDone := make(chan error)
go func() {
// StdCopy demultiplexes the stream into two buffers
_, err = stdcopy.StdCopy(&outBuf, &errBuf, aresp.Reader)
outputDone <- err
}()
select {
case err := <-outputDone:
if err != nil {
return ExecResult{}, err
}
break
case <-ctx.Done():
return ExecResult{}, ctx.Err()
}
// get the exit code
iresp, err := cli.ContainerExecInspect(ctx, execID)
if err != nil {
return ExecResult{}, err
}
return ExecResult{ExitCode: iresp.ExitCode, outBuffer: &outBuf, errBuffer: &errBuf}, nil
}
func buildDockerCmd(cid string, cmd []string) []string {
res := []string{"docker", "exec", "-i", "-t", cid}
res = append(res, cmd...)
return res
}