Skip to content

Commit

Permalink
completion: show cluster list when click tab (#1891)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar authored May 25, 2022
1 parent fd85680 commit 47d9f8b
Show file tree
Hide file tree
Showing 32 changed files with 286 additions and 0 deletions.
8 changes: 8 additions & 0 deletions components/cluster/command/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ You can retain some nodes and roles data when cleanup the cluster, eg:

return cm.CleanCluster(clusterName, gOpt, cleanOpt, skipConfirm)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringArrayVar(&cleanOpt.RetainDataNodes, "ignore-node", nil, "Specify the nodes or hosts whose data will be retained")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ func newDeploy() *cobra.Command {

return cm.Deploy(clusterName, version, topoFile, opt, postDeployHook, skipConfirm, gOpt)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 2:
return nil, cobra.ShellCompDirectiveDefault
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringVarP(&opt.User, "user", "u", utils.CurrentUser(), "The user name to login via SSH. The user must has root (or sudo) privilege.")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ You can retain some nodes and roles data when destroy cluster, eg:

return cm.DestroyCluster(clusterName, gOpt, destroyOpt, skipConfirm)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringArrayVar(&destroyOpt.RetainDataNodes, "retain-node-data", nil, "Specify the nodes or hosts whose data will be retained")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func newDisableCmd() *cobra.Command {

return cm.EnableCluster(clusterName, gOpt, false)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only disable specified roles")
Expand Down
21 changes: 21 additions & 0 deletions components/cluster/command/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package command
import (
"errors"
"fmt"
"strings"
"time"

perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/manager"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/meta"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -77,6 +79,14 @@ func newDisplayCmd() *cobra.Command {
}
return cm.Display(clusterName, gOpt)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only display specified roles")
Expand All @@ -89,3 +99,14 @@ func newDisplayCmd() *cobra.Command {

return cmd
}

func shellCompGetClusterName(cm *manager.Manager, toComplete string) ([]string, cobra.ShellCompDirective) {
var result []string
clusters, _ := cm.GetClusterList()
for _, c := range clusters {
if strings.HasPrefix(c.Name, toComplete) {
result = append(result, c.Name)
}
}
return result, cobra.ShellCompDirectiveNoFileComp
}
8 changes: 8 additions & 0 deletions components/cluster/command/edit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func newEditConfigCmd() *cobra.Command {

return cm.EditConfig(clusterName, opt, skipConfirm)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringVarP(&opt.NewTopoFile, "topology-file", "", opt.NewTopoFile, "Use provided topology file to substitute the original one instead of editing it.")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func newEnableCmd() *cobra.Command {

return cm.EnableCluster(clusterName, gOpt, true)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only enable specified roles")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func newExecCmd() *cobra.Command {

return cm.Exec(clusterName, opt, gOpt)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringVar(&opt.Command, "command", "ls", "the command run on cluster host")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func newPruneCmd() *cobra.Command {

return cm.DestroyTombstone(clusterName, gOpt, skipConfirm)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().BoolVar(&gOpt.Force, "force", false, "Ignore errors when deleting the instance with data from the cluster")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func newReloadCmd() *cobra.Command {

return cm.Reload(clusterName, gOpt, skipRestart, skipConfirm)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().BoolVar(&gOpt.Force, "force", false, "Force reload without transferring PD leader and ignore remote error")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func newRestartCmd() *cobra.Command {

return cm.RestartCluster(clusterName, gOpt, skipConfirm)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only restart specified roles")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/scale_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func newScaleInCmd() *cobra.Command {

return cm.ScaleIn(clusterName, skipConfirm, gOpt, scale)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringSliceVarP(&gOpt.Nodes, "node", "N", nil, "Specify the nodes (required)")
Expand Down
10 changes: 10 additions & 0 deletions components/cluster/command/scale_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ func newScaleOutCmd() *cobra.Command {
gOpt,
)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
case 1:
return nil, cobra.ShellCompDirectiveDefault
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringVarP(&opt.User, "user", "u", utils.CurrentUser(), "The user name to login via SSH. The user must has root (or sudo) privilege.")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/show_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func newShowConfigCmd() *cobra.Command {

return cm.ShowConfig(clusterName)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

return cmd
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ func newStartCmd() *cobra.Command {
}
return nil
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().BoolVar(&initPasswd, "init", false, "Initialize a secure root password for the database")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func newStopCmd() *cobra.Command {

return cm.StopCluster(clusterName, gOpt, skipConfirm, evictLeader)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only stop specified roles")
Expand Down
8 changes: 8 additions & 0 deletions components/cluster/command/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func newUpgradeCmd() *cobra.Command {

return cm.Upgrade(clusterName, version, gOpt, skipConfirm, offlineMode)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}
cmd.Flags().BoolVar(&gOpt.Force, "force", false, "Force upgrade without transferring PD leader")
cmd.Flags().Uint64Var(&gOpt.APITimeout, "transfer-timeout", 600, "Timeout in seconds when transferring PD and TiKV store leaders")
Expand Down
8 changes: 8 additions & 0 deletions components/dm/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ func newDeployCmd() *cobra.Command {

return cm.Deploy(clusterName, version, topoFile, opt, postDeployHook, skipConfirm, gOpt)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 2:
return nil, cobra.ShellCompDirectiveDefault
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringVarP(&opt.User, "user", "u", utils.CurrentUser(), "The user name to login via SSH. The user must has root (or sudo) privilege.")
Expand Down
8 changes: 8 additions & 0 deletions components/dm/command/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func newDestroyCmd() *cobra.Command {

return cm.DestroyCluster(clusterName, gOpt, destroyOpt, skipConfirm)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringArrayVar(&destroyOpt.RetainDataNodes, "retain-node-data", nil, "Specify the nodes or hosts whose data will be retained")
Expand Down
8 changes: 8 additions & 0 deletions components/dm/command/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func newDisableCmd() *cobra.Command {

return cm.EnableCluster(clusterName, gOpt, false)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only disable specified roles")
Expand Down
21 changes: 21 additions & 0 deletions components/dm/command/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package command
import (
"errors"
"fmt"
"strings"

perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/manager"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/meta"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -51,6 +53,14 @@ func newDisplayCmd() *cobra.Command {

return cm.Display(clusterName, gOpt)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only display specified roles")
Expand All @@ -61,3 +71,14 @@ func newDisplayCmd() *cobra.Command {

return cmd
}

func shellCompGetClusterName(cm *manager.Manager, toComplete string) ([]string, cobra.ShellCompDirective) {
var result []string
clusters, _ := cm.GetClusterList()
for _, c := range clusters {
if strings.HasPrefix(c.Name, toComplete) {
result = append(result, c.Name)
}
}
return result, cobra.ShellCompDirectiveNoFileComp
}
8 changes: 8 additions & 0 deletions components/dm/command/edit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func newEditConfigCmd() *cobra.Command {

return cm.EditConfig(clusterName, opt, skipConfirm)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringVarP(&opt.NewTopoFile, "topology-file", "", opt.NewTopoFile, "Use provided topology file to substitute the original one instead of editing it.")
Expand Down
8 changes: 8 additions & 0 deletions components/dm/command/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func newEnableCmd() *cobra.Command {

return cm.EnableCluster(clusterName, gOpt, true)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}

cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only enable specified roles")
Expand Down
Loading

0 comments on commit 47d9f8b

Please sign in to comment.