123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- 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(" all : 自动部署数据库")
- fmt.Println(" send : 发送文件或文件夹")
- os.Exit(1)
- }
- switch os.Args[1] {
- case "version":
- fmt.Println("Version: 1.0.0")
- case "all":
- allCmd := flag.NewFlagSet("all", flag.ExitOnError)
- hMode := allCmd.String("s", "./file/config.toml", "指定配置文件")
- // 如果只有 "all" 命令,没有其他参数
- if len(os.Args) == 2 {
- fmt.Println("使用默认配置文件")
- global.InitNodeSetting("./file/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("./file/config.toml")
- auto.AutoDeployALL()
- }
- case "start":
- paraStart()
- case "send":
- paraSend()
- default:
- fmt.Printf("(未知命令)Unknown command: %s\n", os.Args[1])
- fmt.Println("Commands:")
- fmt.Println(" all : 自动部署数据库")
- fmt.Println(" send : 发送文件或文件夹")
- os.Exit(1)
- }
- }
- func paraSend() {
- allCmd := flag.NewFlagSet("send", flag.ExitOnError)
- hMode := allCmd.String("s", "", "指定配置文件")
- // 如果只有 "send" 命令,没有其他参数
- if len(os.Args) == 2 {
- //fmt.Println("使用默认配置文件参数发送至全部节点")
- global.InitNodeSetting("./file/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("./file/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", "./file/config.toml", "指定配置文件")
- // 如果只有 "start" 命令,没有其他参数
- if len(os.Args) == 2 {
- fmt.Println("使用默认配置文件")
- global.InitNodeSetting("./file/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("./file/config.toml")
- auto.StartAll()
- }
- }
|