Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tkn triggerbinding describe command #912

Merged
merged 1 commit into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/cmd/tkn_triggerbinding.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Manage triggerbindings

* [tkn](tkn.md) - CLI for tekton pipelines
* [tkn triggerbinding delete](tkn_triggerbinding_delete.md) - Delete triggerbindings in a namespace
* [tkn triggerbinding describe](tkn_triggerbinding_describe.md) - Describes a triggerbinding in a namespace
* [tkn triggerbinding list](tkn_triggerbinding_list.md) - Lists triggerbindings in a namespace

49 changes: 49 additions & 0 deletions docs/cmd/tkn_triggerbinding_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## tkn triggerbinding describe

Describes a triggerbinding in a namespace

***Aliases**: desc*

### Usage

```
tkn triggerbinding describe
```

### Synopsis

Describes a triggerbinding in a namespace

### Examples

Describe a TriggerBinding of name 'foo' in namespace 'bar':

tkn triggerbinding describe foo -n bar

or

tkn tb desc foo -n bar


### Options

```
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-h, --help help for describe
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
```

### Options inherited from parent commands

```
-c, --context string name of the kubeconfig context to use (default: kubectl config current-context)
-k, --kubeconfig string kubectl config file (default: $HOME/.kube/config)
-n, --namespace string namespace to use (default: from $KUBECONFIG)
-C, --nocolour disable colouring (default: false)
```

### SEE ALSO

* [tkn triggerbinding](tkn_triggerbinding.md) - Manage triggerbindings

86 changes: 86 additions & 0 deletions docs/man/man1/tkn-triggerbinding-describe.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
.TH "TKN\-TRIGGERBINDING\-DESCRIBE" "1" "" "Auto generated by spf13/cobra" ""
.nh
.ad l


.SH NAME
.PP
tkn\-triggerbinding\-describe \- Describes a triggerbinding in a namespace


.SH SYNOPSIS
.PP
\fBtkn triggerbinding describe\fP


.SH DESCRIPTION
.PP
Describes a triggerbinding in a namespace


.SH OPTIONS
.PP
\fB\-\-allow\-missing\-template\-keys\fP[=true]
If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.

.PP
\fB\-h\fP, \fB\-\-help\fP[=false]
help for describe

.PP
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.

.PP
\fB\-\-template\fP=""
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]].


.SH OPTIONS INHERITED FROM PARENT COMMANDS
.PP
\fB\-c\fP, \fB\-\-context\fP=""
name of the kubeconfig context to use (default: kubectl config current\-context)

.PP
\fB\-k\fP, \fB\-\-kubeconfig\fP=""
kubectl config file (default: $HOME/.kube/config)

.PP
\fB\-n\fP, \fB\-\-namespace\fP=""
namespace to use (default: from $KUBECONFIG)

.PP
\fB\-C\fP, \fB\-\-nocolour\fP[=false]
disable colouring (default: false)


.SH EXAMPLE
.PP
Describe a TriggerBinding of name 'foo' in namespace 'bar':

.PP
.RS

.nf
tkn triggerbinding describe foo \-n bar

.fi
.RE

.PP
or

.PP
.RS

.nf
tkn tb desc foo \-n bar

.fi
.RE


.SH SEE ALSO
.PP
\fBtkn\-triggerbinding(1)\fP
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-triggerbinding.1
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Manage triggerbindings

.SH SEE ALSO
.PP
\fBtkn(1)\fP, \fBtkn\-triggerbinding\-delete(1)\fP, \fBtkn\-triggerbinding\-list(1)\fP
\fBtkn(1)\fP, \fBtkn\-triggerbinding\-delete(1)\fP, \fBtkn\-triggerbinding\-describe(1)\fP, \fBtkn\-triggerbinding\-list(1)\fP
151 changes: 151 additions & 0 deletions pkg/cmd/triggerbinding/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright © 2020 The Tekton Authors.
savitaashture marked this conversation as resolved.
Show resolved Hide resolved
//
// 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 triggerbinding

import (
"fmt"
"io"
"os"
"text/tabwriter"
"text/template"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/printer"
"github.com/tektoncd/cli/pkg/validate"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)

const describeTemplate = `{{decorate "bold" "Name"}}: {{ .TriggerBinding.Name }}
{{decorate "bold" "Namespace"}}: {{ .TriggerBinding.Namespace }}

{{decorate "params" ""}}{{decorate "underline bold" "Params\n"}}

{{- if eq (len .TriggerBinding.Spec.Params) 0 }}
No params
{{- else }}
NAME VALUE
{{- range $p := .TriggerBinding.Spec.Params }}
{{decorate "bullet" $p.Name }} {{ $p.Value.StringVal }}
{{- end }}
{{- end }}
`

func describeCommand(p cli.Params) *cobra.Command {
f := cliopts.NewPrintFlags("describe")
eg := `Describe a TriggerBinding of name 'foo' in namespace 'bar':

tkn triggerbinding describe foo -n bar

or

tkn tb desc foo -n bar
`

c := &cobra.Command{
Use: "describe",
Aliases: []string{"desc"},
Short: "Describes a triggerbinding in a namespace",
Example: eg,
Annotations: map[string]string{
"commandType": "main",
},
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
s := &cli.Stream{
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}

if err := validate.NamespaceExists(p); err != nil {
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 != "" {
return describeTriggerBindingOutput(cmd.OutOrStdout(), p, f, args[0])
}

return printTriggerBindingDescription(s, p, args[0])
},
}

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_triggerbindings")
f.AddFlags(c)
return c
}

func describeTriggerBindingOutput(w io.Writer, p cli.Params, f *cliopts.PrintFlags, name string) error {
cs, err := p.Clients()
if err != nil {
return err
}

tb, err := cs.Triggers.TriggersV1alpha1().TriggerBindings(p.Namespace()).Get(name, metav1.GetOptions{})
if err != nil {
return err
}

// NOTE: this is required for -o json|yaml to work properly since
// tektoncd go client fails to set these; probably a bug
tb.GetObjectKind().SetGroupVersionKind(
schema.GroupVersionKind{
Version: "triggers.tekton.dev",
Kind: "TriggerBinding",
})

return printer.PrintObject(w, tb, f)
}

func printTriggerBindingDescription(s *cli.Stream, p cli.Params, tbName string) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

tb, err := cs.Triggers.TriggersV1alpha1().TriggerBindings(p.Namespace()).Get(tbName, metav1.GetOptions{})
if err != nil {
fmt.Fprintf(s.Err, "failed to get triggerbinding %s\n", tbName)
return err
}

var data = struct {
TriggerBinding *v1alpha1.TriggerBinding
}{
TriggerBinding: tb,
}

funcMap := template.FuncMap{
"decorate": formatted.DecorateAttr,
}

w := tabwriter.NewWriter(s.Out, 0, 5, 3, ' ', tabwriter.TabIndent)
tparsed := template.Must(template.New("Describe Triggerbinding").Funcs(funcMap).Parse(describeTemplate))
if err = tparsed.Execute(w, data); err != nil {
fmt.Fprintf(s.Err, "Failed to execute template \n")
return err
}
return w.Flush()
}
Loading