-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlist.go
94 lines (76 loc) · 2.43 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
// Copyright 2020-Present VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package build
import (
"sort"
"github.com/pivotal/kpack/pkg/apis/build/v1alpha2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/buildpacks-community/kpack-cli/pkg/build"
"github.com/buildpacks-community/kpack-cli/pkg/commands"
"github.com/buildpacks-community/kpack-cli/pkg/k8s"
)
func NewListCommand(clientSetProvider k8s.ClientSetProvider) *cobra.Command {
var (
namespace string
allNamespaces bool
)
cmd := &cobra.Command{
Use: "list [image-resource-name]",
Short: "List builds",
Long: `Prints a table of the most important information about builds in the provided namespace.
The namespace defaults to the kubernetes current-context namespace.`,
Example: "kp build list\nkp build list my-image\nkp build list my-image -n my-namespace\nkp build list -A",
Args: commands.OptionalArgsWithUsage(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
cs, err := clientSetProvider.GetClientSet(namespace)
if err != nil {
return err
}
opts := metav1.ListOptions{}
if len(args) > 0 {
opts.LabelSelector = v1alpha2.ImageLabel + "=" + args[0]
}
var buildNamespace string
if allNamespaces {
buildNamespace = ""
} else {
buildNamespace = cs.Namespace
}
buildList, err := cs.KpackClient.KpackV1alpha2().Builds(buildNamespace).List(cmd.Context(), opts)
if err != nil {
return err
}
if len(buildList.Items) == 0 {
return errors.New("no builds found")
} else {
sort.Slice(buildList.Items, build.Sort(buildList.Items))
return displayBuildsTable(cmd, buildList)
}
},
}
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "kubernetes namespace")
cmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "Return objects found in all namespaces")
return cmd
}
func displayBuildsTable(cmd *cobra.Command, buildList *v1alpha2.BuildList) error {
writer, err := commands.NewTableWriter(cmd.OutOrStdout(), "Build", "Status", "Built Image", "Reason", "Image Resource")
if err != nil {
return err
}
for _, bld := range buildList.Items {
err := writer.AddRow(
bld.Labels[v1alpha2.BuildNumberLabel],
getStatus(bld),
bld.Status.LatestImage,
getTruncatedReason(bld),
bld.Labels[v1alpha2.ImageLabel],
)
if err != nil {
return err
}
}
return writer.Write()
}