代码拉取完成,页面将自动刷新
package main
import (
"flag"
"log"
"os"
"os/exec"
"path/filepath"
"text/template"
)
type args struct {
Mod, Pn string
}
func main() {
mod := flag.String("m", "", "go mod name")
build := flag.Bool("b", true, "build after create files")
flag.Parse()
if *mod == "" {
log.Println("must set -m git url")
return
}
pn, err := getProjectName()
if err != nil {
log.Println("getProjectName err:", err)
return
}
createGitIgnore(pn)
createMain(*mod)
createInternal(*mod, pn)
createGoMod(*mod)
createDockerfile(pn)
createReadme()
if *build {
goBuild()
}
}
func getProjectName() (pn string, err error) {
dir, err := os.Getwd()
if err != nil {
return
}
pn = filepath.Base(dir)
return
}
func createGitIgnore(pn string) {
createTemplateFile(".gitignore", gitignore, pn)
}
func createTemplateFile(fileName, temp string, data interface{}) {
file, err := os.Create(fileName)
if err != nil {
log.Println("create", fileName, "err:", err)
return
}
defer file.Close()
if err = template.Must(template.New(fileName).Parse(temp)).Execute(file, data); err != nil {
log.Println(fileName, "template err:", err)
return
}
log.Println(fileName, "create ok")
}
func createMain(mod string) {
createTemplateFile("main.go", mainTemp, &args{Mod: mod})
}
func createInternal(mod, pn string) {
if err := createDir("internal/app"); err == nil {
createTemplateFile("internal/app/app.go", appgo, &args{Mod: mod, Pn: pn})
}
if err := createDir("internal/db"); err == nil {
createTemplateFile("internal/db/db.go", dbgo, nil)
createTemplateFile("internal/db/sqls.go", sqlsgo, nil)
}
if err := createDir("internal/model"); err == nil {
createTemplateFile("internal/model/config.go", configgo, nil)
createTemplateFile("internal/model/const.go", constgo, nil)
createTemplateFile("internal/model/model.go", modelgo, nil)
}
}
func createDir(dirName string) (err error) {
if err = os.MkdirAll(dirName, os.ModePerm); err != nil {
log.Println(dirName, "MkdirAll err:", err)
}
return
}
func createGoMod(mod string) {
if err := exec.Command("go", "mod", "init", mod).Run(); err != nil {
log.Println("go mod init fail, err:", err)
return
}
log.Println("go mod init ok")
if err := exec.Command("go", "mod", "tidy").Run(); err != nil {
log.Println("go mod tidy fail, err:", err)
return
}
log.Println("go mod tidy ok")
}
func createDockerfile(pn string) {
createTemplateFile("Dockerfile", dockerfile, pn)
}
func createReadme() {
createTemplateFile("readme.md", readme, nil)
}
func goBuild() {
if err := exec.Command("go", "build").Run(); err != nil {
log.Println("go build fail, err:", err)
return
}
log.Println("go build ok")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。