diff --git a/cmd/ctl/BUILD.bazel b/cmd/ctl/BUILD.bazel index 395e75fd3..8a83bd487 100644 --- a/cmd/ctl/BUILD.bazel +++ b/cmd/ctl/BUILD.bazel @@ -34,6 +34,7 @@ filegroup( "//cmd/ctl/cmd:all-srcs", "//cmd/ctl/pkg/approve:all-srcs", "//cmd/ctl/pkg/check:all-srcs", + "//cmd/ctl/pkg/completion:all-srcs", "//cmd/ctl/pkg/convert:all-srcs", "//cmd/ctl/pkg/create:all-srcs", "//cmd/ctl/pkg/deny:all-srcs", diff --git a/cmd/ctl/cmd/BUILD.bazel b/cmd/ctl/cmd/BUILD.bazel index c376d025c..ea64973af 100644 --- a/cmd/ctl/cmd/BUILD.bazel +++ b/cmd/ctl/cmd/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//cmd/ctl/pkg/approve:go_default_library", "//cmd/ctl/pkg/check:go_default_library", + "//cmd/ctl/pkg/completion:go_default_library", "//cmd/ctl/pkg/convert:go_default_library", "//cmd/ctl/pkg/create:go_default_library", "//cmd/ctl/pkg/deny:go_default_library", diff --git a/cmd/ctl/cmd/cmd.go b/cmd/ctl/cmd/cmd.go index 7f8fd0785..8cedd66a0 100644 --- a/cmd/ctl/cmd/cmd.go +++ b/cmd/ctl/cmd/cmd.go @@ -29,6 +29,7 @@ import ( "github.com/jetstack/cert-manager/cmd/ctl/pkg/approve" "github.com/jetstack/cert-manager/cmd/ctl/pkg/check" + "github.com/jetstack/cert-manager/cmd/ctl/pkg/completion" "github.com/jetstack/cert-manager/cmd/ctl/pkg/convert" "github.com/jetstack/cert-manager/cmd/ctl/pkg/create" "github.com/jetstack/cert-manager/cmd/ctl/pkg/deny" @@ -67,6 +68,7 @@ kubectl cert-manager is a CLI tool manage and configure cert-manager resources f cmds.AddCommand(approve.NewCmdApprove(ctx, ioStreams)) cmds.AddCommand(deny.NewCmdDeny(ctx, ioStreams)) cmds.AddCommand(check.NewCmdCheck(ctx, ioStreams)) + cmds.AddCommand(completion.NewCmdCompletion(ctx, ioStreams)) // Experimental features cmds.AddCommand(experimental.NewCmdExperimental(ctx, ioStreams)) diff --git a/cmd/ctl/pkg/completion/BUILD.bazel b/cmd/ctl/pkg/completion/BUILD.bazel new file mode 100644 index 000000000..fce468ef3 --- /dev/null +++ b/cmd/ctl/pkg/completion/BUILD.bazel @@ -0,0 +1,33 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = [ + "bash.go", + "completion.go", + "fish.go", + "powershell.go", + "zsh.go", + ], + importpath = "github.com/jetstack/cert-manager/cmd/ctl/pkg/completion", + visibility = ["//visibility:public"], + deps = [ + "@com_github_spf13_cobra//:go_default_library", + "@io_k8s_cli_runtime//pkg/genericclioptions:go_default_library", + "@io_k8s_kubectl//pkg/cmd/util:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/cmd/ctl/pkg/completion/bash.go b/cmd/ctl/pkg/completion/bash.go new file mode 100644 index 000000000..e67dcddfd --- /dev/null +++ b/cmd/ctl/pkg/completion/bash.go @@ -0,0 +1,44 @@ +/* +Copyright 2021 The cert-manager Authors. + +Licensed 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 completion + +import ( + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/kubectl/pkg/cmd/util" +) + +func newCmdCompletionBash(ioStreams genericclioptions.IOStreams) *cobra.Command { + return &cobra.Command{ + Use: "bash", + Short: "Generate cert-manager CLI scripts for a Bash shell", + Long: `To load completions: +Bash: + $ source <(kubectl cert-manager completion bash) + # To load completions for each session, execute once: + # Linux: + $ cert-manager completion bash > /etc/bash_completion.d/cert-manager + + # macOS: + $ cert-manager completion bash > /usr/local/etc/bash_completion.d/cert-manager +`, + DisableFlagsInUseLine: true, + Run: func(cmd *cobra.Command, args []string) { + util.CheckErr(cmd.Root().GenBashCompletion(ioStreams.Out)) + }, + } +} diff --git a/cmd/ctl/pkg/completion/completion.go b/cmd/ctl/pkg/completion/completion.go new file mode 100644 index 000000000..6ba46a651 --- /dev/null +++ b/cmd/ctl/pkg/completion/completion.go @@ -0,0 +1,39 @@ +/* +Copyright 2021 The cert-manager Authors. + +Licensed 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 completion + +import ( + "context" + + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" +) + +func NewCmdCompletion(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { + cmds := &cobra.Command{ + Use: "completion", + Short: "Generate completion scripts for the cert-manager CLI", + Long: "Generate completion for the cert-manager CLI so arguments and flags can be suggested and auto-completed", + } + + cmds.AddCommand(newCmdCompletionBash(ioStreams)) + cmds.AddCommand(newCmdCompletionZSH(ioStreams)) + cmds.AddCommand(newCmdCompletionFish(ioStreams)) + cmds.AddCommand(newCmdCompletionPowerShell(ioStreams)) + + return cmds +} diff --git a/cmd/ctl/pkg/completion/fish.go b/cmd/ctl/pkg/completion/fish.go new file mode 100644 index 000000000..c42160074 --- /dev/null +++ b/cmd/ctl/pkg/completion/fish.go @@ -0,0 +1,40 @@ +/* +Copyright 2021 The cert-manager Authors. + +Licensed 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 completion + +import ( + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/kubectl/pkg/cmd/util" +) + +func newCmdCompletionFish(ioStreams genericclioptions.IOStreams) *cobra.Command { + return &cobra.Command{ + Use: "fish", + Short: "Generate cert-manager CLI scripts for a Fish shell", + Long: `To load completions: + $ cert-manager completion fish | source + + # To load completions for each session, execute once: + $ cert-manager completion fish > ~/.config/fish/completions/cert-manager.fish +`, + DisableFlagsInUseLine: true, + Run: func(cmd *cobra.Command, args []string) { + util.CheckErr(cmd.Root().GenFishCompletion(ioStreams.Out, true)) + }, + } +} diff --git a/cmd/ctl/pkg/completion/powershell.go b/cmd/ctl/pkg/completion/powershell.go new file mode 100644 index 000000000..4f978d67f --- /dev/null +++ b/cmd/ctl/pkg/completion/powershell.go @@ -0,0 +1,41 @@ +/* +Copyright 2021 The cert-manager Authors. + +Licensed 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 completion + +import ( + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/kubectl/pkg/cmd/util" +) + +func newCmdCompletionPowerShell(ioStreams genericclioptions.IOStreams) *cobra.Command { + return &cobra.Command{ + Use: "powershell", + Short: "Generate cert-manager CLI scripts for a PowerShell shell", + Long: `To load completions: + PS> cert-manager completion powershell | Out-String | Invoke-Expression + + # To load completions for every new session, run: + PS> cert-manager completion powershell > cert-manager.ps1 + # and source this file from your PowerShell profile. +`, + DisableFlagsInUseLine: true, + Run: func(cmd *cobra.Command, args []string) { + util.CheckErr(cmd.Root().GenPowerShellCompletion(ioStreams.Out)) + }, + } +} diff --git a/cmd/ctl/pkg/completion/zsh.go b/cmd/ctl/pkg/completion/zsh.go new file mode 100644 index 000000000..3a34a1a8e --- /dev/null +++ b/cmd/ctl/pkg/completion/zsh.go @@ -0,0 +1,43 @@ +/* +Copyright 2021 The cert-manager Authors. + +Licensed 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 completion + +import ( + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/kubectl/pkg/cmd/util" +) + +func newCmdCompletionZSH(ioStreams genericclioptions.IOStreams) *cobra.Command { + return &cobra.Command{ + Use: "zsh", + Short: "Generation cert-manager CLI scripts for a ZSH shell", + Long: `To load completions: + # 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: + $ cert-manager completion zsh > "${fpath[1]}/_cert-manager" + # You will need to start a new shell for this setup to take effect. +`, + DisableFlagsInUseLine: true, + Run: func(cmd *cobra.Command, args []string) { + util.CheckErr(cmd.Root().GenZshCompletion(ioStreams.Out)) + }, + } +}