Skip to content

Commit

Permalink
Add completion on namespace and kubeconfig
Browse files Browse the repository at this point in the history
This would complete properly the namespaces when we have a -n or files whe we
have kubeconfig

(cherry picked from commit 24839e1)
  • Loading branch information
chmouel authored and vdemeester committed Jun 19, 2019
1 parent 6bd83bf commit b12b90c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/cmd/completion/bash_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

package completion

var (
// BashCompletionFlags this maps between a flag (ie: namespace) to a custom
// zsh completion
BashCompletionFlags = map[string]string{
"namespace": "__kubectl_get_object namespace",
"kubeconfig": "_filedir",
}
)

const (
// BashCompletionFunc the custom bash completion function to complete object.
// The bash completion mechanism will launch the __custom_func func if it cannot
Expand All @@ -31,6 +40,17 @@ __tkn_get_object()
fi
}
__kubectl_get_object()
{
local type=$1
local template
template="{{ range .items }}{{ .metadata.name }} {{ end }}"
local kubectl_out
if kubectl_out=$(kubectl get -o template --template="${template}" ${type} 2>/dev/null); then
COMPREPLY=( $( compgen -W "${kubectl_out}" -- "$cur" ) )
fi
}
__custom_func() {
case ${last_command} in
*_describe|*_logs)
Expand Down
16 changes: 16 additions & 0 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package flags
import (
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/cmd/completion"
)

const (
Expand All @@ -33,6 +34,21 @@ func AddTektonOptions(cmd *cobra.Command) {
cmd.PersistentFlags().StringP(
namespace, "n", "",
"namespace to use (default: from $KUBECONFIG)")

// Add custom completion for that command as specified in
// bashCompletionFlags map
for name, completion := range completion.BashCompletionFlags {
pflag := cmd.PersistentFlags().Lookup(name)
if pflag != nil {
if pflag.Annotations == nil {
pflag.Annotations = map[string][]string{}
}
pflag.Annotations[cobra.BashCompCustom] = append(
pflag.Annotations[cobra.BashCompCustom],
completion,
)
}
}
}

// InitParams initialises cli.Params based on flags defined in command
Expand Down

0 comments on commit b12b90c

Please sign in to comment.