You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/gogen/genmain.go

54 lines
1.6 KiB
Go

4 years ago
package gogen
import (
_ "embed"
4 years ago
"fmt"
"strings"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/zeromicro/go-zero/tools/goctl/vars"
4 years ago
)
//go:embed main.tpl
var mainTemplate string
4 years ago
func genMain(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
4 years ago
name := strings.ToLower(api.Service.Name)
filename, err := format.FileNamingFormat(cfg.NamingFormat, name)
if err != nil {
return err
}
configName := filename
if strings.HasSuffix(filename, "-api") {
filename = strings.ReplaceAll(filename, "-api", "")
}
return genFile(fileGenConfig{
dir: dir,
subdir: "",
filename: filename + ".go",
templateName: "mainTemplate",
category: category,
templateFile: mainTemplateFile,
builtinTemplate: mainTemplate,
data: map[string]string{
"importPackages": genMainImports(rootPkg),
"serviceName": configName,
},
4 years ago
})
}
func genMainImports(parentPkg string) string {
var imports []string
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, configDir)))
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, handlerDir)))
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, contextDir)))
imports = append(imports, fmt.Sprintf("\"%s/core/conf\"", vars.ProjectOpenSourceURL))
imports = append(imports, fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL))
4 years ago
return strings.Join(imports, "\n\t")
}