This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/master by this push: new cced8e2 chore(cobra): add autocompletion for configmap and secrets cced8e2 is described below commit cced8e2d94edb21a6c833149cdb00dab2bea0383 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Tue Sep 18 13:06:03 2018 +0200 chore(cobra): add autocompletion for configmap and secrets --- pkg/client/cmd/completion.go | 48 ++------- pkg/client/cmd/completion_bash.go | 107 +++++++++++++++++++++ .../cmd/{completion.go => completion_zsh.go} | 43 +++------ pkg/client/cmd/context_create.go | 3 + pkg/client/cmd/root.go | 17 +++- pkg/client/cmd/run.go | 3 + 6 files changed, 149 insertions(+), 72 deletions(-) diff --git a/pkg/client/cmd/completion.go b/pkg/client/cmd/completion.go index e172df0..e26444d 100644 --- a/pkg/client/cmd/completion.go +++ b/pkg/client/cmd/completion.go @@ -18,54 +18,22 @@ limitations under the License. package cmd import ( - "os" - "github.com/spf13/cobra" ) -const bashCompletionCmdLongDescription = ` -To load completion run - -. <(kamel completion) - -To configure your bash shell to load completions for each session add to your bashrc - -# ~/.bashrc or ~/.profile -. <(kamel completion) -` - -const zshCompletionCmdLongDescription = ` -To configure your zsh shell to load completions for each session add to your zshrc - -if [ $commands[kamel] ]; then - source <(kamel completion zsh) -fi -` - -// NewCmdCompletion -- -func NewCmdCompletion(root *cobra.Command) *cobra.Command { +func newCmdCompletion(root *cobra.Command) *cobra.Command { completion := cobra.Command{ Use: "completion", Short: "Generates completion scripts", } - completion.AddCommand(&cobra.Command{ - Use: "bash", - Short: "Generates bash completion scripts", - Long: bashCompletionCmdLongDescription, - Run: func(cmd *cobra.Command, args []string) { - root.GenBashCompletion(os.Stdout) - }, - }) - - completion.AddCommand(&cobra.Command{ - Use: "zsh", - Short: "Generates zsh completion scripts", - Long: zshCompletionCmdLongDescription, - Run: func(cmd *cobra.Command, args []string) { - root.GenZshCompletion(os.Stdout) - }, - }) + completion.AddCommand(newCmdCompletionBash(root)) + completion.AddCommand(newCmdCompletionZsh(root)) return &completion } + +func configureKnownCompletions(command *cobra.Command) { + configureKnownBashCompletions(command) + configureKnownZshCompletions(command) +} diff --git a/pkg/client/cmd/completion_bash.go b/pkg/client/cmd/completion_bash.go new file mode 100644 index 0000000..34aad56 --- /dev/null +++ b/pkg/client/cmd/completion_bash.go @@ -0,0 +1,107 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +// ****************************** +// +// +// +// ****************************** + +const bashCompletionCmdLongDescription = ` +To load completion run + +. <(kamel completion bash) + +To configure your bash shell to load completions for each session add to your bashrc + +# ~/.bashrc or ~/.profile +. <(kamel completion bash) +` + +const bashCompletionFunction = ` +__kamel_dependency_type() { + COMPREPLY=( $( compgen -W "camel: mvn: file:" -- "$cur") ) + compopt -o nospace +} + +__kamel_kubectl_get_configmap() { + local template + local kubectl_out + + template="{{ range .items }}{{ .metadata.name }} {{ end }}" + + if kubectl_out=$(kubectl get -o template --template="${template}" configmap 2>/dev/null); then + COMPREPLY=( $( compgen -W "${kubectl_out}" -- "$cur" ) ) + fi +} + +__kamel_kubectl_get_secret() { + local template + local kubectl_out + + template="{{ range .items }}{{ .metadata.name }} {{ end }}" + + if kubectl_out=$(kubectl get -o template --template="${template}" secret 2>/dev/null); then + COMPREPLY=( $( compgen -W "${kubectl_out}" -- "$cur" ) ) + fi +} +` + +// ****************************** +// +// COMMAND +// +// ****************************** + +func newCmdCompletionBash(root *cobra.Command) *cobra.Command { + return &cobra.Command{ + Use: "bash", + Short: "Generates bash completion scripts", + Long: bashCompletionCmdLongDescription, + Run: func(cmd *cobra.Command, args []string) { + root.GenBashCompletion(os.Stdout) + }, + } +} + +func configureKnownBashCompletions(command *cobra.Command) { + // completion support + dependencyFlag := command.Flag("dependency") + if dependencyFlag != nil { + dependencyFlag.Annotations = map[string][]string{ + cobra.BashCompCustom: {"__kamel_dependency_type"}, + } + } + + configMapFlag := command.Flag("configmap") + configMapFlag.Annotations = map[string][]string{ + cobra.BashCompCustom: {"__kamel_kubectl_get_configmap"}, + } + + secretFlag := command.Flag("secret") + secretFlag.Annotations = map[string][]string{ + cobra.BashCompCustom: {"__kamel_kubectl_get_secret"}, + } +} diff --git a/pkg/client/cmd/completion.go b/pkg/client/cmd/completion_zsh.go similarity index 63% copy from pkg/client/cmd/completion.go copy to pkg/client/cmd/completion_zsh.go index e172df0..5217b8e 100644 --- a/pkg/client/cmd/completion.go +++ b/pkg/client/cmd/completion_zsh.go @@ -23,16 +23,11 @@ import ( "github.com/spf13/cobra" ) -const bashCompletionCmdLongDescription = ` -To load completion run - -. <(kamel completion) - -To configure your bash shell to load completions for each session add to your bashrc - -# ~/.bashrc or ~/.profile -. <(kamel completion) -` +// ****************************** +// +// +// +// ****************************** const zshCompletionCmdLongDescription = ` To configure your zsh shell to load completions for each session add to your zshrc @@ -42,30 +37,22 @@ if [ $commands[kamel] ]; then fi ` -// NewCmdCompletion -- -func NewCmdCompletion(root *cobra.Command) *cobra.Command { - completion := cobra.Command{ - Use: "completion", - Short: "Generates completion scripts", - } - - completion.AddCommand(&cobra.Command{ - Use: "bash", - Short: "Generates bash completion scripts", - Long: bashCompletionCmdLongDescription, - Run: func(cmd *cobra.Command, args []string) { - root.GenBashCompletion(os.Stdout) - }, - }) +// ****************************** +// +// COMMAND +// +// ****************************** - completion.AddCommand(&cobra.Command{ +func newCmdCompletionZsh(root *cobra.Command) *cobra.Command { + return &cobra.Command{ Use: "zsh", Short: "Generates zsh completion scripts", Long: zshCompletionCmdLongDescription, Run: func(cmd *cobra.Command, args []string) { root.GenZshCompletion(os.Stdout) }, - }) + } +} - return &completion +func configureKnownZshCompletions(command *cobra.Command) { } diff --git a/pkg/client/cmd/context_create.go b/pkg/client/cmd/context_create.go index 224f1e0..2120d30 100644 --- a/pkg/client/cmd/context_create.go +++ b/pkg/client/cmd/context_create.go @@ -50,6 +50,9 @@ func newContextCreateCmd(rootCmdOptions *RootCmdOptions) *cobra.Command { cmd.Flags().StringSliceVar(&impl.configmaps, "configmap", nil, "Add a ConfigMap") cmd.Flags().StringSliceVar(&impl.secrets, "secret", nil, "Add a Secret") + // completion support + configureKnownCompletions(&cmd) + return &cmd } diff --git a/pkg/client/cmd/root.go b/pkg/client/cmd/root.go index 3168cc2..dfbbfd9 100644 --- a/pkg/client/cmd/root.go +++ b/pkg/client/cmd/root.go @@ -27,20 +27,29 @@ import ( "github.com/spf13/cobra" ) +const kamelCommandLongDescription = ` +Long: "Apache Camel K (a.k.a. Kamel) is a lightweight integration framework +built from Apache Camel that runs natively on Kubernetes and is +specifically designed for serverless and microservice architectures.",, +` + +// RootCmdOptions -- type RootCmdOptions struct { Context context.Context KubeConfig string Namespace string } +// NewKamelCommand -- func NewKamelCommand(ctx context.Context) (*cobra.Command, error) { options := RootCmdOptions{ Context: ctx, } var cmd = cobra.Command{ - Use: "kamel", - Short: "Kamel is a awesome client tool for running Apache Camel integrations natively on Kubernetes", - Long: "Apache Camel K (a.k.a. Kamel) is a lightweight integration framework\nbuilt from Apache Camel that runs natively on Kubernetes and is\nspecifically designed for serverless and microservice architectures.", + Use: "kamel", + Short: "Kamel is a awesome client tool for running Apache Camel integrations natively on Kubernetes", + Long: kamelCommandLongDescription, + BashCompletionFunction: bashCompletionFunction, } cmd.PersistentFlags().StringVar(&options.KubeConfig, "config", "", "Path to the config file to use for CLI requests") @@ -63,7 +72,7 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) { return nil, err } - cmd.AddCommand(NewCmdCompletion(&cmd)) + cmd.AddCommand(newCmdCompletion(&cmd)) cmd.AddCommand(NewCmdVersion()) cmd.AddCommand(NewCmdRun(&options)) cmd.AddCommand(NewCmdGet(&options)) diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go index 150dd85..22c0aa4 100644 --- a/pkg/client/cmd/run.go +++ b/pkg/client/cmd/run.go @@ -61,6 +61,9 @@ func NewCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command { cmd.Flags().StringSliceVar(&options.Secrets, "secret", nil, "Add a Secret") cmd.Flags().BoolVar(&options.Logs, "logs", false, "Print integration logs") + // completion support + configureKnownCompletions(&cmd) + return &cmd }