Merge pull request #4408 from JoshVanL/ctl-generate-completion

Adds generate completion scripts for bash, zsh, fish, and powershell
This commit is contained in:
jetstack-bot 2021-10-11 16:02:14 +01:00 committed by GitHub
commit 89bc977eb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 244 additions and 0 deletions

View File

@ -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",

View File

@ -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",

View File

@ -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))

View File

@ -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"],
)

View File

@ -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))
},
}
}

View File

@ -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
}

View File

@ -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))
},
}
}

View File

@ -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))
},
}
}

View File

@ -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))
},
}
}