-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathdescribe.go
151 lines (129 loc) · 3.96 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 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 pipelinerun
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/options"
pipelinerunpkg "github.com/tektoncd/cli/pkg/pipelinerun"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
const (
defaultDescribeLimit = 5
)
func describeCommand(p cli.Params) *cobra.Command {
f := cliopts.NewPrintFlags("describe")
opts := &options.DescribeOptions{Params: p}
eg := `Describe a PipelineRun of name 'foo' in namespace 'bar':
tkn pipelinerun describe foo -n bar
or
tkn pr desc foo -n bar
`
c := &cobra.Command{
Use: "describe",
Aliases: []string{"desc"},
Short: "Describe a PipelineRun in a namespace",
Example: eg,
SilenceUsage: true,
Annotations: map[string]string{
"commandType": "main",
},
ValidArgsFunction: formatted.ParentCompletion,
RunE: func(cmd *cobra.Command, args []string) error {
s := &cli.Stream{
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}
output, err := cmd.LocalFlags().GetString("output")
if err != nil {
return fmt.Errorf("output option not set properly: %v", err)
}
if !opts.Fzf {
if _, ok := os.LookupEnv("TKN_USE_FZF"); ok {
opts.Fzf = true
}
}
cs, err := p.Clients()
if err != nil {
return err
}
if len(args) == 0 {
lOpts := metav1.ListOptions{}
if !opts.Last {
prs, err := pipelinerunpkg.GetAllPipelineRuns(pipelineRunGroupResource, lOpts, cs, p.Namespace(), opts.Limit, p.Time())
if err != nil {
return err
}
if len(prs) == 1 {
opts.PipelineRunName = strings.Fields(prs[0])[0]
} else {
err = askPipelineRunName(opts, prs)
if err != nil {
return err
}
}
} else {
prs, err := pipelinerunpkg.GetAllPipelineRuns(pipelineRunGroupResource, lOpts, cs, p.Namespace(), 1, p.Time())
if err != nil {
return err
}
if len(prs) == 0 {
fmt.Fprintf(s.Out, "No PipelineRuns present in namespace %s\n", opts.Params.Namespace())
return nil
}
opts.PipelineRunName = strings.Fields(prs[0])[0]
}
} else {
opts.PipelineRunName = args[0]
}
if output != "" {
printer, err := f.ToPrinter()
if err != nil {
return err
}
return actions.PrintObjectV1(pipelineRunGroupResource, opts.PipelineRunName, cmd.OutOrStdout(), cs, printer, p.Namespace())
}
return pipelinerunpkg.PrintPipelineRunDescription(s.Out, cs, opts.Params.Namespace(), opts.PipelineRunName, opts.Params.Time())
},
}
c.Flags().BoolVarP(&opts.Last, "last", "L", false, "show description for last PipelineRun")
c.Flags().IntVarP(&opts.Limit, "limit", "", defaultDescribeLimit, "lists number of PipelineRuns when selecting a PipelineRun to describe")
c.Flags().BoolVarP(&opts.Fzf, "fzf", "F", false, "use fzf to select a PipelineRun to describe")
f.AddFlags(c)
return c
}
func askPipelineRunName(opts *options.DescribeOptions, prs []string) error {
err := opts.ValidateOpts()
if err != nil {
return err
}
if len(prs) == 0 {
return fmt.Errorf("no PipelineRuns found")
}
if opts.Fzf {
err = opts.FuzzyAsk(options.ResourceNamePipelineRun, prs)
} else {
err = opts.Ask(options.ResourceNamePipelineRun, prs)
}
if err != nil {
return err
}
return nil
}