From a3e3eafe0507b938d1aa76a28546e2639da8dcc0 Mon Sep 17 00:00:00 2001 From: wrype <719147488@qq.com> Date: Tue, 8 Aug 2023 14:52:49 +0800 Subject: [PATCH] feat(plugin): add flags shorthand like kubectl --- cmd/plugin/commands/info/info.go | 11 +++-------- cmd/plugin/commands/ingresses/ingresses.go | 9 ++------- cmd/plugin/commands/lint/main.go | 2 +- cmd/plugin/util/util.go | 7 +++++++ 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/cmd/plugin/commands/info/info.go b/cmd/plugin/commands/info/info.go index 246046c3a..f345a1c09 100644 --- a/cmd/plugin/commands/info/info.go +++ b/cmd/plugin/commands/info/info.go @@ -29,21 +29,16 @@ import ( // CreateCommand creates and returns this cobra subcommand func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command { + var service *string cmd := &cobra.Command{ Use: "info", Short: "Show information about the ingress-nginx service", RunE: func(cmd *cobra.Command, args []string) error { - service, err := cmd.Flags().GetString("service") - if err != nil { - return err - } - - util.PrintError(info(flags, service)) + util.PrintError(info(flags, *service)) return nil }, } - - cmd.Flags().String("service", util.DefaultIngressServiceName, "The name of the ingress-nginx service") + service = util.AddServiceFlag(cmd) return cmd } diff --git a/cmd/plugin/commands/ingresses/ingresses.go b/cmd/plugin/commands/ingresses/ingresses.go index dff967103..2e23f92c4 100644 --- a/cmd/plugin/commands/ingresses/ingresses.go +++ b/cmd/plugin/commands/ingresses/ingresses.go @@ -32,6 +32,7 @@ import ( // CreateCommand creates and returns this cobra subcommand func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command { + var allNamespaces bool cmd := &cobra.Command{ Use: "ingresses", Aliases: []string{"ingress", "ing"}, @@ -42,18 +43,12 @@ func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command { return err } - allNamespaces, err := cmd.Flags().GetBool("all-namespaces") - if err != nil { - return err - } - util.PrintError(ingresses(flags, host, allNamespaces)) return nil }, } cmd.Flags().String("host", "", "Show just the ingress definitions for this hostname") - cmd.Flags().Bool("all-namespaces", false, "Find ingress definitions from all namespaces") - + cmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "Find ingress definitions from all namespaces") return cmd } diff --git a/cmd/plugin/commands/lint/main.go b/cmd/plugin/commands/lint/main.go index 2daf8eb87..a45c5120b 100644 --- a/cmd/plugin/commands/lint/main.go +++ b/cmd/plugin/commands/lint/main.go @@ -92,7 +92,7 @@ func addCommonOptions(flags *genericclioptions.ConfigFlags, cmd *cobra.Command) out := lintOptions{ flags: flags, } - cmd.Flags().BoolVar(&out.allNamespaces, "all-namespaces", false, "Check resources in all namespaces") + cmd.Flags().BoolVarP(&out.allNamespaces, "all-namespaces", "A", false, "Check resources in all namespaces") cmd.Flags().BoolVar(&out.showAll, "show-all", false, "Show all resources, not just the ones with problems") cmd.Flags().BoolVarP(&out.verbose, "verbose", "v", false, "Show extra information about the lints") cmd.Flags().StringVarP(&out.versionFrom, "from-version", "f", "0.0.0", "Use lints added for versions starting with this one") diff --git a/cmd/plugin/util/util.go b/cmd/plugin/util/util.go index 4fd21c4e5..52b1d7966 100644 --- a/cmd/plugin/util/util.go +++ b/cmd/plugin/util/util.go @@ -135,6 +135,13 @@ func AddContainerFlag(cmd *cobra.Command) *string { return &v } +// AddServiceFlag adds a --service flag to a cobra command +func AddServiceFlag(cmd *cobra.Command) *string { + v := "" + cmd.Flags().StringVar(&v, "service", DefaultIngressServiceName, "The name of the ingress-nginx service") + return &v +} + // GetNamespace takes a set of kubectl flag values and returns the namespace we should be operating in func GetNamespace(flags *genericclioptions.ConfigFlags) string { namespace, _, err := flags.ToRawKubeConfigLoader().Namespace()