Skip to content

Commit

Permalink
Add type flag to resource list
Browse files Browse the repository at this point in the history
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 tektoncd#147
  • Loading branch information
piyush-garg authored and tekton-robot committed Jul 24, 2019
1 parent 72224f4 commit cefe60d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/cmd/tkn_resource_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion docs/cmd/tkn_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-resource-list.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-version.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 35 additions & 4 deletions pkg/cmd/pipelineresource/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
48 changes: 46 additions & 2 deletions pkg/cmd/pipelineresource/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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: [email protected]: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),
Expand All @@ -103,14 +133,28 @@ 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)
out, _ := test.ExecuteCommand(pipelineresource, "list", "-n", "test-ns-3")
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}
Expand Down

0 comments on commit cefe60d

Please sign in to comment.