Merge pull request #2283 from lonelyCZ/pr-options-cmd

karmadactl: Introduce options subcmd to list global command-line options
This commit is contained in:
karmada-bot 2022-08-01 16:00:57 +08:00 committed by GitHub
commit e153614477
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 1 deletions

View File

@ -76,7 +76,7 @@ func NewCmdInit(parentCommand string) *cobra.Command {
return nil
},
}
flags := cmd.PersistentFlags()
flags := cmd.Flags()
releaseVer, err := version.ParseGitVersion(version.Get().GitVersion)
if err != nil {

View File

@ -14,6 +14,7 @@ import (
"k8s.io/kubectl/pkg/util/templates"
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit"
"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/version/sharedcommand"
)
@ -95,6 +96,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
filters := []string{"options"}
rootCmd.AddCommand(sharedcommand.NewCmdVersion(parentCommand))
rootCmd.AddCommand(options.NewCmdOptions(parentCommand, ioStreams.Out))
templates.ActsAsRootCommand(rootCmd, filters, groups...)

View File

@ -0,0 +1,41 @@
package options
import (
"fmt"
"io"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"
)
var (
optionsExample = templates.Examples(`
# Print flags inherited by all commands
%[1]s options`)
)
// NewCmdOptions implements the options command
func NewCmdOptions(parentCommand string, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "options",
Short: "Print the list of flags inherited by all commands",
Long: "Print the list of flags inherited by all commands",
Example: fmt.Sprintf(optionsExample, parentCommand),
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Usage(); err != nil {
return err
}
return nil
},
}
// The `options` command needs write its output to the `out` stream
// (typically stdout). Without calling SetOutput here, the Usage()
// function call will fall back to stderr.
//
// See https://github.com/kubernetes/kubernetes/pull/46394 for details.
cmd.SetOutput(out)
templates.UseOptionsTemplates(cmd)
return cmd
}