| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package main
- import (
- "flag"
- "fmt"
- "gitea.ckfah.com/go-script/file"
- "gitea.ckfah.com/go-script/logger"
- "gitea.ckfah.com/go-script/scrpit"
- "gitea.ckfah.com/go-script/static"
- "os"
- "path/filepath"
- "strings"
- )
- var (
- gitRemoteAddress string // 克隆的地址
- branch string // 克隆的分支
- cloneDir string // 克隆的地址
- modelName string // go-mod 项目名称
- helps bool // 帮助
- vendor bool // 是否使用vendor模式
- log = logger.NewStdOutLogger("[GO-BUILD]")
- )
- const (
- propertiesFile = "application.properties"
- templateName = "go-template"
- )
- func init() {
- flag.StringVar(&modelName, "mod", "go-template", "go mod name, eg:-mod=go-template")
- 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")
- flag.StringVar(&branch, "branch", "master", "git branch, eg:-branch=master")
- flag.StringVar(&cloneDir, "dir", "", "git clone dir if not set default go mod project name, eg:-dir=/data/temp")
- flag.BoolVar(&vendor, "vendor", false, "use go vendor mod, default disable")
- flag.BoolVar(&helps, "h", false, "this help")
- }
- func main() {
- // 初始化脚本命令
- initFlag()
- if cloneDir == "" {
- cloneDir = "./" + modelName
- }
- dir, err := filepath.Abs(cloneDir)
- if err != nil {
- log.Fatalf("get clone path err, err: %v", err)
- }
- log.Infof("clone git file into path: %s", dir)
- defer func() {
- if err := recover(); err != nil {
- if err == logger.ExitError {
- scrpit.Delete(dir)
- }
- }
- }()
- // 克隆git
- cloneGit(dir)
- // 重命名项目的一些内容
- err = file.NewTemplate(dir, templateName, modelName)
- if err != nil {
- log.Fatalf("build template err, err: %v", err)
- }
- log.Infof("build template success")
- // 删除模版无效文件
- deleteTemplateFile(dir)
- // copy 配置文件
- initTemplateConfig(dir)
- // 处理vendor模式
- handlerOpenVendor(dir)
- // 处理readme
- scrpit.Mv(join(dir, "README_project.md"), join(dir, "README.md"))
- log.Infof("init project README.md success!")
- // 处理预设定配置文件
- err = writeProperties(dir)
- if err != nil {
- log.Fatalf("copy properties find err, err: %v", err)
- }
- 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`)
- 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`
- shell := fmt.Sprintf(str, dir)
- scrpit.Run(shell)
- log.Infof(`你现在可以进入目录执行, make , 然后请求 curl -H "Content-type: application/json" -X POST -d '{"params":{"id":1}}' http://localhost:13058/report/info`)
- log.Infof(`初始化项目成功,谢谢使用!!!!`)
- }
- func initTemplateConfig(dir string) {
- scrpit.Copy(join(dir, "config/env.dev.ini"), join(dir, "config/env.ini"))
- scrpit.Copy(join(dir, "config/apollo/apollo.dev.json"), join(dir, "config/apollo/apollo.json"))
- log.Infof("copy config success !")
- }
- func deleteTemplateFile(dir string) {
- scrpit.Delete(join(dir, ".git"))
- log.Infof("delete .git file success")
- }
- func cloneGit(dir string) {
- log.Infof("git clone %s to %s\n", gitRemoteAddress, dir)
- if branch == "master" {
- scrpit.Git(gitRemoteAddress, dir)
- } else {
- scrpit.GitBranch(branch, gitRemoteAddress, dir)
- }
- log.Infof("git clone -b %s %s %s success", branch, gitRemoteAddress, dir)
- }
- func handlerOpenVendor(dir string) {
- if vendor {
- scrpit.Copy(join(dir, ".gitignore_vendor"), join(dir, ".gitignore"))
- scrpit.Copy(join(dir, "Makefile_vendor"), join(dir, "Makefile"))
- log.Infof("copy make file and .gitignore success because enable go mod vendor")
- }
- scrpit.Delete(join(dir, "Makefile_vendor"))
- scrpit.Delete(join(dir, ".gitignore_vendor"))
- log.Infof("handler vendor handler success!")
- }
- func writeProperties(dir string) error {
- properties, err := static.Asset("static/file/application.properties")
- if err != nil {
- return err
- }
- newProperties := strings.ReplaceAll(string(properties), templateName, modelName)
- propertiesFilePath := join(dir, propertiesFile)
- propertiesFile, err := os.OpenFile(propertiesFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(0644))
- if err != nil {
- panic(err)
- }
- defer propertiesFile.Close()
- _, err = propertiesFile.Write([]byte(newProperties))
- if err != nil {
- return err
- }
- log.Infof("copy application.properties success,使用 nacos/apollo 需要使用这个配置文件 %s, 复制到配置中心去!", propertiesFilePath)
- return nil
- }
- func echoLine() {
- fmt.Println()
- }
- func join(dir, file string) string {
- return filepath.Clean(fmt.Sprintf("%s%s%s", dir, string(os.PathSeparator), file))
- }
- func initFlag() {
- flag.Parse()
- if helps {
- printHelp()
- os.Exit(-1)
- }
- if modelName == "" {
- log.Infof("please set -mod=your_project_name")
- printHelp()
- os.Exit(-1)
- }
- log.Infof("Init flag success")
- }
- func printHelp() {
- log.Infof(`\n================go-build help=====================
- go-build -dir=/data/temp -mod=go-template -git=git@gitlab.corp.cjjyProject:ebike-urban-op/go-template.git -branch=gauss -vendor=true
- Option:
- 构建master分支(例如:项目本地地址/data/ebike-op-demo,项目名称为:ebike-op-demo):
- go-build -dir=/data/ebike-op-demo -mod=ebike-op-demo
- 构建gauss分支:
- go-build -dir=/data/temp -mod=go-template -branch=gauss
- 构建vendor项目:
- go-build -dir=/data/temp -mod=go-template -vendor=true
- `)
- flag.PrintDefaults()
- }
|