Skip to content

Commit

Permalink
Add platform support to hub cli
Browse files Browse the repository at this point in the history
- add platforms option to search
- show platforms in the resource info
- show platforms in search results for wide format

Signed-off-by: Yulia Gaponenko <yulia.gaponenko1@de.ibm.com>
  • Loading branch information
barthy1 authored and tekton-robot committed Sep 21, 2021
1 parent 4bc68fe commit 3c53fab
Show file tree
Hide file tree
Showing 30 changed files with 782 additions and 18 deletions.
7 changes: 7 additions & 0 deletions api/pkg/cli/cmd/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ const resTemplate = `{{ icon "name" }}Name: {{ .Resource.Name }}
{{- end }}
{{- end }}
{{ $ps := len .ResVersion.Platforms }}{{ if ne $ps 0 }}
{{- icon "platforms" }}Platforms
{{- range $p := .ResVersion.Platforms }}
{{ icon "bullet" }}{{ $p.Name }}
{{- end }}
{{- end }}
{{ icon "install" }}Install Command:
{{ formatInstallCMD .Resource .ResVersion .Latest }}
`
Expand Down
24 changes: 24 additions & 0 deletions api/pkg/cli/cmd/info/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ var taskResWithLatestVersion = &res.ResourceVersionData{
RawURL: "http://raw.github.url/foo-bar/",
WebURL: "http://web.github.com/foo-bar/",
UpdatedAt: "2020-01-01 12:00:00 +0000 UTC",
Platforms: []*res.Platform{
{
ID: 3,
Name: "linux/amd64",
},
},
Resource: &res.ResourceData{
ID: 2,
Name: "foo-bar",
Expand All @@ -64,6 +70,12 @@ var taskResWithLatestVersion = &res.ResourceVersionData{
Name: "foo",
},
},
Platforms: []*res.Platform{
{
ID: 3,
Name: "linux/amd64",
},
},
},
}

