feat: add sub-command options to print support global flags

This commit is contained in:
fengcaiwen
2023-01-14 19:10:18 +08:00
parent 21f79e03d8
commit 710904b350
2 changed files with 61 additions and 9 deletions

View File

@@ -0,0 +1,33 @@
package cmds
import (
"os"
"github.com/spf13/cobra"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
var (
optionsExample = templates.Examples(i18n.T(`
# Print flags inherited by all commands
kubectl options`))
)
func CmdOptions(cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "options",
Short: i18n.T("Print the list of flags inherited by all commands"),
Long: i18n.T("Print the list of flags inherited by all commands"),
Example: optionsExample,
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
cmd.SetOut(os.Stdout)
templates.UseOptionsTemplates(cmd)
return cmd
}

View File

@@ -4,13 +4,17 @@ import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
func NewKubeVPNCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "kubevpn",
Short: "kubevpn",
Long: `kubevpn`,
Short: i18n.T("kubevpn connect to Kubernetes cluster network"),
Long: templates.LongDesc(`
kubevpn connect to Kubernetes cluster network.
`),
}
flags := cmd.PersistentFlags()
@@ -20,12 +24,27 @@ func NewKubeVPNCommand() *cobra.Command {
matchVersionFlags.AddFlags(flags)
factory := cmdutil.NewFactory(matchVersionFlags)
cmd.AddCommand(CmdConnect(factory))
cmd.AddCommand(CmdReset(factory))
cmd.AddCommand(CmdControlPlane(factory))
cmd.AddCommand(CmdServe(factory))
cmd.AddCommand(CmdUpgrade(factory))
cmd.AddCommand(CmdWebhook(factory))
cmd.AddCommand(CmdVersion(factory))
groups := templates.CommandGroups{
{
Message: "Client Commands:",
Commands: []*cobra.Command{
CmdConnect(factory),
CmdReset(factory),
CmdUpgrade(factory),
CmdVersion(factory),
CmdOptions(factory),
},
},
{
Message: "Server Commands (DO NOT USE IT !!!):",
Commands: []*cobra.Command{
CmdControlPlane(factory),
CmdServe(factory),
CmdWebhook(factory),
},
},
}
groups.Add(cmd)
templates.ActsAsRootCommand(cmd, []string{"options"}, groups...)
return cmd
}