-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (139 loc) · 2.76 KB
/
main.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
158
159
160
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"io"
"log"
"github.com/urfave/cli"
)
var (
columnSep = regexp.MustCompile(`\s+`)
debug = false
)
func main() {
var all bool
var image bool
app := cli.NewApp()
app.Name = "docker-selector"
app.Usage = "docker container selector."
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "Show all containers",
Destination: &all,
},
cli.BoolFlag{
Name: "image, i",
Usage: "Show images",
Destination: &image,
},
cli.BoolFlag{
Name: "debug",
Usage: "Show debug logs",
Destination: &debug,
},
}
app.Action = func(c *cli.Context) error {
var ids string
if image {
ids = execDockerImage()
} else if all {
ids = execDockerPsAll()
} else {
ids = execDockerPs()
}
fmt.Print(ids)
return nil
}
app.Run(os.Args)
}
func logD(format string, a ...interface{}) {
if debug {
if a == nil {
log.Print(format)
} else {
log.Printf(format, a)
}
}
}
func logE(a ...interface{}) {
if debug && a != nil {
log.Fatal(a)
}
}
func execDockerImage() string {
var s string
s = output([]string{"docker", "images"}, "")
s = removeHeader(s)
s = output([]string{"peco"}, s)
return extractColumn(s, 2)
}
func execDockerPs() string {
var s string
s = output([]string{"docker", "ps"}, "")
s = removeHeader(s)
s = output([]string{"peco"}, s)
return extractColumn(s, 0)
}
func execDockerPsAll() string {
var s string
s = output([]string{"docker", "ps", "-a"}, "")
s = removeHeader(s)
s = output([]string{"peco"}, s)
return extractColumn(s, 0)
}
func output(cmd []string, toStdin string) string {
logD("execute start %v", cmd)
var c *exec.Cmd
if len(cmd) >= 2 {
c = exec.Command(cmd[0], cmd[1:]...)
} else {
c = exec.Command(cmd[0])
}
if toStdin != "" {
// I can't do anything if fails here.
in, err := c.StdinPipe()
if err != nil {
logE(err)
}
go func() {
_, err := io.WriteString(in, toStdin)
if err != nil {
logE(err)
}
}()
}
// Depending on the environment, fails here.
s, err := c.Output()
if err != nil {
logE(err)
}
logD("execute finish %v", cmd)
return string(s)
}
func removeHeader(in string) string {
lines := strings.Split(in, "\n")
// The measured value was 3.
if len(lines) >= 3 {
logD("removed a header.")
return strings.Join(lines[1:], "\n")
}
// If there is no container or image,
// remains header to avoiding error of peco.
logD("remains a header.")
return in
}
func extractColumn(out string, pos int) string {
lines := strings.Split(out, "\n")
ids := make([]string, len(lines))
for i, l := range lines {
columns := columnSep.Split(l, -1)
if 0 <= pos && pos < len(columns) {
ids[i] = columns[pos]
}
}
return strings.Join(ids, " ")
}