Expand All @@ -77,6 +89,12 @@ var taskResWithOldVersion = &res.ResourceVersionData{
RawURL: "http://raw.github.url/foo-bar/",
WebURL: "http://web.github.com/foo-bar/",
UpdatedAt: "2020-01-01 12:00:00 +0000 UTC",
Platforms: []*res.Platform{
{
ID: 2,
Name: "linux/s390x",
},
},
Resource: &res.ResourceData{
ID: 2,
Name: "foo-bar",
Expand All @@ -99,6 +117,12 @@ var taskResWithOldVersion = &res.ResourceVersionData{
Name: "foo",
},
},
Platforms: []*res.Platform{
{
ID: 2,
Name: "linux/s390x",
},
},
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
🏷 Tags
∙ foo

💻 Platforms
∙ linux/amd64

⚒ Install Command:
tkn hub install task foo-bar --version 0.2
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
🏷 Tags
∙ foo

💻 Platforms
∙ linux/s390x

⚒ Install Command:
tkn hub install task foo-bar --version 0.1
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
🏷 Tags
∙ foo

💻 Platforms
∙ linux/amd64

⚒ Install Command:
tkn hub install task foo-bar --version 0.2
13 changes: 9 additions & 4 deletions api/pkg/cli/cmd/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import (
const resTemplate = `{{- $rl := len .Resources }}{{ if eq $rl 0 -}}
No Resources found
{{ else -}}
NAME KIND CATALOG DESCRIPTION TAGS CATEGORIES
NAME KIND CATALOG DESCRIPTION PLATFORMS TAGS CATEGORIES
{{ range $_, $r := .Resources -}}
{{ formatName $r.Name $r.LatestVersion.Version }} {{ $r.Kind }} {{ formatCatalogName $r.Catalog.Name }} {{ formatDesc $r.LatestVersion.Description 40 }} {{ formatTags $r.Tags }} {{ formatCategories $r.Categories }}
{{ formatName $r.Name $r.LatestVersion.Version }} {{ $r.Kind }} {{ formatCatalogName $r.Catalog.Name }} {{ formatDesc $r.LatestVersion.Description 40 }} {{ formatPlatforms $r.LatestVersion.Platforms }} {{ formatTags $r.Tags }} {{ formatCategories $r.Categories }}
{{ end }}
{{- end -}}
`
Expand All @@ -55,6 +55,7 @@ var (
"formatDesc": formatter.FormatDesc,
"formatTags": formatter.FormatTags,
"formatCategories": formatter.FormatCategories,
"formatPlatforms": formatter.FormatPlatforms,
}
tmpl = template.Must(template.New("List Resources").Funcs(funcMap).Parse(resTemplate))
minTmpl = template.Must(template.New("List Resources").Funcs(funcMap).Parse(minResTemplate))
Expand All @@ -68,6 +69,7 @@ type options struct {
tags []string
categories []string
kinds []string
platforms []string
args []string
}

Expand Down Expand Up @@ -108,6 +110,7 @@ func Command(cli app.CLI) *cobra.Command {
cmd.Flags().StringArrayVar(&opts.kinds, "kinds", nil, "Accepts a comma separated list of kinds")
cmd.Flags().StringArrayVar(&opts.tags, "tags", nil, "Accepts a comma separated list of tags")
cmd.Flags().StringArrayVar(&opts.categories, "categories", nil, "Accepts a comma separated list of categories")
cmd.Flags().StringArrayVar(&opts.platforms, "platforms", nil, "Accepts a comma separated list of platforms")
cmd.Flags().StringVarP(&opts.output, "output", "o", "table", "Accepts output format: [table, json, wide]")

return cmd
Expand All @@ -126,6 +129,7 @@ func (opts *options) run() error {
Kinds: opts.kinds,
Tags: opts.tags,
Categories: opts.categories,
Platforms: opts.platforms,
Match: opts.match,
Limit: opts.limit,
})
Expand Down Expand Up @@ -155,8 +159,8 @@ func (opts *options) run() error {

func (opts *options) validate() error {

if flag.AllEmpty(opts.args, opts.kinds, opts.tags, opts.categories) {
return fmt.Errorf("please specify a resource name, --tags, --categories or --kinds flag to search")
if flag.AllEmpty(opts.args, opts.kinds, opts.tags, opts.categories, opts.platforms) {
return fmt.Errorf("please specify a resource name, --tags, --platforms, --categories or --kinds flag to search")
}

if err := flag.InList("match", opts.match, []string{"contains", "exact"}); err != nil {
Expand All @@ -170,6 +174,7 @@ func (opts *options) validate() error {
opts.kinds = flag.TrimArray(opts.kinds)
opts.tags = flag.TrimArray(opts.tags)
opts.categories = flag.TrimArray(opts.categories)
opts.platforms = flag.TrimArray(opts.platforms)

for _, k := range opts.kinds {
if !parser.IsSupportedKind(k) {
Expand Down
10 changes: 9 additions & 1 deletion api/pkg/cli/cmd/search/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ var res1 = &res.ResourceData{
RawURL: "http://raw.github.url/foo/",
WebURL: "http://web.github.com/foo/",
UpdatedAt: "2020-01-01 12:00:00 +0000 UTC",
Platforms: []*res.Platform{
{ID: 3, Name: "linux/amd64"},
{ID: 1, Name: "linux/s390x"},
},
},
Tags: []*res.Tag{
{ID: 3, Name: "tag3"},
{ID: 1, Name: "tag1"},
},
Platforms: []*res.Platform{
{ID: 3, Name: "linux/amd64"},
{ID: 1, Name: "linux/s390x"},
},
}

var res2 = &res.ResourceData{
Expand Down Expand Up @@ -106,7 +114,7 @@ func TestValidate_ErrorCases(t *testing.T) {
opt := options{}
err := opt.validate()
assert.Error(t, err)
assert.EqualError(t, err, "please specify a resource name, --tags, --categories or --kinds flag to search")
assert.EqualError(t, err, "please specify a resource name, --tags, --platforms, --categories or --kinds flag to search")

opt = options{
kinds: []string{"abc"},
Expand Down
6 changes: 3 additions & 3 deletions api/pkg/cli/cmd/search/testdata/TestSearch_TableFormat.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
NAME KIND CATALOG DESCRIPTION TAGS CATEGORIES
foo (0.1) Task Tekton Description for task abc version 0.1 tag3, tag1 ---
foo-bar (0.2) Pipeline Foo Description for pipeline foo-bar versio... --- ---
NAME KIND CATALOG DESCRIPTION PLATFORMS TAGS CATEGORIES
foo (0.1) Task Tekton Description for task abc version 0.1 linux/amd64, linux/s390x tag3, tag1 ---
foo-bar (0.2) Pipeline Foo Description for pipeline foo-bar versio... --- --- ---
17 changes: 17 additions & 0 deletions api/pkg/cli/formatter/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var icons = map[string]string{
"minPipelineVersion": "🗒 ",
"rating": "⭐ ️",
"tags": "🏷 ",
"platforms": "💻 ",
"install": "⚒ ",
"categories": "🏷️ ️",
}
Expand Down Expand Up @@ -90,6 +91,22 @@ func FormatCategories(categories []*client.CategoryResponseBody) string {
return sb.String()
}

// FormatPlatforms returns list of platforms seperated by comma
func FormatPlatforms(platforms []*client.PlatformResponseBody) string {
var sb strings.Builder
if len(platforms) == 0 {
return "---"
}
for i, p := range platforms {
if i != len(platforms)-1 {
sb.WriteString(strings.Trim(*p.Name, " ") + ", ")
continue
}
sb.WriteString(strings.Trim(*p.Name, " "))
}
return sb.String()
}

// WrapText returns description broken down in multiple lines with
// max width passed to it
// titleLength would be the length of title on left hand side before the
Expand Down
22 changes: 22 additions & 0 deletions api/pkg/cli/formatter/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ func TestFormatCategories(t *testing.T) {
assert.Equal(t, "---", categories)
}

func TestFormatPlatforms(t *testing.T) {

pName1 := "linux/amd64"
pName2 := "linux/s390x"

pRes := []*client.PlatformResponseBody{
{
Name: &pName1,
},
{
Name: &pName2,
},
}

platforms := FormatPlatforms(pRes)
assert.Equal(t, "linux/amd64, linux/s390x", platforms)

// No Tags
platforms = FormatTags(nil)
assert.Equal(t, "---", platforms)
}

func TestWrapText(t *testing.T) {

// Description of resource with just summa
Expand Down
4 changes: 4 additions & 0 deletions api/pkg/cli/hub/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type SearchOption struct {
Kinds []string
Tags []string
Categories []string
Platforms []string
Match string
Limit uint
Catalog string
Expand Down Expand Up @@ -95,6 +96,9 @@ func (so SearchOption) Endpoint() string {
if len(so.Categories) != 0 {
addArraytoURL("categories", so.Categories, v)
}
if len(so.Platforms) != 0 {
addArraytoURL("platforms", so.Platforms, v)
}
if so.Limit != 0 {
v.Set("limit", strconv.FormatUint(uint64(so.Limit), 10))
}
Expand Down
1 change: 1 addition & 0 deletions api/pkg/service/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (s *service) Query(ctx context.Context, p *resource.QueryPayload) (*resourc
Catalogs: p.Catalogs,
Categories: p.Categories,
Tags: p.Tags,
Platforms: p.Platforms,
Limit: p.Limit,
Match: p.Match,
}
Expand Down
17 changes: 17 additions & 0 deletions api/pkg/service/resource/resource_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ func TestQueryWithTags_Http(t *testing.T) {
})
}

func TestQueryWithPlatforms_Http(t *testing.T) {
tc := testutils.Setup(t)
testutils.LoadFixtures(t, tc.FixturePath())

QueryChecker(tc).Test(t, http.MethodGet, "/query?platforms=linux/s390x&platforms=linux/amd64").Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

res, err := testutils.FormatJSON(b)
assert.NoError(t, err)

golden.Assert(t, res, fmt.Sprintf("%s.golden", t.Name()))
})
}

func TestQueryWithExactName_Http(t *testing.T) {
tc := testutils.Setup(t)
testutils.LoadFixtures(t, tc.FixturePath())
Expand Down
11 changes: 11 additions & 0 deletions api/pkg/service/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ func TestQuery_ByTags(t *testing.T) {
assert.Equal(t, 1, len(all.Data))
}

func TestQuery_ByPlatformss(t *testing.T) {
tc := testutils.Setup(t)
testutils.LoadFixtures(t, tc.FixturePath())

resourceSvc := New(tc)
payload := &resource.QueryPayload{Name: "", Kinds: []string{}, Platforms: []string{"linux/amd64"}, Limit: 100}
all, err := resourceSvc.Query(context.Background(), payload)
assert.NoError(t, err)
assert.Equal(t, 3, len(all.Data))
}

func TestQuery_ByNameAndKind(t *testing.T) {
tc := testutils.Setup(t)
testutils.LoadFixtures(t, tc.FixturePath())
Expand Down
Loading

0 comments on commit 3c53fab

Please sign in to comment.