Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Shell completions #270

Merged
merged 10 commits into from
Jun 5, 2020
7 changes: 7 additions & 0 deletions cmd/create/createNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/spf13/cobra"

"github.com/rancher/k3d/cmd/util"
k3dc "github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
Expand Down Expand Up @@ -57,10 +58,16 @@ func NewCmdCreateNode() *cobra.Command {
// add flags
cmd.Flags().Int("replicas", 1, "Number of replicas of this node specification.")
cmd.Flags().String("role", string(k3d.WorkerRole), "Specify node role [master, worker]")
if err := cmd.RegisterFlagCompletionFunc("role", util.ValidArgsNodeRoles); err != nil {
log.Fatalln("Failed to register flag completion for '--role'", err)
}
cmd.Flags().StringP("cluster", "c", k3d.DefaultClusterName, "Select the cluster that the node shall connect to.")
if err := cmd.MarkFlagRequired("cluster"); err != nil {
log.Fatalln("Failed to mark required flag '--cluster'")
}
if err := cmd.RegisterFlagCompletionFunc("cluster", util.ValidArgsAvailableClusters); err != nil {
log.Fatalln("Failed to register flag completion for '--cluster'", err)
}

cmd.Flags().StringP("image", "i", fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.GetK3sVersion(false)), "Specify k3s image used for the node(s)")

