Skip to content

Commit

Permalink
Add description command for condition
Browse files Browse the repository at this point in the history
This will add description command for
condition. It also supports output
in different format

Add docs and test

Part of tektoncd#594
  • Loading branch information
piyush-garg committed Apr 27, 2020
1 parent a403fbc commit e5432fd
Show file tree
Hide file tree
Showing 17 changed files with 637 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/cmd/tkn_condition.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Manage conditions

* [tkn](tkn.md) - CLI for tekton pipelines
* [tkn condition delete](tkn_condition_delete.md) - Delete a condition in a namespace
* [tkn condition describe](tkn_condition_describe.md) - Describe Conditions in a namespace
* [tkn condition list](tkn_condition_list.md) - Lists conditions in a namespace

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

Describe Conditions in a namespace

***Aliases**: desc*

### Usage

```
tkn condition describe
```

### Synopsis

Describe Conditions in a namespace

### Examples

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

tkn condition describe foo -n bar

or

tkn cond 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 condition](tkn_condition.md) - Manage conditions

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


.SH NAME
.PP
tkn\-condition\-describe \- Describe Conditions in a namespace


.SH SYNOPSIS
.PP
\fBtkn condition describe\fP


.SH DESCRIPTION
.PP
Describe Conditions 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 Condition of name 'foo' in namespace 'bar':

.PP
.RS

.nf
tkn condition describe foo \-n bar

.fi
.RE

.PP
or

.PP
.RS

.nf
tkn cond desc foo \-n bar

.fi
.RE


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

.SH SEE ALSO
.PP
\fBtkn(1)\fP, \fBtkn\-condition\-delete(1)\fP, \fBtkn\-condition\-list(1)\fP
\fBtkn(1)\fP, \fBtkn\-condition\-delete(1)\fP, \fBtkn\-condition\-describe(1)\fP, \fBtkn\-condition\-list(1)\fP
3 changes: 2 additions & 1 deletion pkg/cmd/condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func Command(p cli.Params) *cobra.Command {

flags.AddTektonOptions(cmd)
cmd.AddCommand(
listCommand(p),
deleteCommand(p),
describeCommand(p),
listCommand(p),
)
return cmd
}
183 changes: 183 additions & 0 deletions pkg/cmd/condition/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Copyright © 2020 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 condition

import (
"fmt"
"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/pipeline/pkg/apis/pipeline/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)

const templ = `{{decorate "bold" "Name"}}: {{ .Condition.Name }}
{{decorate "bold" "Namespace"}}: {{ .Condition.Namespace }}
{{- if ne .Condition.Spec.Description "" }}
{{decorate "bold" "Description"}}: {{ .Condition.Spec.Description }}
{{- end }}
{{ decorate "check" ""}}{{decorate "underline bold" "Check\n"}}
{{decorate "bold" "Name"}}: {{ .Condition.Spec.Check.Name }}
{{decorate "bold" "Image"}}: {{ .Condition.Spec.Check.Image }}
{{- if .Condition.Spec.Check.Command }}
{{decorate "bold" "Command"}}: {{ .Condition.Spec.Check.Command }}
{{- end }}
{{- if .Condition.Spec.Check.Args }}
{{decorate "bold" "Args"}}: {{ .Condition.Spec.Check.Args }}
{{- end }}
{{- if ne .Condition.Spec.Check.Script "" }}
{{decorate "bold" "Script"}}: {{ .Condition.Spec.Check.Script }}
{{- end }}
{{decorate "resources" ""}}{{decorate "underline bold" "Resources\n"}}
{{- $rl := len .Condition.Spec.Resources }}{{ if eq $rl 0 }}
No resources
{{- else }}
NAME TYPE
{{- range $r := .Condition.Spec.Resources }}
{{decorate "bullet" $r.Name }} {{ $r.Type }}
{{- end }}
{{- end }}
{{decorate "params" ""}}{{decorate "underline bold" "Params\n"}}
{{- $l := len .Condition.Spec.Params }}{{ if eq $l 0 }}
No params
{{- else }}
NAME TYPE DESCRIPTION DEFAULT VALUE
{{- range $p := .Condition.Spec.Params }}
{{- if not $p.Default }}
{{decorate "bullet" $p.Name }} {{ $p.Type }} {{ formatDesc $p.Description }} {{ "---" }}
{{- else }}
{{- if eq $p.Type "string" }}
{{decorate "bullet" $p.Name }} {{ $p.Type }} {{ formatDesc $p.Description }} {{ $p.Default.StringVal }}
{{- else }}
{{decorate "bullet" $p.Name }} {{ $p.Type }} {{ formatDesc $p.Description }} {{ $p.Default.ArrayVal }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
`

func describeCommand(p cli.Params) *cobra.Command {
f := cliopts.NewPrintFlags("describe")
eg := `Describe a Condition of name 'foo' in namespace 'bar':
tkn condition describe foo -n bar
or
tkn cond desc foo -n bar
`

c := &cobra.Command{
Use: "describe",
Aliases: []string{"desc"},
Short: "Describe Conditions in a namespace",
Example: eg,
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
Annotations: map[string]string{
"commandType": "main",
},
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
}

condition, err := getCondition(p, args[0])
if err != nil {
return err
}

if output != "" {
if err := printer.PrintObject(cmd.OutOrStdout(), condition, f); err != nil {
return err
}
return nil
}

return printConditionDescription(s, p, condition)
},
}

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

func getCondition(p cli.Params, name string) (*v1alpha1.Condition, error) {
cs, err := p.Clients()
if err != nil {
return nil, fmt.Errorf("failed to create tekton client")
}

condition, err := cs.Tekton.TektonV1alpha1().Conditions(p.Namespace()).Get(name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to find condition %q", name)
}

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

return condition, nil
}

func printConditionDescription(s *cli.Stream, p cli.Params, condition *v1alpha1.Condition) error {
var data = struct {
Condition *v1alpha1.Condition
Params cli.Params
}{
Condition: condition,
Params: p,
}

w := tabwriter.NewWriter(s.Out, 0, 5, 3, ' ', tabwriter.TabIndent)
FuncMap := template.FuncMap{
"decorate": formatted.DecorateAttr,
"formatDesc": formatted.FormatDesc,
}
t := template.Must(template.New("Describe Condition").Funcs(FuncMap).Parse(templ))

if err := t.Execute(w, data); err != nil {
fmt.Fprintf(s.Err, "Failed to execute template")
return err
}
return w.Flush()
}
Loading

0 comments on commit e5432fd

Please sign in to comment.