main.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "gitea.ckfah.com/go-script/file"
  6. "gitea.ckfah.com/go-script/logger"
  7. "gitea.ckfah.com/go-script/scrpit"
  8. "gitea.ckfah.com/go-script/static"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. var (
  14. gitRemoteAddress string // 克隆的地址
  15. branch string // 克隆的分支
  16. cloneDir string // 克隆的地址
  17. modelName string // go-mod 项目名称
  18. helps bool // 帮助
  19. vendor bool // 是否使用vendor模式
  20. log = logger.NewStdOutLogger("[GO-BUILD]")
  21. )
  22. const (
  23. propertiesFile = "application.properties"
  24. templateName = "go-template"
  25. )
  26. func init() {
  27. flag.StringVar(&modelName, "mod", "go-template", "go mod name, eg:-mod=go-template")
  28. flag.StringVar(&gitRemoteAddress, "git", "git@gitlab.corp.cjjyProject:ebike-urban-op/go-template.git", "git remote addr, eg:-git=git@gitlab.corp.cjjyProject:ebike-urban-op/go-template.git")
  29. flag.StringVar(&branch, "branch", "master", "git branch, eg:-branch=master")
  30. flag.StringVar(&cloneDir, "dir", "", "git clone dir if not set default go mod project name, eg:-dir=/data/temp")
  31. flag.BoolVar(&vendor, "vendor", false, "use go vendor mod, default disable")
  32. flag.BoolVar(&helps, "h", false, "this help")
  33. }
  34. func main() {
  35. // 初始化脚本命令
  36. initFlag()
  37. if cloneDir == "" {
  38. cloneDir = "./" + modelName
  39. }
  40. dir, err := filepath.Abs(cloneDir)
  41. if err != nil {
  42. log.Fatalf("get clone path err, err: %v", err)
  43. }
  44. log.Infof("clone git file into path: %s", dir)
  45. defer func() {
  46. if err := recover(); err != nil {
  47. if err == logger.ExitError {
  48. scrpit.Delete(dir)
  49. }
  50. }
  51. }()
  52. // 克隆git
  53. cloneGit(dir)
  54. // 重命名项目的一些内容
  55. err = file.NewTemplate(dir, templateName, modelName)
  56. if err != nil {
  57. log.Fatalf("build template err, err: %v", err)
  58. }
  59. log.Infof("build template success")
  60. // 删除模版无效文件
  61. deleteTemplateFile(dir)
  62. // copy 配置文件
  63. initTemplateConfig(dir)
  64. // 处理vendor模式
  65. handlerOpenVendor(dir)
  66. // 处理readme
  67. scrpit.Mv(join(dir, "README_project.md"), join(dir, "README.md"))
  68. log.Infof("init project README.md success!")
  69. // 处理预设定配置文件
  70. err = writeProperties(dir)
  71. if err != nil {
  72. log.Fatalf("copy properties find err, err: %v", err)
  73. }
  74. log.Infof(`初始化 git hook 中..... 如果失败请进入项目手动执行!!! git init && curl -O https://tyut.oss-cn-beijing.aliyuncs.com/static/shell/new-githook.shell && bash new-githook.shell && rm new-githook.shell`)
  75. str := `cd %s && git init && curl -O https://tyut.oss-cn-beijing.aliyuncs.com/static/shell/new-githook.shell && bash new-githook.shell && rm new-githook.shell`
  76. shell := fmt.Sprintf(str, dir)
  77. scrpit.Run(shell)
  78. log.Infof(`你现在可以进入目录执行, make , 然后请求 curl -H "Content-type: application/json" -X POST -d '{"params":{"id":1}}' http://localhost:13058/report/info`)
  79. log.Infof(`初始化项目成功,谢谢使用!!!!`)
  80. }
  81. func initTemplateConfig(dir string) {
  82. scrpit.Copy(join(dir, "config/env.dev.ini"), join(dir, "config/env.ini"))
  83. scrpit.Copy(join(dir, "config/apollo/apollo.dev.json"), join(dir, "config/apollo/apollo.json"))
  84. log.Infof("copy config success !")
  85. }
  86. func deleteTemplateFile(dir string) {
  87. scrpit.Delete(join(dir, ".git"))
  88. log.Infof("delete .git file success")
  89. }
  90. func cloneGit(dir string) {
  91. log.Infof("git clone %s to %s\n", gitRemoteAddress, dir)
  92. if branch == "master" {
  93. scrpit.Git(gitRemoteAddress, dir)
  94. } else {
  95. scrpit.GitBranch(branch, gitRemoteAddress, dir)
  96. }
  97. log.Infof("git clone -b %s %s %s success", branch, gitRemoteAddress, dir)
  98. }
  99. func handlerOpenVendor(dir string) {
  100. if vendor {
  101. scrpit.Copy(join(dir, ".gitignore_vendor"), join(dir, ".gitignore"))
  102. scrpit.Copy(join(dir, "Makefile_vendor"), join(dir, "Makefile"))
  103. log.Infof("copy make file and .gitignore success because enable go mod vendor")
  104. }
  105. scrpit.Delete(join(dir, "Makefile_vendor"))
  106. scrpit.Delete(join(dir, ".gitignore_vendor"))
  107. log.Infof("handler vendor handler success!")
  108. }
  109. func writeProperties(dir string) error {
  110. properties, err := static.Asset("static/file/application.properties")
  111. if err != nil {
  112. return err
  113. }
  114. newProperties := strings.ReplaceAll(string(properties), templateName, modelName)
  115. propertiesFilePath := join(dir, propertiesFile)
  116. propertiesFile, err := os.OpenFile(propertiesFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(0644))
  117. if err != nil {
  118. panic(err)
  119. }
  120. defer propertiesFile.Close()
  121. _, err = propertiesFile.Write([]byte(newProperties))
  122. if err != nil {
  123. return err
  124. }
  125. log.Infof("copy application.properties success,使用 nacos/apollo 需要使用这个配置文件 %s, 复制到配置中心去!", propertiesFilePath)
  126. return nil
  127. }
  128. func echoLine() {
  129. fmt.Println()
  130. }
  131. func join(dir, file string) string {
  132. return filepath.Clean(fmt.Sprintf("%s%s%s", dir, string(os.PathSeparator), file))
  133. }
  134. func initFlag() {
  135. flag.Parse()
  136. if helps {
  137. printHelp()
  138. os.Exit(-1)
  139. }
  140. if modelName == "" {
  141. log.Infof("please set -mod=your_project_name")
  142. printHelp()
  143. os.Exit(-1)
  144. }
  145. log.Infof("Init flag success")
  146. }
  147. func printHelp() {
  148. log.Infof(`\n================go-build help=====================
  149. go-build -dir=/data/temp -mod=go-template -git=git@gitlab.corp.cjjyProject:ebike-urban-op/go-template.git -branch=gauss -vendor=true
  150. Option:
  151. 构建master分支(例如:项目本地地址/data/ebike-op-demo,项目名称为:ebike-op-demo):
  152. go-build -dir=/data/ebike-op-demo -mod=ebike-op-demo
  153. 构建gauss分支:
  154. go-build -dir=/data/temp -mod=go-template -branch=gauss
  155. 构建vendor项目:
  156. go-build -dir=/data/temp -mod=go-template -vendor=true
  157. `)
  158. flag.PrintDefaults()
  159. }