cmd.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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(" install : 自动部署数据库")
  15. fmt.Println(" send : 发送文件或文件夹")
  16. fmt.Println(" start : 启动配置文件内的所有节点")
  17. fmt.Println(" killdb : kill -9 数据库")
  18. fmt.Println(" cmd : 向指定节点发送命令")
  19. os.Exit(1)
  20. }
  21. switch os.Args[1] {
  22. case "version":
  23. fmt.Println("Version: 1.0.0")
  24. case "install":
  25. parainstall()
  26. case "start":
  27. paraStart()
  28. case "send":
  29. paraSend()
  30. case "cmd":
  31. paraSendCmd()
  32. case "killdb":
  33. paraKillDB()
  34. default:
  35. fmt.Printf("(未知命令)Unknown command: %s\n", os.Args[1])
  36. fmt.Println("Commands:")
  37. fmt.Println(" install : 自动部署数据库")
  38. fmt.Println(" send : 发送文件或文件夹")
  39. fmt.Println(" start : 启动配置文件内的所有节点")
  40. fmt.Println(" killdb : kill -9 数据库")
  41. fmt.Println(" cmd : 向指定节点发送命令")
  42. os.Exit(1)
  43. }
  44. }
  45. func paraSendCmd() {
  46. SendCmd := flag.NewFlagSet("cmd", flag.ExitOnError)
  47. hMode := SendCmd.String("s", "", "指定配置文件")
  48. // 如果只有 "send" 命令,没有其他参数
  49. if len(os.Args) == 2 {
  50. fmt.Println("缺少命令参数")
  51. //global.InitNodeSetting("./config.toml")
  52. return
  53. }
  54. // 解析 all 命令的参数
  55. SendCmd.Parse(os.Args[2:])
  56. if *hMode != "" {
  57. //fmt.Printf("指定配置文件参数发送: %s\n", *hMode)
  58. global.InitNodeSetting(*hMode)
  59. paraArgs := os.Args[2:]
  60. switch len(paraArgs) {
  61. case 2:
  62. //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", *hMode)
  63. fmt.Println("缺少命令参数 cmd")
  64. return
  65. case 3:
  66. // fmt.Println("缺少命令参数 cmd")
  67. auto.CmdToNodes([]string{"all"}, paraArgs[2])
  68. return
  69. case 4:
  70. if paraArgs[2] == "all" {
  71. //fmt.Printf("发送至所有节点\n")
  72. auto.CmdToNodes([]string{"all"}, paraArgs[3])
  73. return
  74. }
  75. nodeIds := strings.Split(paraArgs[2], ",")
  76. //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  77. auto.CmdToNodes(nodeIds, paraArgs[3])
  78. }
  79. } else {
  80. //fmt.Println("使用默认配置文件参数发送")
  81. paraArgs := os.Args[2:]
  82. global.InitNodeSetting("./config.toml")
  83. switch len(paraArgs) {
  84. case 1:
  85. nodeIds := strings.Split(paraArgs[0], ",")
  86. fmt.Printf("使用默认配置文件参数发送至所有节点: %s\n", nodeIds)
  87. //auto.SendToNodes(nodeIds, "", "")
  88. auto.CmdToNodes([]string{"all"}, paraArgs[0])
  89. case 2:
  90. nodeIds := strings.Split(paraArgs[0], ",")
  91. fmt.Printf("使用默认配置文件参数发送至所有节点: %s\n", nodeIds)
  92. auto.CmdToNodes(nodeIds, paraArgs[1])
  93. }
  94. }
  95. }
  96. func parainstall() {
  97. allCmd := flag.NewFlagSet("install", flag.ExitOnError)
  98. hMode := allCmd.String("s", "./config.toml", "指定配置文件")
  99. // 如果只有 "all" 命令,没有其他参数
  100. if len(os.Args) == 2 {
  101. fmt.Println("使用默认配置文件")
  102. global.InitNodeSetting("./config.toml")
  103. auto.AutoDeployALL()
  104. return
  105. }
  106. // 解析 all 命令的参数
  107. allCmd.Parse(os.Args[2:])
  108. if *hMode != "" {
  109. fmt.Printf("指定配置文件: %s\n", *hMode)
  110. global.InitNodeSetting(*hMode)
  111. auto.AutoDeployALL()
  112. } else {
  113. fmt.Println("使用默认配置文件")
  114. global.InitNodeSetting("./config.toml")
  115. auto.AutoDeployALL()
  116. }
  117. }
  118. func paraKillDB() {
  119. killdbCmd := flag.NewFlagSet("killdb", flag.ExitOnError)
  120. hMode := killdbCmd.String("s", "", "指定配置文件")
  121. // 如果只有 "send" 命令,没有其他参数
  122. if len(os.Args) == 2 {
  123. //fmt.Println("使用默认配置文件参数发送至全部节点")
  124. global.InitNodeSetting("./config.toml")
  125. auto.KillToNodes([]string{"all"})
  126. return
  127. }
  128. // 解析 all 命令的参数
  129. killdbCmd.Parse(os.Args[2:])
  130. if *hMode != "" {
  131. //fmt.Printf("指定配置文件参数发送: %s\n", *hMode)
  132. global.InitNodeSetting(*hMode)
  133. paraArgs := os.Args[2:]
  134. switch len(paraArgs) {
  135. case 2:
  136. //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", *hMode)
  137. auto.KillToNodes([]string{"all"})
  138. return
  139. case 3:
  140. if paraArgs[2] == "all" {
  141. //fmt.Printf("发送至所有节点\n")
  142. auto.KillToNodes([]string{"all"})
  143. return
  144. }
  145. nodeIds := strings.Split(paraArgs[2], ",")
  146. //fmt.Printf("发送至指定节点: %s\n", nodeIds)
  147. auto.KillToNodes(nodeIds)
  148. return
  149. }
  150. } else {
  151. fmt.Println("使用默认配置文件参数发送")
  152. global.InitNodeSetting("./config.toml")
  153. auto.KillToNodes([]string{"all"})
  154. }
  155. }
  156. func paraSend() {
  157. allCmd := flag.NewFlagSet("send", flag.ExitOnError)
  158. hMode := allCmd.String("s", "", "指定配置文件")
  159. // 如果只有 "send" 命令,没有其他参数
  160. if len(os.Args) == 2 {
  161. //fmt.Println("使用默认配置文件参数发送至全部节点")
  162. global.InitNodeSetting("./config.toml")
  163. auto.SendToNodes([]string{"all"}, "", "")
  164. return
  165. }
  166. // 解析 all 命令的参数
  167. allCmd.Parse(os.Args[2:])
  168. if *hMode != "" {
  169. //fmt.Printf("指定配置文件参数发送: %s\n", *hMode)
  170. global.InitNodeSetting(*hMode)
  171. paraArgs := os.Args[2:]
  172. switch len(paraArgs) {
  173. case 2:
  174. //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", *hMode)
  175. auto.SendToNodes([]string{"all"}, "", "")
  176. return
  177. case 3:
  178. if paraArgs[2] == "all" {
  179. //fmt.Printf("发送至所有节点\n")
  180. auto.SendToNodes([]string{"all"}, "", "")
  181. return
  182. }
  183. nodeIds := strings.Split(paraArgs[2], ",")
  184. //fmt.Printf("发送至指定节点: %s\n", nodeIds)
  185. auto.SendToNodes(nodeIds, "", "")
  186. return
  187. case 4:
  188. if paraArgs[2] == "all" {
  189. //fmt.Printf("发送至所有节点\n")
  190. auto.SendToNodes([]string{"all"}, paraArgs[3], "")
  191. return
  192. }
  193. nodeIds := strings.Split(paraArgs[2], ",")
  194. //fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  195. auto.SendToNodes(nodeIds, paraArgs[3], "")
  196. case 5:
  197. if paraArgs[2] == "all" {
  198. //fmt.Printf("发送至所有节点\n")
  199. auto.SendToNodes([]string{"all"}, paraArgs[3], paraArgs[4])
  200. return
  201. }
  202. nodeIds := strings.Split(paraArgs[2], ",")
  203. // fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  204. auto.SendToNodes(nodeIds, paraArgs[3], paraArgs[4])
  205. }
  206. } else {
  207. //fmt.Println("使用默认配置文件参数发送")
  208. paraArgs := os.Args[2:]
  209. global.InitNodeSetting("./config.toml")
  210. switch len(paraArgs) {
  211. case 1:
  212. nodeIds := strings.Split(paraArgs[0], ",")
  213. fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  214. auto.SendToNodes(nodeIds, "", "")
  215. case 2:
  216. nodeIds := strings.Split(paraArgs[0], ",")
  217. fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  218. auto.SendToNodes(nodeIds, paraArgs[1], "")
  219. case 3:
  220. nodeIds := strings.Split(paraArgs[0], ",")
  221. fmt.Printf("指定配置文件参数发送至所有节点: %s\n", nodeIds)
  222. auto.SendToNodes(nodeIds, paraArgs[1], paraArgs[2])
  223. }
  224. }
  225. }
  226. func paraStart() {
  227. allCmd := flag.NewFlagSet("start", flag.ExitOnError)
  228. hMode := allCmd.String("s", "", "指定配置文件")
  229. // 如果只有 "start" 命令,没有其他参数
  230. if len(os.Args) == 2 {
  231. fmt.Println("使用默认配置文件")
  232. global.InitNodeSetting("./config.toml")
  233. auto.StartAll()
  234. return
  235. }
  236. // 解析 all 命令的参数
  237. allCmd.Parse(os.Args[2:])
  238. if *hMode != "" {
  239. fmt.Printf("指定配置文件: %s\n", *hMode)
  240. global.InitNodeSetting(*hMode)
  241. auto.StartAll()
  242. } else {
  243. fmt.Println("使用默认配置文件")
  244. global.InitNodeSetting("./config.toml")
  245. auto.StartAll()
  246. }
  247. }