-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
66 lines (63 loc) · 1.71 KB
/
list.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
package main
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"github.com/yunfeiyang1916/cloud-docker/container"
"io/ioutil"
"os"
"text/tabwriter"
)
func ListContainers() {
dirPath := fmt.Sprintf(container.DefaultInfoLocation, "")
// 去除最后一个字符
dirPath = dirPath[:len(dirPath)-1]
// 读取该文件夹下的所有文件
files, err := ioutil.ReadDir(dirPath)
if err != nil {
logrus.Errorf("read dir %s error %v", dirPath, err)
return
}
var infoList []*container.ContainerInfo
// 遍历所有文件
for _, file := range files {
// 根据容器配置文件获取对应信息
info, err := getContainerInfo(file.Name())
if err != nil {
logrus.Errorf("getContainerInfo error %s", err)
continue
}
infoList = append(infoList, info)
}
// 使用tabwriter打印容器信息
w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
fmt.Fprint(w, "ID\tNAME\tPID\tSTATUS\tCOMMAND\tCREATED\n")
for _, item := range infoList {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
item.Id,
item.Name,
item.Pid,
item.Status,
item.Command,
item.CreatedTime)
}
// 刷新标准输出流到缓存区
if err = w.Flush(); err != nil {
logrus.Errorf("Flush error %s", err)
return
}
}
func getContainerInfo(containerName string) (*container.ContainerInfo, error) {
configFilePath := fmt.Sprintf(container.DefaultInfoLocation, containerName) + container.ConfigName
content, err := ioutil.ReadFile(configFilePath)
if err != nil {
logrus.Errorf("read file %s error %v", configFilePath, err)
return nil, err
}
var info container.ContainerInfo
if err = json.Unmarshal(content, &info); err != nil {
logrus.Errorf("json.Unmarshal error %s", err)
return nil, err
}
return &info, nil
}