cleanup: rootcmd use the common way of creating a cobra cmd
This commit is contained in:
parent
5364bd1300
commit
ed7db5daa9
71
cmd/root.go
71
cmd/root.go
@ -56,8 +56,10 @@ type RootFlags struct {
|
||||
|
||||
var flags = RootFlags{}
|
||||
|
||||
func NewCmdK3d() *cobra.Command {
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "k3d",
|
||||
Short: "https://k3d.io/ -> Run k3s in Docker!",
|
||||
Long: `https://k3d.io/
|
||||
@ -75,33 +77,6 @@ All Nodes of a k3d cluster are part of the same docker network.`,
|
||||
},
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
if len(os.Args) > 1 {
|
||||
parts := os.Args[1:]
|
||||
// Check if it's a built-in command, else try to execute it as a plugin
|
||||
if _, _, err := rootCmd.Find(parts); err != nil {
|
||||
pluginFound, err := cliutil.HandlePlugin(context.Background(), parts)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to execute plugin '%+v'", parts)
|
||||
log.Fatalln(err)
|
||||
} else if pluginFound {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetRootCmd() *cobra.Command {
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
rootCmd.PersistentFlags().BoolVar(&flags.debugLogging, "verbose", false, "Enable verbose output (debug logging)")
|
||||
rootCmd.PersistentFlags().BoolVar(&flags.traceLogging, "trace", false, "Enable super verbose output (trace logging)")
|
||||
rootCmd.PersistentFlags().BoolVar(&flags.timestampedLogging, "timestamps", false, "Enable Log timestamps")
|
||||
@ -110,7 +85,7 @@ func init() {
|
||||
rootCmd.Flags().BoolVar(&flags.version, "version", false, "Show k3d and default k3s version")
|
||||
|
||||
// add subcommands
|
||||
rootCmd.AddCommand(NewCmdCompletion())
|
||||
rootCmd.AddCommand(NewCmdCompletion(rootCmd))
|
||||
rootCmd.AddCommand(cluster.NewCmdCluster())
|
||||
rootCmd.AddCommand(kubeconfig.NewCmdKubeconfig())
|
||||
rootCmd.AddCommand(node.NewCmdNode())
|
||||
@ -147,6 +122,30 @@ func init() {
|
||||
|
||||
// Init
|
||||
cobra.OnInitialize(initLogging, initRuntime)
|
||||
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
cmd := NewCmdK3d()
|
||||
if len(os.Args) > 1 {
|
||||
parts := os.Args[1:]
|
||||
// Check if it's a built-in command, else try to execute it as a plugin
|
||||
if _, _, err := cmd.Find(parts); err != nil {
|
||||
pluginFound, err := cliutil.HandlePlugin(context.Background(), parts)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to execute plugin '%+v'", parts)
|
||||
log.Fatalln(err)
|
||||
} else if pluginFound {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := cmd.Execute(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
// initLogging initializes the logger
|
||||
@ -216,12 +215,10 @@ func printVersion() {
|
||||
fmt.Printf("k3s version %s (default)\n", version.K3sVersion)
|
||||
}
|
||||
|
||||
func generateFishCompletion(writer io.Writer) error {
|
||||
return rootCmd.GenFishCompletion(writer, true)
|
||||
}
|
||||
// NewCmdCompletion creates a new completion command
|
||||
func NewCmdCompletion(rootCmd *cobra.Command) *cobra.Command {
|
||||
|
||||
// Completion
|
||||
var completionFunctions = map[string]func(io.Writer) error{
|
||||
completionFunctions := map[string]func(io.Writer) error{
|
||||
"bash": rootCmd.GenBashCompletion,
|
||||
"zsh": func(writer io.Writer) error {
|
||||
if err := rootCmd.GenZshCompletion(writer); err != nil {
|
||||
@ -234,11 +231,11 @@ var completionFunctions = map[string]func(io.Writer) error{
|
||||
},
|
||||
"psh": rootCmd.GenPowerShellCompletion,
|
||||
"powershell": rootCmd.GenPowerShellCompletionWithDesc,
|
||||
"fish": generateFishCompletion,
|
||||
"fish": func(writer io.Writer) error {
|
||||
return rootCmd.GenFishCompletion(writer, true)
|
||||
},
|
||||
}
|
||||
|
||||
// NewCmdCompletion creates a new completion command
|
||||
func NewCmdCompletion() *cobra.Command {
|
||||
// create new cobra command
|
||||
cmd := &cobra.Command{
|
||||
Use: "completion SHELL",
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
k3d := cmd.GetRootCmd()
|
||||
k3d := cmd.NewCmdK3d()
|
||||
k3d.DisableAutoGenTag = true
|
||||
|
||||
if err := doc.GenMarkdownTree(k3d, "../docs/usage/commands"); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user