|
|
|
@ -56,8 +56,10 @@ type RootFlags struct { |
|
|
|
|
|
|
|
|
|
var flags = RootFlags{} |
|
|
|
|
|
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
|
var rootCmd = &cobra.Command{ |
|
|
|
|
func NewCmdK3d() *cobra.Command { |
|
|
|
|
|
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
|
rootCmd := &cobra.Command{ |
|
|
|
|
Use: "k3d", |
|
|
|
|
Short: "https://k3d.io/ -> Run k3s in Docker!", |
|
|
|
|
Long: `https://k3d.io/
|
|
|
|
@ -73,34 +75,7 @@ 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)") |
|
|
|
@ -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", |
|
|
|
|