forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps.go
62 lines (52 loc) · 1.59 KB
/
ps.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
package main
import (
"fmt"
"github.com/spf13/cobra"
)
// psDescription is used to describe ps command in detail and auto generate command doc.
var psDescription = "\nList Containers with container name, ID, status, image reference and runtime."
// PsCommand is used to implement 'ps' command.
type PsCommand struct {
baseCommand
}
// Init initializes PsCommand command.
func (p *PsCommand) Init(c *Cli) {
p.cli = c
p.cmd = &cobra.Command{
Use: "ps",
Short: "List all containers",
Long: psDescription,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return p.runPs(args)
},
Example: psExample(),
}
p.addFlags()
}
// addFlags adds flags for specific command.
func (p *PsCommand) addFlags() {
// TODO: add flags here
}
// runPs is the entry of PsCommand command.
func (p *PsCommand) runPs(args []string) error {
apiClient := p.cli.Client()
containers, err := apiClient.ContainerList()
if err != nil {
return fmt.Errorf("failed to get container list: %v", err)
}
display := p.cli.NewTableDisplay()
display.AddRow([]string{"Name", "ID", "Status", "Image", "Runtime"})
for _, c := range containers {
display.AddRow([]string{c.Names[0], c.ID[:6], c.Status, c.Image, c.HostConfig.Runtime})
}
display.Flush()
return nil
}
// psExample shows examples in ps command, and is used in auto-generated cli docs.
func psExample() string {
return `$ pouch ps
Name ID Status Image Runtime
1dad17 1dad17 stopped docker.io/library/busybox:latest runv
505571 505571 stopped docker.io/library/busybox:latest runc`
}