From cefe60d77ed4c2b6678b8cdfd665a2a1ed49914f Mon Sep 17 00:00:00 2001 From: Piyush Garg Date: Wed, 24 Jul 2019 22:05:37 +0530 Subject: [PATCH] Add type flag to resource list This will add a flag type to resource list command which will list the resources filter by the given type It will show error in case of invalid type Add docs and tests Fix https://github.com/tektoncd/cli/issues/147 --- docs/cmd/tkn_resource_list.md | 1 + docs/cmd/tkn_version.md | 3 +- docs/man/man1/tkn-resource-list.1 | 4 +++ docs/man/man1/tkn-version.1 | 4 +++ pkg/cmd/pipelineresource/list.go | 39 +++++++++++++++++++--- pkg/cmd/pipelineresource/list_test.go | 48 +++++++++++++++++++++++++-- 6 files changed, 92 insertions(+), 7 deletions(-) diff --git a/docs/cmd/tkn_resource_list.md b/docs/cmd/tkn_resource_list.md index 224635d48..dc4c6a70a 100644 --- a/docs/cmd/tkn_resource_list.md +++ b/docs/cmd/tkn_resource_list.md @@ -28,6 +28,7 @@ tkn pre list -n foo", -h, --help help for list -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]. + -t, --type string Pipeline resource type ``` ### Options inherited from parent commands diff --git a/docs/cmd/tkn_version.md b/docs/cmd/tkn_version.md index ee347afa9..34b66e1e0 100644 --- a/docs/cmd/tkn_version.md +++ b/docs/cmd/tkn_version.md @@ -15,7 +15,8 @@ Prints version information ### Options ``` - -h, --help help for version + -c, --check check if a newer version is available + -h, --help help for version ``` ### SEE ALSO diff --git a/docs/man/man1/tkn-resource-list.1 b/docs/man/man1/tkn-resource-list.1 index 83aff61d1..dae4abcc6 100644 --- a/docs/man/man1/tkn-resource-list.1 +++ b/docs/man/man1/tkn-resource-list.1 @@ -36,6 +36,10 @@ Lists pipeline resources in a namespace 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]]. +.PP +\fB\-t\fP, \fB\-\-type\fP="" + Pipeline resource type + .SH OPTIONS INHERITED FROM PARENT COMMANDS .PP diff --git a/docs/man/man1/tkn-version.1 b/docs/man/man1/tkn-version.1 index 7e374ec6a..b1dd07586 100644 --- a/docs/man/man1/tkn-version.1 +++ b/docs/man/man1/tkn-version.1 @@ -19,6 +19,10 @@ Prints version information .SH OPTIONS +.PP +\fB\-c\fP, \fB\-\-check\fP[=false] + check if a newer version is available + .PP \fB\-h\fP, \fB\-\-help\fP[=false] help for version diff --git a/pkg/cmd/pipelineresource/list.go b/pkg/cmd/pipelineresource/list.go index 06cf9cf51..e94574b92 100644 --- a/pkg/cmd/pipelineresource/list.go +++ b/pkg/cmd/pipelineresource/list.go @@ -35,7 +35,13 @@ const ( msgNoPREsFound = "No pipelineresources found." ) +type ListOptions struct { + Type string +} + func listCommand(p cli.Params) *cobra.Command { + + opts := &ListOptions{Type: ""} f := cliopts.NewPrintFlags("list") eg := ` # List all PipelineResources in a namespaces 'foo' @@ -54,7 +60,19 @@ tkn pre list -n foo", if err != nil { return err } - pres, err := list(cs.Tekton, p.Namespace()) + + valid := false + for _, allowed := range v1alpha1.AllResourceTypes { + if string(allowed) == opts.Type || opts.Type == "" { + valid = true + break + } + } + if !valid { + return fmt.Errorf("Failed to list pipelineresources. Invalid resource type %s", opts.Type) + } + + pres, err := list(cs.Tekton, p.Namespace(), opts.Type) stream := &cli.Stream{ Out: cmd.OutOrStdout(), Err: cmd.OutOrStderr(), @@ -84,19 +102,23 @@ tkn pre list -n foo", } f.AddFlags(cmd) + cmd.Flags().StringVarP(&opts.Type, "type", "t", "", "Pipeline resource type") return cmd } -func list(client versioned.Interface, namespace string) (*v1alpha1.PipelineResourceList, error) { +func list(client versioned.Interface, namespace string, resourceType string) (*v1alpha1.PipelineResourceList, error) { - options := v1.ListOptions{} prec := client.TektonV1alpha1().PipelineResources(namespace) - pres, err := prec.List(options) + pres, err := prec.List(v1.ListOptions{}) if err != nil { return nil, err } + if resourceType != "" { + pres.Items = filterByType(pres.Items, resourceType) + } + // NOTE: this is required for -o json|yaml to work properly since // tektoncd go client fails to set these; probably a bug pres.GetObjectKind().SetGroupVersionKind( @@ -141,3 +163,12 @@ func details(pre v1alpha1.PipelineResource) string { return "---" } + +func filterByType(resources []v1alpha1.PipelineResource, resourceType string) (ret []v1alpha1.PipelineResource) { + for _, resource := range resources { + if string(resource.Spec.Type) == resourceType { + ret = append(ret, resource) + } + } + return +} diff --git a/pkg/cmd/pipelineresource/list_test.go b/pkg/cmd/pipelineresource/list_test.go index 49a04f6d8..9ab215bcf 100644 --- a/pkg/cmd/pipelineresource/list_test.go +++ b/pkg/cmd/pipelineresource/list_test.go @@ -26,7 +26,7 @@ import ( tb "github.com/tektoncd/pipeline/test/builder" ) -func TestPipelineResources(t *testing.T) { +func TestPipelineResourceList(t *testing.T) { pres := []*v1alpha1.PipelineResource{ tb.PipelineResource("test-1", "test-ns-1", @@ -77,6 +77,36 @@ func TestPipelineResources(t *testing.T) { "", }, }, + { + name: "Single Pipeline Resource by type", + command: command(t, pres), + args: []string{"list", "-n", "test-ns-1", "-t", "git"}, + expected: []string{ + "NAME TYPE DETAILS", + "test-2 git url: git@github.com:tektoncd/cli.git", + "", + }, + }, + { + name: "Multiple Pipeline Resource by type", + command: command(t, pres), + args: []string{"list", "-n", "test-ns-1", "-t", "image"}, + expected: []string{ + "NAME TYPE DETAILS", + "test-1 image URL: quey.io/tekton/controller", + "test-3 image ---", + "", + }, + }, + { + name: "Empty Pipeline Resource by type", + command: command(t, pres), + args: []string{"list", "-n", "test-ns-1", "-t", "storage"}, + expected: []string{ + "No pipelineresources found.", + "", + }, + }, { name: "By template", command: command(t, pres), @@ -103,7 +133,7 @@ func TestPipelineResources(t *testing.T) { } -func TestPipelineResource_empty(t *testing.T) { +func TestPipelineResourceList_empty(t *testing.T) { cs, _ := pipelinetest.SeedTestData(t, pipelinetest.Data{}) p := &test.Params{Tekton: cs.Pipeline} pipelineresource := Command(p) @@ -111,6 +141,20 @@ func TestPipelineResource_empty(t *testing.T) { tu.AssertOutput(t, msgNoPREsFound+"\n", out) } +func TestPipelineResourceList_invalidType(t *testing.T) { + cs, _ := pipelinetest.SeedTestData(t, pipelinetest.Data{}) + p := &test.Params{Tekton: cs.Pipeline} + c := Command(p) + + _, err := tu.ExecuteCommand(c, "list", "-n", "ns", "-t", "registry") + + if err == nil { + t.Error("Expecting an error but it's empty") + } + + tu.AssertOutput(t, "Failed to list pipelineresources. Invalid resource type registry", err.Error()) +} + func command(t *testing.T, pres []*v1alpha1.PipelineResource) *cobra.Command { cs, _ := pipelinetest.SeedTestData(t, pipelinetest.Data{PipelineResources: pres}) p := &test.Params{Tekton: cs.Pipeline}