-
Notifications
You must be signed in to change notification settings - Fork 236
/
describe.go
135 lines (118 loc) · 3.39 KB
/
describe.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
// 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 pipeline
import (
"fmt"
"io"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
const (
resHeader = "NAME\tTYPE\n"
resBody = "%s\t%s\n"
taskHeader = "NAME\tTASKREF\tRUNAFTER\n"
taskBody = "%s\t%s\t%v\n"
runsHeader = "NAME\tSTARTED\tDURATION\tSTATUS\n"
runsBody = "%s\t%s\t%s\t%s\n"
)
func describeCommand(p cli.Params) *cobra.Command {
f := cliopts.NewPrintFlags("describe")
c := &cobra.Command{
Use: "describe",
Aliases: []string{"desc"},
Short: "Describes a pipeline in a namespace",
Long: ``,
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return printPipelineDescription(cmd.OutOrStdout(), p, args[0])
},
}
f.AddFlags(c)
return c
}
func printPipelineDescription(out io.Writer, p cli.Params, pname string) error {
cs, err := p.Clients()
if err != nil {
return err
}
pipeline, err := cs.Tekton.TektonV1alpha1().Pipelines(p.Namespace()).Get(pname, metav1.GetOptions{})
if err != nil {
return err
}
w := tabwriter.NewWriter(out, 0, 5, 3, ' ', tabwriter.TabIndent)
fmt.Fprintf(out, "Name: %s\n", pname)
printResources(w, pipeline)
printTasks(w, pipeline)
printRuns(w, cs, p, pname)
return w.Flush()
}
func printResources(w *tabwriter.Writer, pipeline *v1alpha1.Pipeline) {
fmt.Fprintln(w, "\nResources")
if len(pipeline.Spec.Resources) == 0 {
fmt.Fprintln(w, "No resources found")
return
}
fmt.Fprintf(w, resHeader)
for _, val := range pipeline.Spec.Resources {
fmt.Fprintf(w, resBody,
val.Name,
val.Type,
)
}
}
func printTasks(w *tabwriter.Writer, pipeline *v1alpha1.Pipeline) {
fmt.Fprintln(w, "\nTasks")
if len(pipeline.Spec.Tasks) == 0 {
fmt.Fprintln(w, "No tasks found")
return
}
fmt.Fprintf(w, taskHeader)
for _, val := range pipeline.Spec.Tasks {
fmt.Fprintf(w, taskBody,
val.Name,
val.TaskRef.Name,
val.RunAfter,
)
}
}
func printRuns(w *tabwriter.Writer, cs *cli.Clients, p cli.Params, pname string) {
fmt.Fprintln(w, "\nRuns")
opts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/pipeline=%s", pname),
}
runs, err := cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).List(opts)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
if len(runs.Items) == 0 {
fmt.Fprintln(w, "No runs found")
return
}
fmt.Fprintf(w, runsHeader)
for _, run := range runs.Items {
fmt.Fprintf(w, runsBody,
run.Name,
formatted.Age(*run.Status.StartTime, p.Time()),
formatted.Duration(run.Status.StartTime, run.Status.CompletionTime),
formatted.Condition(run.Status.Conditions[0]),
)
}
}