github.com/pingcap/tiup@v1.15.1/components/dm/command/display.go (about) 1 // Copyright 2020 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package command 15 16 import ( 17 "errors" 18 "fmt" 19 "strings" 20 21 perrs "github.com/pingcap/errors" 22 "github.com/pingcap/tiup/pkg/cluster/manager" 23 "github.com/pingcap/tiup/pkg/cluster/spec" 24 "github.com/pingcap/tiup/pkg/meta" 25 "github.com/spf13/cobra" 26 ) 27 28 func newDisplayCmd() *cobra.Command { 29 var ( 30 dopt manager.DisplayOption 31 showVersionOnly bool 32 statusTimeout uint64 33 ) 34 cmd := &cobra.Command{ 35 Use: "display <cluster-name>", 36 Short: "Display information of a DM cluster", 37 RunE: func(cmd *cobra.Command, args []string) error { 38 if len(args) != 1 { 39 return cmd.Help() 40 } 41 42 gOpt.APITimeout = statusTimeout 43 dopt.ClusterName = args[0] 44 45 if showVersionOnly { 46 metadata, err := spec.ClusterMetadata(dopt.ClusterName) 47 if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) { 48 return err 49 } 50 fmt.Println(metadata.Version) 51 return nil 52 } 53 54 return cm.Display(dopt, gOpt) 55 }, 56 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 57 switch len(args) { 58 case 0: 59 return shellCompGetClusterName(cm, toComplete) 60 default: 61 return nil, cobra.ShellCompDirectiveNoFileComp 62 } 63 }, 64 } 65 66 cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only display specified roles") 67 cmd.Flags().StringSliceVarP(&gOpt.Nodes, "node", "N", nil, "Only display specified nodes") 68 cmd.Flags().BoolVar(&showVersionOnly, "version", false, "Only display DM cluster version") 69 cmd.Flags().BoolVar(&dopt.ShowUptime, "uptime", false, "Display DM with uptime") 70 cmd.Flags().Uint64Var(&statusTimeout, "status-timeout", 10, "Timeout in seconds when getting node status") 71 72 return cmd 73 } 74 75 func shellCompGetClusterName(cm *manager.Manager, toComplete string) ([]string, cobra.ShellCompDirective) { 76 var result []string 77 clusters, _ := cm.GetClusterList() 78 for _, c := range clusters { 79 if strings.HasPrefix(c.Name, toComplete) { 80 result = append(result, c.Name) 81 } 82 } 83 return result, cobra.ShellCompDirectiveNoFileComp 84 }