/
list.go
127 lines (105 loc) · 2.85 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
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
package app
import (
"fmt"
"strings"
"time"
"github.com/cppforlife/go-cli-ui/ui"
uitable "github.com/cppforlife/go-cli-ui/ui/table"
ctlapp "github.com/k14s/kapp/pkg/kapp/app"
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
"github.com/spf13/cobra"
)
type ListOptions struct {
ui ui.UI
depsFactory cmdcore.DepsFactory
NamespaceFlags cmdcore.NamespaceFlags
AllNamespaces bool
}
func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory) *ListOptions {
return &ListOptions{ui: ui, depsFactory: depsFactory}
}
func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"l", "ls"},
Short: "List all apps in a namespace",
RunE: func(_ *cobra.Command, _ []string) error { return o.Run() },
}
o.NamespaceFlags.Set(cmd, flagsFactory)
cmd.Flags().BoolVar(&o.AllNamespaces, "all-namespaces", false, "List apps in all namespaces")
return cmd
}
func (o *ListOptions) Run() error {
coreClient, err := o.depsFactory.CoreClient()
if err != nil {
return err
}
dynamicClient, err := o.depsFactory.DynamicClient()
if err != nil {
return err
}
nsName := o.NamespaceFlags.Name
tableTitle := fmt.Sprintf("Apps in namespace '%s'", o.NamespaceFlags.Name)
nsHeader := uitable.NewHeader("Namespace")
nsHeader.Hidden = true
if o.AllNamespaces {
nsName = ""
tableTitle = "Apps in all namespaces"
nsHeader.Hidden = false
}
apps := ctlapp.NewApps(nsName, coreClient, dynamicClient)
items, err := apps.List(nil)
if err != nil {
return err
}
table := uitable.Table{
Title: tableTitle,
Content: "apps",
Header: []uitable.Header{
nsHeader,
uitable.NewHeader("Name"),
uitable.NewHeader("Label"),
uitable.NewHeader("Namespaces"),
uitable.NewHeader("Last Change Successful"),
uitable.NewHeader("Last Change Age"),
},
SortBy: []uitable.ColumnSort{
{Column: 0, Asc: true},
{Column: 1, Asc: true},
},
}
for _, item := range items {
sel, err := item.LabelSelector()
if err != nil {
return err
}
row := []uitable.Value{
uitable.NewValueString(item.Namespace()),
uitable.NewValueString(item.Name()),
uitable.NewValueString(sel.String()),
}
lastChange, err := item.LastChange()
if err != nil {
return err
}
if lastChange != nil {
row = append(row,
uitable.NewValueString(strings.Join(lastChange.Meta().Namespaces, ",")),
uitable.ValueFmt{
V: cmdcore.NewValueUnknownBool(lastChange.Meta().Successful),
Error: lastChange.Meta().Successful == nil || *lastChange.Meta().Successful != true,
},
cmdcore.NewValueAge(lastChange.Meta().StartedAt),
)
} else {
row = append(row,
uitable.NewValueString(""),
cmdcore.NewValueUnknownBool(nil),
cmdcore.NewValueAge(time.Time{}),
)
}
table.Rows = append(table.Rows, row)
}
o.ui.PrintTable(table)
return nil
}