Skip to content

Commit

Permalink
Adds interactive mode for info command
Browse files Browse the repository at this point in the history
  - This patch interactively ask the users to select

	- Catalog first
   	- Resources in the catalog selected
	- All Versions of the resource selected

  - If there is only one catalog or version then
    it will be auto-selected

  - User can also get the information of the resource
    if the user know the catalog name, resource name
    and version of the resource by

    `tkn hub info task <task-name> --version <version-number> --from catalog <catalog-name>`

Signed-off-by: Puneet Punamiya <ppunamiy@redhat.com>
  • Loading branch information
PuneetPunamiya committed Jun 7, 2021
1 parent c032b76 commit 0d5d7c5
Show file tree
Hide file tree
Showing 18 changed files with 823 additions and 245 deletions.
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.

123 changes: 112 additions & 11 deletions api/pkg/cli/cmd/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 +39,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 +81,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 +107,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.")
cmd.PersistentFlags().StringVar(&opts.version, "version", "", "Version of Resource")

return cmd
Expand All @@ -123,7 +126,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 +140,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
}
}

name := opts.name()

// Check if resource name is passed else
// ask the user to select the resource
if name == "" {
name, err = opts.AskResourceName()
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 Down Expand Up @@ -172,11 +205,79 @@ func (opts *options) run() error {
return printer.New(out).Tabbed(tmpl, tmplData)
}

func (opts *options) AskCatalogName() (string, error) {
// Get all Catalogs
catalog, err := opts.hubClient.GetCatalogsList()
if err != nil {
return "", err
}
if len(catalog) == 1 {
return catalog[0], nil
} else {
// Ask the catalog
err = opts.selectOption.Ask("catalog", catalog)
if err != nil {
return "", err
}
return opts.selectOption.Catalog, nil
}
}

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
}

// 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
}
if len(ver) == 1 {
return ver[0], nil
} else {
latestVersion := ver[0]
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
}
}

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

0 comments on commit 0d5d7c5

Please sign in to comment.