cmd.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package cmd
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "xg_auto_deploy/internal/auto"
  8. "xg_auto_deploy/internal/global"
  9. )
  10. // Execute 初始化和运行应用程序
  11. func Execute() {
  12. if len(os.Args) < 2 {
  13. fmt.Println("Commands:")
  14. fmt.Println(" all : 自动部署数据库")
  15. fmt.Println(" send : 发送文件或文件夹")
  16. os.Exit(1)
  17. }
  18. switch os.Args[1] {
  19. case "version":
  20. fmt.Println("Version: 1.0.0")
  21. case "all":
  22. allCmd := flag.NewFlagSet("all", flag.ExitOnError)
  23. hMode := allCmd.String("s", "./file/config.toml", "指定配置文件")
  24. // 如果只有 "all" 命令,没有其他参数
  25. if len(os.Args) == 2 {
  26. fmt.Println("使用默认配置文件")
  27. global.InitNodeSetting("./file/config.toml")
  28. auto.AutoDeployALL()
  29. return
  30. }
  31. // 解析 all 命令的参数
  32. allCmd.Parse(os.Args[2:])
  33. if *hMode != "" {
  34. fmt.Printf("指定配置文件: %s\n", *hMode)
  35. global.InitNodeSetting(*hMode)
  36. auto.AutoDeployALL()
  37. } else {
  38. fmt.Println("使用默认配置文件")
  39. global.InitNodeSetting("./file/config.toml")
  40. auto.AutoDeployALL()
  41. }
  42. case "start":
  43. paraStart()
  44. case "send":
  45. paraSend()
  46. default:
  47. fmt.Printf("(未知命令)Unknown command: %s\n", os.Args[1])
  48. fmt.Println("Commands:")
  49. fmt.Println(" all : 自动部署数据库")
  50. fmt.Println(" send : 发送文件或文件夹")
  51. os.Exit(1)
  52. }
  53. }
  54. func paraSend() {
  55. allCmd := flag.NewFlagSet("send", flag.ExitOnError)
  56. hMode := allCmd.String("s", "", "指定配置文件")
  57. // 如果只有 "send" 命令,没有其他参数
  58. if len(os.Args) == 2 {
  59. //fmt.Println("使用默认配置文件参数发送至全部节点")
  60. global.InitNodeSetting("./file/config.toml")
  61. auto.SendToNodes([]string{"all"}, "", "")
  62. return
  63. }
  64. // 解析 all 命令的参数
  65. allCmd.Parse(os.Args[2:])
  66. if *hMode != "" {
  67. //fmt.Printf("指定配置文件参数发送: %s\n", *hMode)
  68. global.InitNodeSetting(*hMode)
  69. paraArgs := os.Args[2:]
  70. switch len(paraArgs) {
  71. case 2:
  72. //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", *hMode)
  73. auto.SendToNodes([]string{"all"}, "", "")
  74. return
  75. case 3:
  76. if paraArgs[2] == "all" {
  77. //fmt.Printf("发送至所有节点\n")
  78. auto.SendToNodes([]string{"all"}, "", "")
  79. return
  80. }
  81. nodeIds := strings.Split(paraArgs[2], ",")
  82. //fmt.Printf("发送至指定节点: %s\n", nodeIds)
  83. auto.SendToNodes(nodeIds, "", "")
  84. return
  85. case 4:
  86. if paraArgs[2] == "all" {
  87. //fmt.Printf("发送至所有节点\n")
  88. auto.SendToNodes([]string{"all"}, paraArgs[3], "")
  89. return
  90. }
  91. nodeIds := strings.Split(paraArgs[2], ",")
  92. //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  93. auto.SendToNodes(nodeIds, paraArgs[3], "")
  94. case 5:
  95. if paraArgs[2] == "all" {
  96. //fmt.Printf("发送至所有节点\n")
  97. auto.SendToNodes([]string{"all"}, paraArgs[3], paraArgs[4])
  98. return
  99. }
  100. nodeIds := strings.Split(paraArgs[2], ",")
  101. // fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  102. auto.SendToNodes(nodeIds, paraArgs[3], paraArgs[4])
  103. }
  104. } else {
  105. //fmt.Println("使用默认配置文件参数发送")
  106. paraArgs := os.Args[2:]
  107. global.InitNodeSetting("./file/config.toml")
  108. switch len(paraArgs) {
  109. case 1:
  110. nodeIds := strings.Split(paraArgs[0], ",")
  111. fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  112. auto.SendToNodes(nodeIds, "", "")
  113. case 2:
  114. nodeIds := strings.Split(paraArgs[0], ",")
  115. fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  116. auto.SendToNodes(nodeIds, paraArgs[1], "")
  117. case 3:
  118. nodeIds := strings.Split(paraArgs[0], ",")
  119. fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  120. auto.SendToNodes(nodeIds, paraArgs[1], paraArgs[2])
  121. }
  122. }
  123. }
  124. func paraStart() {
  125. allCmd := flag.NewFlagSet("start", flag.ExitOnError)
  126. hMode := allCmd.String("s", "./file/config.toml", "指定配置文件")
  127. // 如果只有 "start" 命令,没有其他参数
  128. if len(os.Args) == 2 {
  129. fmt.Println("使用默认配置文件")
  130. global.InitNodeSetting("./file/config.toml")
  131. auto.StartAll()
  132. return
  133. }
  134. // 解析 all 命令的参数
  135. allCmd.Parse(os.Args[2:])
  136. if *hMode != "" {
  137. fmt.Printf("指定配置文件: %s\n", *hMode)
  138. global.InitNodeSetting(*hMode)
  139. auto.StartAll()
  140. } else {
  141. fmt.Println("使用默认配置文件")
  142. global.InitNodeSetting("./file/config.toml")
  143. auto.StartAll()
  144. }
  145. }