/commands.go

https://github.com/yezihack/go-mygen · Go · 184 lines · 154 code · 16 blank · 14 comment · 32 complexity · f9ca32830a5dcac65a9f2183dca75bdc MD5 · raw file

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type commands struct {
  11. l *Logic
  12. }
  13. func NewCommands(logic *Logic) *commands {
  14. return &commands{
  15. l: logic,
  16. }
  17. }
  18. //映射相应的命令
  19. func (c *commands) Handlers() map[string]func(args []string) int {
  20. return map[string]func(args []string) int{
  21. "0": c.CustomDir,
  22. "1": c.MarkDown,
  23. "2": c.GenerateEntry,
  24. "3": c.GenerateCURD,
  25. "4": c.CustomFormat,
  26. "5": c.ShowTableList,
  27. "7": c.Clean,
  28. "clear": c.Clean,
  29. "c": c.Clean,
  30. "8": c.Help,
  31. "h": c.Help,
  32. "help": c.Help,
  33. "ll": c.Help,
  34. "ls": c.Help,
  35. "quit": c.Quit,
  36. "q": c.Quit,
  37. "exit": c.Quit,
  38. }
  39. }
  40. //生成数据库表的markdown文档
  41. func (c *commands) MarkDown(args []string) int {
  42. fmt.Println("Preparing to generate the markdown document...")
  43. //检查目录是否存在
  44. CreateDir(c.l.Path)
  45. err := c.l.CreateMarkdown()
  46. if err != nil {
  47. log.Println("MarkDown>>", err)
  48. }
  49. return 0
  50. }
  51. //help list
  52. func (c *commands) Help(args []string) int {
  53. for _, row := range CmdHelp {
  54. s := fmt.Sprintf("%s %s\n", "NO:"+row.No, row.Msg)
  55. fmt.Print(s)
  56. }
  57. return 0
  58. }
  59. //生成golang表对应的结构实体
  60. func (c *commands) GenerateEntry(args []string) int {
  61. fmt.Print("Do you need to set the format of the structure?(Yes|No)>")
  62. line, _, _ := bufio.NewReader(os.Stdin).ReadLine()
  63. switch strings.ToLower(string(line)) {
  64. case "yes", "y":
  65. formats = c._setFormat()
  66. }
  67. err := c.l.CreateEntity(formats)
  68. if err != nil {
  69. log.Println("GenerateEntry>>", err.Error())
  70. }
  71. go Gofmt(GetExeRootDir())
  72. return 0
  73. }
  74. //还可以自定义结构体解析实体,如json,gorm,xml
  75. func (c *commands) CustomFormat(args []string) int {
  76. formats = c._setFormat()
  77. return 0
  78. }
  79. //生成golang操作mysql的CRUD增删改查语句
  80. func (c *commands) GenerateCURD(args []string) int {
  81. err := c.l.CreateCURD(formats)
  82. if err != nil {
  83. log.Println("GenerateCURD>>", err.Error())
  84. }
  85. go Gofmt(GetExeRootDir())
  86. return 0
  87. }
  88. //自定义生成目录
  89. func (c *commands) CustomDir(args []string) int {
  90. fmt.Print("Please set the build directory>")
  91. line, _, _ := bufio.NewReader(os.Stdin).ReadLine()
  92. if string(line) != "" {
  93. path, err := c.l.T.GenerateDir(string(line))
  94. if err == nil {
  95. c.l.Path = path
  96. fmt.Println("Directory success:", path)
  97. } else {
  98. log.Println("Set directory failed>>", err)
  99. }
  100. }
  101. return 0
  102. }
  103. //显示所有的表名
  104. func (c *commands) ShowTableList(args []string) int {
  105. if len(c.l.DB.Tables) == 0 {
  106. fmt.Println("Whoops, Nothing at all!!!")
  107. return 0
  108. }
  109. c._showTableList(c.l.DB.Tables)
  110. fmt.Print("Select the table sequence number you need?(By default all, comma separated,all represents all)>")
  111. line, _, _ := bufio.NewReader(os.Stdin).ReadLine()
  112. if !strings.EqualFold(string(line), "") {
  113. c.l.DB.DoTables = c._filterTables(string(line), c.l.DB.Tables)
  114. }
  115. return 0
  116. }
  117. //清屏
  118. func (c *commands) Clean(args []string) int {
  119. Clean()
  120. return 0
  121. }
  122. //退出
  123. func (c *commands) Quit(args []string) int {
  124. return 1
  125. }
  126. //过滤表名
  127. func (c *commands) _filterTables(ids string, tables []TableNameAndComment) []TableNameAndComment {
  128. lst := strings.Split(ids, ",")
  129. result := make([]TableNameAndComment, 0)
  130. if strings.ToLower(ids) == "all" {
  131. return tables
  132. }
  133. for _, id := range lst {
  134. id = strings.TrimSpace(id)
  135. for _, t := range tables {
  136. if strconv.Itoa(t.Index) == id || id == t.Name {
  137. result = append(result, t)
  138. }
  139. }
  140. }
  141. return result
  142. }
  143. //显示所有名视图
  144. func (c *commands) _showTableList(NameAndComment []TableNameAndComment) {
  145. for idx, table := range NameAndComment {
  146. idx++
  147. info := fmt.Sprintf("%s:%s", strconv.Itoa(idx), table.Name)
  148. if table.Comment != "" {
  149. info += fmt.Sprintf("(%s)", table.Comment)
  150. }
  151. fmt.Println(info)
  152. }
  153. fmt.Println("Total " + strconv.Itoa(len(NameAndComment)) + " tables\n")
  154. }
  155. //set struct format
  156. func (c *commands) _setFormat() []string {
  157. fmt.Print("Set the mapping name of the structure, separated by a comma (example :json,gorm)>")
  158. input, _, _ := bufio.NewReader(os.Stdin).ReadLine()
  159. if string(input) != "" {
  160. formatList := CheckCharDoSpecialArr(string(input), ',', `[\w\,\-]+`)
  161. if len(formatList) > 0 {
  162. fmt.Printf("Set format success: %v\n", formatList)
  163. return formatList
  164. }
  165. }
  166. fmt.Println("Set failed")
  167. return nil
  168. }