Expand Down
4 changes: 2 additions & 2 deletions cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewCmdDelete() *cobra.Command {
// create new cobra command
cmd := &cobra.Command{
Use: "delete",
Short: "Delete a resource.",
Long: `Delete a resource.`,
Short: "Delete a resource [cluster, node].",
Long: `Delete a resource [cluster, node].`,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Errorln("Couldn't get help text")
Expand Down
10 changes: 6 additions & 4 deletions cmd/delete/deleteCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ THE SOFTWARE.
package delete

import (
"github.com/rancher/k3d/cmd/util"
"github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
Expand All @@ -35,10 +36,11 @@ func NewCmdDeleteCluster() *cobra.Command {

// create new cobra command
cmd := &cobra.Command{
Use: "cluster (NAME | --all)",
Short: "Delete a cluster.",
Long: `Delete a cluster.`,
Args: cobra.MinimumNArgs(0), // 0 or n arguments; 0 only if --all is set
Use: "cluster (NAME | --all)",
Short: "Delete a cluster.",
Long: `Delete a cluster.`,
Args: cobra.MinimumNArgs(0), // 0 or n arguments; 0 only if --all is set
ValidArgsFunction: util.ValidArgsAvailableClusters,
Run: func(cmd *cobra.Command, args []string) {
clusters := parseDeleteClusterCmd(cmd, args)

Expand Down
10 changes: 6 additions & 4 deletions cmd/delete/deleteNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ THE SOFTWARE.
package delete

import (
"github.com/rancher/k3d/cmd/util"
"github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
Expand All @@ -34,10 +35,11 @@ func NewCmdDeleteNode() *cobra.Command {

// create new cobra command
cmd := &cobra.Command{
Use: "node (NAME | --all)",
Short: "Delete a node.",
Long: `Delete a node.`,
Args: cobra.MinimumNArgs(1), // at least one node has to be specified
Use: "node (NAME | --all)",
Short: "Delete a node.",
Long: `Delete a node.`,
Args: cobra.MinimumNArgs(1), // at least one node has to be specified
ValidArgsFunction: util.ValidArgsAvailableNodes,
Run: func(cmd *cobra.Command, args []string) {

nodes := parseDeleteNodeCmd(cmd, args)
Expand Down
4 changes: 2 additions & 2 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewCmdGet() *cobra.Command {
// create new cobra command
cmd := &cobra.Command{
Use: "get",
Short: "Get a resource.",
Long: `Get a resource.`,
Short: "Get a resource [cluster, node, kubeconfig].",
Long: `Get a resource [cluster, node, kubeconfig].`,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Errorln("Couldn't get help text")
Expand Down
2 changes: 2 additions & 0 deletions cmd/get/getCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"os"
"strings"

"github.com/rancher/k3d/cmd/util"
k3cluster "github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
Expand Down Expand Up @@ -58,6 +59,7 @@ func NewCmdGetCluster() *cobra.Command {
clusters := buildClusterList(cmd.Context(), args)
PrintClusters(clusters, clusterFlags)
},
ValidArgsFunction: util.ValidArgsAvailableClusters,
}

// add flags
Expand Down
8 changes: 5 additions & 3 deletions cmd/get/getKubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"os"

"github.com/rancher/k3d/cmd/util"
"github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
Expand All @@ -48,9 +49,10 @@ func NewCmdGetKubeconfig() *cobra.Command {

// create new command
cmd := &cobra.Command{
Use: "kubeconfig [CLUSTER [CLUSTER [...]] | --all]", // TODO: getKubeconfig: allow more than one cluster name or even --all
Short: "Get kubeconfig",
Long: `Get kubeconfig.`,
Use: "kubeconfig [CLUSTER [CLUSTER [...]] | --all]", // TODO: getKubeconfig: allow more than one cluster name or even --all
Short: "Get kubeconfig",
Long: `Get kubeconfig.`,
ValidArgsFunction: util.ValidArgsAvailableClusters,
Args: func(cmd *cobra.Command, args []string) error {
if (len(args) < 1 && !getKubeconfigFlags.all) || (len(args) > 0 && getKubeconfigFlags.all) {
return fmt.Errorf("Need to specify one or more cluster names *or* set `--all` flag")
Expand Down
12 changes: 7 additions & 5 deletions cmd/get/getNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"

"github.com/liggitt/tabwriter"
"github.com/rancher/k3d/cmd/util"
"github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
Expand All @@ -41,11 +42,12 @@ func NewCmdGetNode() *cobra.Command {

// create new command
cmd := &cobra.Command{
Use: "node [NAME [NAME...]]",
Aliases: []string{"nodes"},
Short: "Get node(s)",
Long: `Get node(s).`,
Args: cobra.MinimumNArgs(0), // 0 or more; 0 = all
Use: "node [NAME [NAME...]]",
Aliases: []string{"nodes"},
Short: "Get node(s)",
Long: `Get node(s).`,
Args: cobra.MinimumNArgs(0), // 0 or more; 0 = all
ValidArgsFunction: util.ValidArgsAvailableNodes,
Run: func(cmd *cobra.Command, args []string) {
nodes, headersOff := parseGetNodeCmd(cmd, args)
var existingNodes []*k3d.Node
Expand Down
4 changes: 2 additions & 2 deletions cmd/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func NewCmdLoad() *cobra.Command {
// create new cobra command
cmd := &cobra.Command{
Use: "load",
Short: "Load a resource [image].",
Long: `Load a resource [image].`,
Short: "Load a resource [image] into a cluster.",
Long: `Load a resource [image] into a cluster.`,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Errorln("Couldn't get help text")
Expand Down
5 changes: 5 additions & 0 deletions cmd/load/loadImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package load
import (
"github.com/spf13/cobra"

"github.com/rancher/k3d/cmd/util"
"github.com/rancher/k3d/pkg/runtimes"
"github.com/rancher/k3d/pkg/tools"
k3d "github.com/rancher/k3d/pkg/types"
Expand Down Expand Up @@ -61,6 +62,10 @@ func NewCmdLoadImage() *cobra.Command {
* Flags *
*********/
cmd.Flags().StringArrayP("cluster", "c", []string{k3d.DefaultClusterName}, "Select clusters to load the image to.")
if err := cmd.RegisterFlagCompletionFunc("cluster", util.ValidArgsAvailableClusters); err != nil {
log.Fatalln("Failed to register flag completion for '--cluster'", err)
}

cmd.Flags().BoolVarP(&loadImageOpts.KeepTar, "keep-tarball", "k", false, "Do not delete the tarball containing the saved images from the shared volume")

/* Subcommands */
Expand Down
6 changes: 2 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import (
type RootFlags struct {
debugLogging bool
version bool
runtime string
}

var flags = RootFlags{}
Expand Down Expand Up @@ -87,7 +86,6 @@ func init() {
// add persistent flags (present to all subcommands)
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k3d/config.yaml)")
rootCmd.PersistentFlags().BoolVar(&flags.debugLogging, "verbose", false, "Enable verbose output (debug logging)")
rootCmd.PersistentFlags().StringVarP(&flags.runtime, "runtime", "r", "docker", "Choose a container runtime environment [docker, containerd]")

// add local flags
rootCmd.Flags().BoolVar(&flags.version, "version", false, "Show k3d and default k3s version")
Expand Down Expand Up @@ -150,7 +148,7 @@ func initLogging() {
}

func initRuntime() {
runtime, err := runtimes.GetRuntime(flags.runtime)
runtime, err := runtimes.GetRuntime("docker")
if err != nil {
log.Fatalln(err)
}
Expand All @@ -166,7 +164,7 @@ func printVersion() {
// Completion
var completionFunctions = map[string]func(io.Writer) error{
"bash": rootCmd.GenBashCompletion,
"zsh": rootCmd.GenZshCompletion, // FIXME: zsh completion requires https://github.com/spf13/cobra/pull/899 due to square brackets in our help texts
"zsh": rootCmd.GenZshCompletion,
"psh": rootCmd.GenPowerShellCompletion,
"powershell": rootCmd.GenPowerShellCompletion,
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewCmdStart() *cobra.Command {
// create new cobra command
cmd := &cobra.Command{
Use: "start",
Short: "Start a resource.",
Long: `Start a resource.`,
Short: "Start a resource [cluster, node].",
Long: `Start a resource [cluster, node].`,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Errorln("Couldn't get help text")
Expand Down
8 changes: 5 additions & 3 deletions cmd/start/startCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package start
import (
"time"

"github.com/rancher/k3d/cmd/util"
"github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
"github.com/rancher/k3d/pkg/types"
Expand All @@ -41,9 +42,10 @@ func NewCmdStartCluster() *cobra.Command {

// create new command
cmd := &cobra.Command{
Use: "cluster (NAME [NAME...] | --all)",
Short: "Start an existing k3d cluster",
Long: `Start an existing k3d cluster`,
Use: "cluster (NAME [NAME...] | --all)",
Short: "Start an existing k3d cluster",
Long: `Start an existing k3d cluster`,
ValidArgsFunction: util.ValidArgsAvailableClusters,
Run: func(cmd *cobra.Command, args []string) {
clusters := parseStartClusterCmd(cmd, args)
if len(clusters) == 0 {
Expand Down
8 changes: 5 additions & 3 deletions cmd/start/startNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ THE SOFTWARE.
package start

import (
"github.com/rancher/k3d/cmd/util"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
"github.com/spf13/cobra"
Expand All @@ -34,9 +35,10 @@ func NewCmdStartNode() *cobra.Command {

// create new command
cmd := &cobra.Command{
Use: "node NAME", // TODO: startNode: allow one or more names or --all
Short: "Start an existing k3d node",
Long: `Start an existing k3d node.`,
Use: "node NAME", // TODO: startNode: allow one or more names or --all
Short: "Start an existing k3d node",
Long: `Start an existing k3d node.`,
ValidArgsFunction: util.ValidArgsAvailableNodes,
Run: func(cmd *cobra.Command, args []string) {
node := parseStartNodeCmd(cmd, args)
if err := runtimes.SelectedRuntime.StartNode(cmd.Context(), node); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewCmdStop() *cobra.Command {
// create new cobra command
cmd := &cobra.Command{
Use: "stop",
Short: "Stop a resource.",
Long: `Stop a resource.`,
Short: "Stop a resource [cluster, node].",
Long: `Stop a resource [cluster, node].`,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Errorln("Couldn't get help text")
Expand Down
8 changes: 5 additions & 3 deletions cmd/stop/stopCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package stop
import (
"github.com/spf13/cobra"

"github.com/rancher/k3d/cmd/util"
"github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
Expand All @@ -36,9 +37,10 @@ func NewCmdStopCluster() *cobra.Command {

// create new command
cmd := &cobra.Command{
Use: "cluster (NAME [NAME...] | --all)",
Short: "Stop an existing k3d cluster",
Long: `Stop an existing k3d cluster.`,
Use: "cluster (NAME [NAME...] | --all)",
Short: "Stop an existing k3d cluster",
Long: `Stop an existing k3d cluster.`,
ValidArgsFunction: util.ValidArgsAvailableClusters,
Run: func(cmd *cobra.Command, args []string) {
clusters := parseStopClusterCmd(cmd, args)
if len(clusters) == 0 {
Expand Down
8 changes: 5 additions & 3 deletions cmd/stop/stopNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ THE SOFTWARE.
package stop

import (
"github.com/rancher/k3d/cmd/util"
"github.com/rancher/k3d/pkg/runtimes"
"github.com/spf13/cobra"

Expand All @@ -35,9 +36,10 @@ func NewCmdStopNode() *cobra.Command {

// create new command
cmd := &cobra.Command{
Use: "node NAME", // TODO: stopNode: allow one or more names or --all",
Short: "Stop an existing k3d node",
Long: `Stop an existing k3d node.`,
Use: "node NAME", // TODO: stopNode: allow one or more names or --all",
Short: "Stop an existing k3d node",
Long: `Stop an existing k3d node.`,
ValidArgsFunction: util.ValidArgsAvailableNodes,
Run: func(cmd *cobra.Command, args []string) {
node := parseStopNodeCmd(cmd, args)
if err := runtimes.SelectedRuntime.StopNode(cmd.Context(), node); err != nil {
Expand Down
Loading