go-zero/tools/goctl/api/gogen/genconfig.go

59 lines
1.2 KiB
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package gogen
import (
2020-08-08 15:16:59 +08:00
"fmt"
"strings"
2020-07-29 17:11:41 +08:00
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/util/format"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/tools/goctl/vars"
2020-07-29 17:11:41 +08:00
)
const (
configFile = "config"
2020-07-29 17:11:41 +08:00
configTemplate = `package config
2020-08-27 14:40:05 +08:00
import {{.authImport}}
2020-07-29 17:11:41 +08:00
type Config struct {
2020-07-31 11:45:16 +08:00
rest.RestConf
{{.auth}}
2020-07-29 17:11:41 +08:00
}
`
jwtTemplate = ` struct {
AccessSecret string
AccessExpire int64
}
2020-07-29 17:11:41 +08:00
`
)
func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
filename, err := format.FileNamingFormat(cfg.NamingFormat, configFile)
if err != nil {
return err
}
var authNames = getAuths(api)
var auths []string
for _, item := range authNames {
auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
}
2020-08-08 15:16:59 +08:00
var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)
2020-10-15 16:36:49 +08:00
2021-01-09 00:17:23 +08:00
return genFile(fileGenConfig{
dir: dir,
subdir: configDir,
filename: filename + ".go",
templateName: "configTemplate",
category: category,
templateFile: configTemplateFile,
builtinTemplate: configTemplate,
data: map[string]string{
"authImport": authImportStr,
"auth": strings.Join(auths, "\n"),
},
2020-07-29 17:11:41 +08:00
})
}