nicolaferraro closed pull request #91: chore(cobra): add autocompletion for
configmap and secrets
URL: https://github.com/apache/camel-k/pull/91
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
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_zsh.go b/pkg/client/cmd/completion_zsh.go
new file mode 100644
index 0000000..5217b8e
--- /dev/null
+++ b/pkg/client/cmd/completion_zsh.go
@@ -0,0 +1,58 @@
+/*
+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 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
+`
+
+// ******************************
+//
+// 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)
+ },
+ }
+}
+
+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
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services