cmd.go 4.4 KB

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