-
Notifications
You must be signed in to change notification settings - Fork 305
/
main.go
91 lines (81 loc) · 1.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path"
"github.com/fatih/color"
"github.com/pingcap/tiup/pkg/localdata"
"github.com/pingcap/tiup/pkg/utils"
"github.com/spf13/cobra"
)
func main() {
if err := execute(); err != nil {
os.Exit(1)
}
}
func execute() error {
home := os.Getenv(localdata.EnvNameComponentInstallDir)
if home == "" {
return errors.New("component `ctl` cannot run in standalone mode")
}
rootCmd := &cobra.Command{
Use: "tiup ctl {tidb/pd/tikv/binlog/etcd}",
Short: "TiDB controllers",
SilenceUsage: true,
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return cmd.Help()
}
var transparentParams []string
componentSpec := args[0]
for i, arg := range os.Args {
if arg == componentSpec {
transparentParams = os.Args[i+1:]
break
}
}
bin, err := binaryPath(home, componentSpec)
if err != nil {
return err
}
return run(bin, transparentParams...)
},
}
originHelpFunc := rootCmd.HelpFunc()
rootCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
originHelpFunc(cmd, args)
return
}
args = utils.RebuildArgs(args)
bin, err := binaryPath(home, args[0])
if err != nil {
fmt.Println(color.RedString("Error: %v", err))
return
}
if err := run(bin, args[1:]...); err != nil {
fmt.Println(color.RedString("Error: %v", err))
}
})
return rootCmd.Execute()
}
func binaryPath(home, cmd string) (string, error) {
switch cmd {
case "tidb", "tikv", "pd":
return path.Join(home, cmd+"-ctl"), nil
case "binlog", "etcd":
return path.Join(home, cmd+"ctl"), nil
default:
return "", errors.New("ctl only supports tidb, tikv, pd, binlog and etcd currently")
}
}
func run(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}