123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- package cmd
- import (
- "flag"
- "fmt"
- "os"
- "strings"
- "xg_auto_deploy/internal/auto"
- "xg_auto_deploy/internal/global"
- )
- // Execute 初始化和运行应用程序
- func Execute() {
- if len(os.Args) < 2 {
- fmt.Println("Commands:")
- fmt.Println(" install : 自动部署数据库")
- fmt.Println(" send : 发送文件或文件夹")
- fmt.Println(" start : 启动配置文件内的所有节点")
- fmt.Println(" killdb : kill -9 数据库")
- fmt.Println(" cmd : 向指定节点发送命令")
- os.Exit(1)
- }
- switch os.Args[1] {
- case "version":
- fmt.Println("Version: 1.0.0")
- case "install":
- parainstall()
- case "start":
- paraStart()
- case "send":
- paraSend()
- case "cmd":
- paraSendCmd()
- case "killdb":
- paraKillDB()
- default:
- fmt.Printf("(未知命令)Unknown command: %s\n", os.Args[1])
- fmt.Println("Commands:")
- fmt.Println(" install : 自动部署数据库")
- fmt.Println(" send : 发送文件或文件夹")
- fmt.Println(" start : 启动配置文件内的所有节点")
- fmt.Println(" killdb : kill -9 数据库")
- fmt.Println(" cmd : 向指定节点发送命令")
- os.Exit(1)
- }
- }
- func paraSendCmd() {
- SendCmd := flag.NewFlagSet("cmd", flag.ExitOnError)
- hMode := SendCmd.String("s", "", "指定配置文件")
- // 如果只有 "send" 命令,没有其他参数
- if len(os.Args) == 2 {
- fmt.Println("缺少命令参数")
- //global.InitNodeSetting("./config.toml")
- return
- }
- // 解析 all 命令的参数
- SendCmd.Parse(os.Args[2:])
- if *hMode != "" {
- //fmt.Printf("指定配置文件参数发送: %s\n", *hMode)
- global.InitNodeSetting(*hMode)
- paraArgs := os.Args[2:]
- switch len(paraArgs) {
- case 2:
- //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", *hMode)
- fmt.Println("缺少命令参数 cmd")
- return
- case 3:
- // fmt.Println("缺少命令参数 cmd")
- auto.CmdToNodes([]string{"all"}, paraArgs[2])
- return
- case 4:
- if paraArgs[2] == "all" {
- //fmt.Printf("发送至所有节点\n")
- auto.CmdToNodes([]string{"all"}, paraArgs[3])
- return
- }
- nodeIds := strings.Split(paraArgs[2], ",")
- //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
- auto.CmdToNodes(nodeIds, paraArgs[3])
- }
- } else {
- //fmt.Println("使用默认配置文件参数发送")
- paraArgs := os.Args[2:]
- global.InitNodeSetting("./config.toml")
- switch len(paraArgs) {
- case 1:
- nodeIds := strings.Split(paraArgs[0], ",")
- fmt.Printf("使用默认配置文件参数发送至所有节点: %s\n", nodeIds)
- //auto.SendToNodes(nodeIds, "", "")
- auto.CmdToNodes([]string{"all"}, paraArgs[0])
- case 2:
- nodeIds := strings.Split(paraArgs[0], ",")
- fmt.Printf("使用默认配置文件参数发送至所有节点: %s\n", nodeIds)
- auto.CmdToNodes(nodeIds, paraArgs[1])
- }
- }
- }
- func parainstall() {
- allCmd := flag.NewFlagSet("install", flag.ExitOnError)
- hMode := allCmd.String("s", "./config.toml", "指定配置文件")
- // 如果只有 "all" 命令,没有其他参数
- if len(os.Args) == 2 {
- fmt.Println("使用默认配置文件")
- global.InitNodeSetting("./config.toml")
- auto.AutoDeployALL()
- return
- }
- // 解析 all 命令的参数
- allCmd.Parse(os.Args[2:])
- if *hMode != "" {
- fmt.Printf("指定配置文件: %s\n", *hMode)
- global.InitNodeSetting(*hMode)
- auto.AutoDeployALL()
- } else {
- fmt.Println("使用默认配置文件")
- global.InitNodeSetting("./config.toml")
- auto.AutoDeployALL()
- }
- }
- func paraKillDB() {
- killdbCmd := flag.NewFlagSet("killdb", flag.ExitOnError)
- hMode := killdbCmd.String("s", "", "指定配置文件")
- // 如果只有 "send" 命令,没有其他参数
- if len(os.Args) == 2 {
- //fmt.Println("使用默认配置文件参数发送至全部节点")
- global.InitNodeSetting("./config.toml")
- auto.KillToNodes([]string{"all"})
- return
- }
- // 解析 all 命令的参数
- killdbCmd.Parse(os.Args[2:])
- if *hMode != "" {
- //fmt.Printf("指定配置文件参数发送: %s\n", *hMode)
- global.InitNodeSetting(*hMode)
- paraArgs := os.Args[2:]
- switch len(paraArgs) {
- case 2:
- //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", *hMode)
- auto.KillToNodes([]string{"all"})
- return
- case 3:
- if paraArgs[2] == "all" {
- //fmt.Printf("发送至所有节点\n")
- auto.KillToNodes([]string{"all"})
- return
- }
- nodeIds := strings.Split(paraArgs[2], ",")
- //fmt.Printf("发送至指定节点: %s\n", nodeIds)
- auto.KillToNodes(nodeIds)
- return
- }
- } else {
- fmt.Println("使用默认配置文件参数发送")
- global.InitNodeSetting("./config.toml")
- auto.KillToNodes([]string{"all"})
- }
- }
- func paraSend() {
- allCmd := flag.NewFlagSet("send", flag.ExitOnError)
- hMode := allCmd.String("s", "", "指定配置文件")
- // 如果只有 "send" 命令,没有其他参数
- if len(os.Args) == 2 {
- //fmt.Println("使用默认配置文件参数发送至全部节点")
- global.InitNodeSetting("./config.toml")
- auto.SendToNodes([]string{"all"}, "", "")
- return
- }
- // 解析 all 命令的参数
- allCmd.Parse(os.Args[2:])
- if *hMode != "" {
- //fmt.Printf("指定配置文件参数发送: %s\n", *hMode)
- global.InitNodeSetting(*hMode)
- paraArgs := os.Args[2:]
- switch len(paraArgs) {
- case 2:
- //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", *hMode)
- auto.SendToNodes([]string{"all"}, "", "")
- return
- case 3:
- if paraArgs[2] == "all" {
- //fmt.Printf("发送至所有节点\n")
- auto.SendToNodes([]string{"all"}, "", "")
- return
- }
- nodeIds := strings.Split(paraArgs[2], ",")
- //fmt.Printf("发送至指定节点: %s\n", nodeIds)
- auto.SendToNodes(nodeIds, "", "")
- return
- case 4:
- if paraArgs[2] == "all" {
- //fmt.Printf("发送至所有节点\n")
- auto.SendToNodes([]string{"all"}, paraArgs[3], "")
- return
- }
- nodeIds := strings.Split(paraArgs[2], ",")
- //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
- auto.SendToNodes(nodeIds, paraArgs[3], "")
- case 5:
- if paraArgs[2] == "all" {
- //fmt.Printf("发送至所有节点\n")
- auto.SendToNodes([]string{"all"}, paraArgs[3], paraArgs[4])
- return
- }
- nodeIds := strings.Split(paraArgs[2], ",")
- // fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
- auto.SendToNodes(nodeIds, paraArgs[3], paraArgs[4])
- }
- } else {
- //fmt.Println("使用默认配置文件参数发送")
- paraArgs := os.Args[2:]
- global.InitNodeSetting("./config.toml")
- switch len(paraArgs) {
- case 1:
- nodeIds := strings.Split(paraArgs[0], ",")
- fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
- auto.SendToNodes(nodeIds, "", "")
- case 2:
- nodeIds := strings.Split(paraArgs[0], ",")
- fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
- auto.SendToNodes(nodeIds, paraArgs[1], "")
- case 3:
- nodeIds := strings.Split(paraArgs[0], ",")
- fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
- auto.SendToNodes(nodeIds, paraArgs[1], paraArgs[2])
- }
- }
- }
- func paraStart() {
- allCmd := flag.NewFlagSet("start", flag.ExitOnError)
- hMode := allCmd.String("s", "", "指定配置文件")
- // 如果只有 "start" 命令,没有其他参数
- if len(os.Args) == 2 {
- fmt.Println("使用默认配置文件")
- global.InitNodeSetting("./config.toml")
- auto.StartAll()
- return
- }
- // 解析 all 命令的参数
- allCmd.Parse(os.Args[2:])
- if *hMode != "" {
- fmt.Printf("指定配置文件: %s\n", *hMode)
- global.InitNodeSetting(*hMode)
- auto.StartAll()
- } else {
- fmt.Println("使用默认配置文件")
- global.InitNodeSetting("./config.toml")
- auto.StartAll()
- }
- }
|