-
Notifications
You must be signed in to change notification settings - Fork 8
/
query.go
104 lines (87 loc) · 1.77 KB
/
query.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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"text/tabwriter"
"github.com/reconquest/karma-go"
)
type container struct {
Name string `json:"name"`
Status string `json:"status"`
Root string `json:"root"`
Address string `json:"address"`
}
func queryContainers(
args map[string]interface{}, storageEngine storage,
) error {
var (
rootDir = args["-r"].(string)
useJSON = args["-j"].(bool)
filter = args["<name>"].([]string)
)
all, err := listContainers(filepath.Join(rootDir, "containers"))
if err != nil {
return err
}
active, err := listActiveContainers(containerSuffix)
if err != nil {
return err
}
containers := []container{}
for _, name := range all {
if len(filter) > 0 {
found := false
for _, target := range filter {
if target == name {
found = true
break
}
}
if !found {
continue
}
}
container := container{
Name: name,
Status: "inactive",
Root: storageEngine.GetContainerRoot(name),
Address: "",
}
_, ok := active[name]
if ok {
container.Status = "active"
container.Address, err = getContainerIP(name)
if err != nil {
fmt.Fprintln(os.Stderr, karma.Format(err,
"WARNING: can't obtain container '%s' address",
name,
))
}
}
containers = append(containers, container)
}
if !useJSON {
writer := tabwriter.NewWriter(os.Stdout, 2, 4, 2, ' ', 0)
for _, container := range containers {
fmt.Fprintf(
writer,
"%s\t%s\t%s\t%s\n",
container.Name, container.Status,
container.Address, container.Root,
)
}
err = writer.Flush()
if err != nil {
return err
}
return nil
}
output, err := json.MarshalIndent(containers, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}