Skip to content

Commit

Permalink
Add new completions based on new upstream completion system
Browse files Browse the repository at this point in the history
Adapted the completion we had previously to the new completion system from
cobra 1.0.

I started to use the client-go directly but since there is no caching, the
<Tab><Tab> is unusably slow. So using the kuebctl binrary directly instead (I
can't use tkn since caching is not there as well) to get the completions.

This is how we were doing it previously (using kubectl or tkn) and completions
should be much faster.

We are winning support for other shells (powershell, fish) along the way an d
not having an outdated fork in tree.

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel authored and tekton-robot committed Dec 23, 2020
1 parent 25af5ad commit b41bcfb
Show file tree
Hide file tree
Showing 40 changed files with 332 additions and 278 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ The following commands help you understand and effectively use the Tekton CLI:
* `tkn help:` Displays a list of the commands with helpful information.
* [`tkn clustertask:`](docs/cmd/tkn_clustertask.md) Parent command of the ClusterTask command group.
* [`tkn clustertriggerbinding:`](docs/cmd/tkn_clustertriggerbinding.md) Parent command of the ClusterTriggerBinding command group.
* [`tkn completion:`](docs/cmd/tkn_completion.md) Outputs a BASH or ZSH completion script for `tkn` to allow command completion with Tab.
* [`tkn completion:`](docs/cmd/tkn_completion.md) Outputs a BASH, ZSH, Fish or PowerShell completion script for `tkn` to allow command completion with Tab.
* [`tkn condition:`](docs/cmd/tkn_condition.md) Parent command of the Condition command group.
* [`tkn eventlistener:`](docs/cmd/tkn_eventlistener.md) Parent command of the Eventlistener command group.
* [`tkn hub:`](docs/cmd/tkn_hub.md) Search and install Tekton Resources from [Hub](https://hub-preview.tekton.dev)
Expand Down
34 changes: 29 additions & 5 deletions docs/cmd/tkn_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ tkn completion [SHELL]

### Synopsis


This command prints shell completion code which must be evaluated to provide
interactive completion

Expand All @@ -21,12 +20,37 @@ Supported Shells:

### Examples

To load completions:

Bash:

$ source <(tkn completion bash)

# To load completions for each session, execute once:
Linux:
$ tkn completion bash > /etc/bash_completion.d/tkn

MacOS:
$ tkn completion bash > /usr/local/etc/bash_completion.d/tkn

Zsh:

# If shell completion is not already enabled in your environment you will need
# to enable it. You can execute the following once:

$ echo "autoload -U compinit; compinit" >> ~/.zshrc

# To load completions for each session, execute once:
$ tkn completion zsh > "${fpath[1]}/_tkn"

# You will need to start a new shell for this setup to take effect.

Fish:

# generate completion code for bash
source <(tkn completion bash)
$ tkn completion fish | source

# generate completion code for zsh
source <(tkn completion zsh)
# To load completions for each session, execute once:
$ tkn completion fish > ~/.config/fish/completions/tkn.fish


### Options
Expand Down
47 changes: 43 additions & 4 deletions docs/man/man1/tkn-completion.1
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,51 @@ Supported Shells:

.SH EXAMPLE
.PP
# generate completion code for bash
source <(tkn completion bash)
To load completions:

.PP
# generate completion code for zsh
source <(tkn completion zsh)
Bash:

.PP
$ source <(tkn completion bash)


.SH To load completions for each session, execute once:
.PP
Linux:
$ tkn completion bash > /etc/bash\_completion.d/tkn

.PP
MacOS:
$ tkn completion bash > /usr/local/etc/bash\_completion.d/tkn

.PP
Zsh:


.SH If shell completion is not already enabled in your environment you will need

.SH to enable it. You can execute the following once:
.PP
$ echo "autoload \-U compinit; compinit" >> \~/.zshrc


.SH To load completions for each session, execute once:
.PP
$ tkn completion zsh > "${fpath[1]}/\_tkn"


.SH You will need to start a new shell for this setup to take effect.
.PP
Fish:

.PP
$ tkn completion fish | source


.SH To load completions for each session, execute once:
.PP
$ tkn completion fish > \~/.config/fish/completions/tkn.fish


.SH SEE ALSO
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/Netflix/go-expect v0.0.0-20200312175327-da48e75238e2
github.com/blang/semver v3.5.1+incompatible
github.com/cpuguy83/go-md2man v1.0.10
github.com/davecgh/go-spew v1.1.1
github.com/fatih/color v1.9.0
github.com/ghodss/yaml v1.0.0
github.com/google/go-cmp v0.5.2
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/clustertask/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/clustertask"
"github.com/tektoncd/cli/pkg/deleter"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/options"
"github.com/tektoncd/cli/pkg/task"
trlist "github.com/tektoncd/cli/pkg/taskrun/list"
Expand Down Expand Up @@ -52,6 +53,7 @@ or
Annotations: map[string]string{
"commandType": "main",
},
ValidArgsFunction: formatted.ParentCompletion,
RunE: func(cmd *cobra.Command, args []string) error {
s := &cli.Stream{
In: cmd.InOrStdin(),
Expand All @@ -70,7 +72,6 @@ or
c.Flags().BoolVarP(&opts.ForceDelete, "force", "f", false, "Whether to force deletion (default: false)")
c.Flags().BoolVarP(&opts.DeleteAll, "all", "", false, "Delete all ClusterTasks (default: false)")
c.Flags().BoolVarP(&opts.DeleteRelated, "trs", "", false, "Whether to delete ClusterTask(s) and related resources (TaskRuns) (default: false)")
_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_clustertasks")
return c
}
func deleteClusterTasks(opts *options.DeleteOptions, s *cli.Stream, p cli.Params, ctNames []string) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/clustertask/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const describeTemplate = `{{decorate "bold" "Name"}}: {{ .ClusterTask.Name }}
No steps
{{- else }}
{{- range $step := .ClusterTask.Spec.Steps }}
{{ autoStepName $step.Name | decorate "bullet" }}
{{ autoStepName $step.Name | decorate "bullet" }}
{{- end }}
{{- end }}
Expand Down Expand Up @@ -152,7 +152,8 @@ or
Annotations: map[string]string{
"commandType": "main",
},
SilenceUsage: true,
SilenceUsage: true,
ValidArgsFunction: formatted.ParentCompletion,
RunE: func(cmd *cobra.Command, args []string) error {
s := &cli.Stream{
Out: cmd.OutOrStdout(),
Expand Down Expand Up @@ -190,7 +191,6 @@ or
},
}

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_clustertasks")
f.AddFlags(c)
return c
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/cmd/clustertask/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/tektoncd/cli/pkg/cmd/taskrun"
"github.com/tektoncd/cli/pkg/file"
"github.com/tektoncd/cli/pkg/flags"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/labels"
"github.com/tektoncd/cli/pkg/options"
"github.com/tektoncd/cli/pkg/params"
Expand Down Expand Up @@ -131,8 +132,9 @@ like cat,foo,bar
Annotations: map[string]string{
"commandType": "main",
},
Example: eg,
SilenceUsage: true,
Example: eg,
SilenceUsage: true,
ValidArgsFunction: formatted.ParentCompletion,
Args: func(cmd *cobra.Command, args []string) error {
if opt.UseParamDefaults && (opt.Last || opt.UseTaskRun != "") {
return errors.New("cannot use --last or --use-taskrun options with --use-param-defaults option")
Expand Down Expand Up @@ -165,10 +167,8 @@ like cat,foo,bar
c.Flags().StringSliceVarP(&opt.OutputResources, "outputresource", "o", []string{}, "pass the output resource name and ref as name=ref")
c.Flags().StringArrayVarP(&opt.Params, "param", "p", []string{}, "pass the param as key=value for string type, or key=value1,value2,... for array type")
c.Flags().StringVarP(&opt.ServiceAccountName, "serviceaccount", "s", "", "pass the serviceaccount name")
flags.AddShellCompletion(c.Flags().Lookup("serviceaccount"), "__kubectl_get_serviceaccount")
c.Flags().BoolVarP(&opt.Last, "last", "L", false, "re-run the ClusterTask using last TaskRun values")
c.Flags().StringVarP(&opt.UseTaskRun, "use-taskrun", "", "", "specify a TaskRun name to use its values to re-run the TaskRun")
flags.AddShellCompletion(c.Flags().Lookup("use-taskrun"), "__tkn_get_taskrun")
c.Flags().StringSliceVarP(&opt.Labels, "labels", "l", []string{}, "pass labels as label=value.")
c.Flags().StringArrayVarP(&opt.Workspaces, "workspace", "w", []string{}, "pass one or more workspaces to map to the corresponding physical volumes as name=name,claimName=pvcName or name=name,emptyDir=")
c.Flags().BoolVarP(&opt.ShowLog, "showlog", "", false, "show logs right after starting the ClusterTask")
Expand All @@ -179,8 +179,6 @@ like cat,foo,bar
c.Flags().StringVar(&opt.PodTemplate, "pod-template", "", "local or remote file containing a PodTemplate definition")
c.Flags().BoolVar(&opt.UseParamDefaults, "use-param-defaults", false, "use default parameter values without prompting for input")

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_clustertasks")

return c
}

Expand Down
15 changes: 8 additions & 7 deletions pkg/cmd/clustertriggerbinding/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/deleter"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/options"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
Expand All @@ -39,12 +40,13 @@ or
`

c := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Short: "Delete ClusterTriggerBindings",
Example: eg,
Args: cobra.MinimumNArgs(0),
SilenceUsage: true,
Use: "delete",
Aliases: []string{"rm"},
Short: "Delete ClusterTriggerBindings",
Example: eg,
Args: cobra.MinimumNArgs(0),
SilenceUsage: true,
ValidArgsFunction: formatted.ParentCompletion,
Annotations: map[string]string{
"commandType": "main",
},
Expand All @@ -66,7 +68,6 @@ or
c.Flags().BoolVarP(&opts.ForceDelete, "force", "f", false, "Whether to force deletion (default: false)")
c.Flags().BoolVarP(&opts.DeleteAll, "all", "", false, "Delete all ClusterTriggerBindings (default: false)")

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_clustertriggerbinding")
return c
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/clustertriggerbinding/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ or
`

c := &cobra.Command{
Use: "describe",
Aliases: []string{"desc"},
Short: "Describes a ClusterTriggerBinding",
Example: eg,
Use: "describe",
Aliases: []string{"desc"},
Short: "Describes a ClusterTriggerBinding",
Example: eg,
ValidArgsFunction: formatted.ParentCompletion,
Annotations: map[string]string{
"commandType": "main",
},
Expand All @@ -85,7 +86,6 @@ or
},
}

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_clustertriggerbindings")
f.AddFlags(c)
return c
}
Expand Down
Loading

0 comments on commit b41bcfb

Please sign in to comment.