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

Adds interactive mode for info command #265

Merged
merged 1 commit into from
Jun 23, 2021
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
11 changes: 7 additions & 4 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ module github.com/tektoncd/hub/api
go 1.14

require (
github.com/AlecAivazis/survey/v2 v2.2.12
github.com/Netflix/go-expect v0.0.0-20200312175327-da48e75238e2
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dimfeld/httptreemux/v5 v5.3.0 // indirect
github.com/fatih/color v1.7.0
github.com/fatih/color v1.9.0
github.com/go-gormigrate/gormigrate/v2 v2.0.0
github.com/go-testfixtures/testfixtures/v3 v3.2.0
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174
github.com/ikawaha/goahttpcheck v1.3.1
github.com/joho/godotenv v1.3.0
github.com/mitchellh/go-homedir v1.1.0
github.com/sergi/go-diff v1.2.0 // indirect
github.com/spf13/cobra v1.1.1
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.6.1
github.com/tektoncd/pipeline v0.20.1-0.20210204110343-8c5a751b53ea
github.com/tektoncd/pipeline v0.24.1
go.uber.org/zap v1.16.0
goa.design/goa/v3 v3.3.1
goa.design/plugins/v3 v3.1.3
Expand All @@ -31,6 +34,6 @@ require (
gotest.tools/v3 v3.0.2
k8s.io/apimachinery v0.19.7
k8s.io/client-go v0.19.7
knative.dev/pkg v0.0.0-20210203171706-6045ed499615
knative.dev/pkg v0.0.0-20210331065221-952fdd90dbb0
maze.io/x/duration v0.0.0-20160924141736-faac084b6075
)
132 changes: 50 additions & 82 deletions api/go.sum

Large diffs are not rendered by default.

151 changes: 127 additions & 24 deletions api/pkg/cli/cmd/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package info

import (
"fmt"
"strings"
"text/template"

Expand All @@ -24,6 +25,7 @@ import (
"github.com/tektoncd/hub/api/pkg/cli/formatter"
"github.com/tektoncd/hub/api/pkg/cli/hub"
"github.com/tektoncd/hub/api/pkg/cli/printer"
so "github.com/tektoncd/hub/api/pkg/cli/select_options"
)

var cmdExamples string = `
Expand All @@ -38,7 +40,7 @@ Display info of a %S of name 'foo' of version '0.3':
tkn hub info %s foo --version 0.3
`

const resTemplate = `{{ icon "name" }}Name: {{ .Resource.Name }}
const resTemplate = `{{ icon "name" }}Name: {{ .Resource.Name }}
{{ $n := .ResVersion.DisplayName }}{{ if ne (default $n "") "" }}
{{ icon "displayName" }}Display Name: {{ $n }}
{{ end }}
Expand Down Expand Up @@ -80,11 +82,13 @@ type templateData struct {
}

type options struct {
cli app.CLI
from string
version string
kind string
args []string
cli app.CLI
from string
version string
kind string
args []string
hubClient hub.Client
selectOption so.Options
}

func Command(cli app.CLI) *cobra.Command {
Expand All @@ -104,7 +108,7 @@ func Command(cli app.CLI) *cobra.Command {
commandForKind("task", opts),
)

cmd.PersistentFlags().StringVar(&opts.from, "from", "tekton", "Name of Catalog to which resource belongs.")
cmd.PersistentFlags().StringVar(&opts.from, "from", "", "Name of Catalog to which resource belongs.")
PuneetPunamiya marked this conversation as resolved.
Show resolved Hide resolved
cmd.PersistentFlags().StringVar(&opts.version, "version", "", "Version of Resource")

return cmd
Expand All @@ -123,7 +127,6 @@ func commandForKind(kind string, opts *options) *cobra.Command {
Annotations: map[string]string{
"commandType": "main",
},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.kind = kind
opts.args = args
Expand All @@ -138,9 +141,40 @@ func (opts *options) run() error {
return err
}

hubClient := opts.cli.Hub()
res := hubClient.GetResource(hub.ResourceOption{
Name: opts.name(),
opts.hubClient = opts.cli.Hub()
var err error

// Check if catalog name is passed else
// ask the user to select the catalog
if opts.from == "" {
opts.from, err = opts.AskCatalogName()
if err != nil {
return err
}
PuneetPunamiya marked this conversation as resolved.
Show resolved Hide resolved
}

name := opts.name()

// Check if resource name is passed else
// ask the user to select the resource
if name == "" {
PuneetPunamiya marked this conversation as resolved.
Show resolved Hide resolved
name, err = opts.AskResourceName()
PuneetPunamiya marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
}

// Check if version of the resource is passed else
// ask the user to select the version of resource
if opts.version == "" {
opts.version, err = opts.AskVersion(name)
if err != nil {
return err
}
}

res := opts.hubClient.GetResource(hub.ResourceOption{
Name: name,
Catalog: opts.from,
Kind: opts.kind,
Version: opts.version,
Expand All @@ -153,30 +187,99 @@ func (opts *options) run() error {

out := opts.cli.Stream().Out

if opts.version != "" {
resVersion := resource.(hub.ResourceWithVersionData)
tmplData := templateData{
ResVersion: &resVersion,
Resource: resVersion.Resource,
Latest: false,
resVersion := resource.(hub.ResourceWithVersionData)
tmplData := templateData{
ResVersion: &resVersion,
Resource: resVersion.Resource,
Latest: false,
}
return printer.New(out).Tabbed(tmpl, tmplData)
}

func (opts *options) AskCatalogName() (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line no 200-207 won't get be executed now right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes it won't be executed but in that case we don't need to even check if the version is empty, should we remove the condition to check if the version is empty ??

// Get all Catalogs
catalog, err := opts.hubClient.GetCatalogsList()
if err != nil {
return "", err
}
switch len(catalog) {
case 0:
return "", fmt.Errorf("No catalogs found")
case 1:
return catalog[0], nil
default:
// Ask the catalog
err = opts.selectOption.Ask("catalog", catalog)
if err != nil {
return "", err
}
return printer.New(out).Tabbed(tmpl, tmplData)
return opts.selectOption.Catalog, nil
}

hubRes := resource.(hub.ResourceData)
tmplData := templateData{
Resource: &hubRes,
ResVersion: hubRes.LatestVersion,
Latest: true,
}

func (opts *options) AskResourceName() (string, error) {
// Get all resources from the Catalog selected
resources, err := opts.hubClient.GetResourcesList(hub.SearchOption{
Kinds: []string{opts.kind},
Catalog: opts.from,
})
if err != nil {
return "", err
}
switch len(resources) {
case 0:
return "", fmt.Errorf("No resources found")
case 1:
return resources[0], nil
default:
// Ask the resource name
err = opts.selectOption.Ask(opts.kind, resources)
if err != nil {
return "", err
}
return opts.selectOption.Name, nil
}
}

func (opts *options) AskVersion(name string) (string, error) {
// Get all the versions of the resource selected
ver, err := opts.hubClient.GetResourceVersionslist(hub.ResourceOption{
Name: name,
Kind: opts.kind,
Catalog: opts.from,
})
if err != nil {
return "", err
}
switch len(ver) {
case 1:
return ver[0], nil
default:
latestVersion := ver[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a field in API response for latest version, better use that in case of versions array is not sorted

ver[0] = ver[0] + " (latest)"

// Ask the version
err = opts.selectOption.Ask("version", ver)
if err != nil {
return "", err
}

if strings.Contains(opts.selectOption.Version, "(latest)") {
opts.selectOption.Version = latestVersion
}
return opts.selectOption.Version, nil
}
return printer.New(out).Tabbed(tmpl, tmplData)
}

func (opts *options) validate() error {
return flag.ValidateVersion(opts.version)
}

func (opts *options) name() string {
if len(opts.args) == 0 {
return ""
}
return strings.TrimSpace(opts.args[0])
}

Expand Down
Loading