From 2f99b3130c7ef9094a095e668d054a73037bd233 Mon Sep 17 00:00:00 2001 From: joshvanl Date: Thu, 14 Oct 2021 14:43:05 +0100 Subject: [PATCH] Adds build/commands package to expose which commands should be registered. Gates the completion command which is disabled by defualt. Can be configured at build time. Signed-off-by: joshvanl --- cmd/ctl/pkg/build/commands/BUILD.bazel | 37 ++++++++++++++ cmd/ctl/pkg/build/commands/commands.go | 69 ++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 cmd/ctl/pkg/build/commands/BUILD.bazel create mode 100644 cmd/ctl/pkg/build/commands/commands.go diff --git a/cmd/ctl/pkg/build/commands/BUILD.bazel b/cmd/ctl/pkg/build/commands/BUILD.bazel new file mode 100644 index 000000000..b22d8ea9a --- /dev/null +++ b/cmd/ctl/pkg/build/commands/BUILD.bazel @@ -0,0 +1,37 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["commands.go"], + importpath = "github.com/jetstack/cert-manager/cmd/ctl/pkg/build/commands", + visibility = ["//visibility:public"], + 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", + "//cmd/ctl/pkg/experimental:go_default_library", + "//cmd/ctl/pkg/inspect:go_default_library", + "//cmd/ctl/pkg/renew:go_default_library", + "//cmd/ctl/pkg/status:go_default_library", + "//cmd/ctl/pkg/version:go_default_library", + "@com_github_spf13_cobra//:go_default_library", + "@io_k8s_cli_runtime//pkg/genericclioptions: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/build/commands/commands.go b/cmd/ctl/pkg/build/commands/commands.go new file mode 100644 index 000000000..3319c6046 --- /dev/null +++ b/cmd/ctl/pkg/build/commands/commands.go @@ -0,0 +1,69 @@ +/* +Copyright 2020 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 commands + +import ( + "context" + "strings" + + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" + + "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" + "github.com/jetstack/cert-manager/cmd/ctl/pkg/experimental" + "github.com/jetstack/cert-manager/cmd/ctl/pkg/inspect" + "github.com/jetstack/cert-manager/cmd/ctl/pkg/renew" + "github.com/jetstack/cert-manager/cmd/ctl/pkg/status" + "github.com/jetstack/cert-manager/cmd/ctl/pkg/version" +) + +// registerCompletion gates whether the completion command is registered. +// Specifically useful when building the CLI as a kubectl plugin which does not +// support completion. +var registerCompletion = "false" + +type RegisterCommandFunc func(context.Context, genericclioptions.IOStreams) *cobra.Command + +// Commands returns the cobra Commands that should be registered for the CLI +// build. +func Commands() []RegisterCommandFunc { + cmds := []RegisterCommandFunc{ + version.NewCmdVersion, + convert.NewCmdConvert, + create.NewCmdCreate, + renew.NewCmdRenew, + status.NewCmdStatus, + inspect.NewCmdInspect, + approve.NewCmdApprove, + deny.NewCmdDeny, + check.NewCmdCheck, + + // Experimental features + experimental.NewCmdExperimental, + } + + if strings.ToLower(registerCompletion) == "true" { + cmds = append(cmds, completion.NewCmdCompletion) + } + + return cmds +}