-
Notifications
You must be signed in to change notification settings - Fork 17
/
list.go
94 lines (84 loc) · 2.23 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
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
package imagerunner
import (
"context"
"errors"
"fmt"
"time"
"github.com/jedib0t/go-pretty/v6/table"
cmds "github.com/saucelabs/saucectl/internal/cmd"
"github.com/saucelabs/saucectl/internal/http"
"github.com/saucelabs/saucectl/internal/segment"
"github.com/saucelabs/saucectl/internal/usage"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func ListCommand() *cobra.Command {
var out string
cmd := &cobra.Command{
Use: "list",
Short: "Returns the list of containers",
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
err := http.CheckProxy()
if err != nil {
return fmt.Errorf("invalid HTTP_PROXY value")
}
tracker := segment.DefaultTracker
go func() {
tracker.Collect(
cases.Title(language.English).String(cmds.FullName(cmd)),
usage.Properties{}.SetFlags(cmd.Flags()),
)
_ = tracker.Close()
}()
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return list(out)
},
}
flags := cmd.PersistentFlags()
flags.StringVarP(&out, "out", "o", "text", "Output format to the console. Options: text, json.")
return cmd
}
func list(outputFormat string) error {
containers, err := imagerunnerClient.ListContainers(context.Background())
if err != nil {
return fmt.Errorf("failed to get container list: %v", err)
}
switch outputFormat {
case "json":
if err := renderJSON(containers); err != nil {
return fmt.Errorf("failed to render output: %v", err)
}
case "text":
renderContainersTable(containers)
default:
return errors.New("unknown output format")
}
return nil
}
func renderContainersTable(containers http.ContainersResp) {
if len(containers.Items) == 0 {
println("Cannot find any containers")
return
}
t := table.NewWriter()
t.SetStyle(defaultTableStyle)
t.SuppressEmptyColumns()
t.AppendHeader(table.Row{
"ID", "Image", "Status", "CreationTime", "TerminationTime",
})
for _, c := range containers.Items {
// the order of values must match the order of the header
t.AppendRow(table.Row{
c.ID,
c.Image,
c.Status,
time.Unix(c.CreationTime, 0).Format(time.RFC3339),
time.Unix(c.TerminationTime, 0).Format(time.RFC3339),
})
}
fmt.Println(t.Render())
}