-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathlist.go
184 lines (149 loc) · 4.39 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright © 2019 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package taskrun
import (
"fmt"
"os"
"text/tabwriter"
"github.com/jonboulle/clockwork"
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
trhsort "github.com/tektoncd/cli/pkg/helper/taskrun/sort"
validate "github.com/tektoncd/cli/pkg/helper/validate"
"github.com/tektoncd/cli/pkg/printer"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
const (
emptyMsg = "No TaskRuns found"
)
type ListOptions struct {
Limit int
}
func listCommand(p cli.Params) *cobra.Command {
opts := &ListOptions{Limit: 0}
f := cliopts.NewPrintFlags("list")
eg := `List all TaskRuns in namespace 'bar':
tkn tr list -n bar
List all TaskRuns of Task 'foo' in namespace 'bar':
tkn taskrun list foo -n bar
`
c := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "Lists TaskRuns in a namespace",
Annotations: map[string]string{
"commandType": "main",
},
Example: eg,
RunE: func(cmd *cobra.Command, args []string) error {
var task string
if len(args) > 0 {
task = args[0]
}
if err := validate.NamespaceExists(p); err != nil {
return err
}
if opts.Limit < 0 {
fmt.Fprintf(os.Stderr, "Limit was %d but must be a positive number\n", opts.Limit)
return nil
}
trs, err := list(p, task, opts.Limit)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list taskruns from %s namespace \n", p.Namespace())
return err
}
output, err := cmd.LocalFlags().GetString("output")
if err != nil {
fmt.Fprint(os.Stderr, "Error: output option not set properly \n")
return err
}
if output != "" && trs != nil {
return printer.PrintObject(cmd.OutOrStdout(), trs, f)
}
stream := &cli.Stream{
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}
if trs != nil {
err = printFormatted(stream, trs, p.Time())
}
if err != nil {
fmt.Fprint(os.Stderr, "Failed to print taskruns \n")
return err
}
return nil
},
}
f.AddFlags(c)
c.Flags().IntVarP(&opts.Limit, "limit", "", 0, "limit taskruns listed (default: return all taskruns)")
return c
}
func list(p cli.Params, task string, limit int) (*v1alpha1.TaskRunList, error) {
cs, err := p.Clients()
if err != nil {
return nil, err
}
options := v1.ListOptions{}
if task != "" {
options = v1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/task=%s", task),
}
}
trc := cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace())
trs, err := trc.List(options)
if err != nil {
return nil, err
}
trslen := len(trs.Items)
if trslen != 0 {
trs.Items = trhsort.SortTaskRunsByStartTime(trs.Items)
}
// If greater than maximum amount of taskruns, return all taskruns by setting limit to default
if limit > trslen {
limit = 0
}
// Return all taskruns if limit is 0 or is same as trslen
if limit != 0 && trslen > limit {
trs.Items = trs.Items[0:limit]
}
// NOTE: this is required for -o json|yaml to work properly since
// tektoncd go client fails to set these; probably a bug
trs.GetObjectKind().SetGroupVersionKind(
schema.GroupVersionKind{
Version: "tekton.dev/v1alpha1",
Kind: "TaskRunList",
})
return trs, nil
}
func printFormatted(s *cli.Stream, trs *v1alpha1.TaskRunList, c clockwork.Clock) error {
if len(trs.Items) == 0 {
fmt.Fprintln(s.Err, emptyMsg)
return nil
}
w := tabwriter.NewWriter(s.Out, 0, 5, 3, ' ', tabwriter.TabIndent)
fmt.Fprintln(w, "NAME\tSTARTED\tDURATION\tSTATUS\t")
for _, tr := range trs.Items {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n",
tr.Name,
formatted.Age(tr.Status.StartTime, c),
formatted.Duration(tr.Status.StartTime, tr.Status.CompletionTime),
formatted.Condition(tr.Status.Conditions),
)
}
return w.Flush()
}