2020-07-29 17:11:41 +08:00
|
|
|
package gogen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2020-08-08 16:40:10 +08:00
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
2020-11-24 15:11:18 +08:00
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/config"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
2020-07-29 17:11:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultPort = 8888
|
|
|
|
etcDir = "etc"
|
2020-08-27 11:44:35 +08:00
|
|
|
etcTemplate = `Name: {{.serviceName}}
|
|
|
|
Host: {{.host}}
|
|
|
|
Port: {{.port}}
|
|
|
|
`
|
2020-07-29 17:11:41 +08:00
|
|
|
)
|
|
|
|
|
2020-11-24 15:11:18 +08:00
|
|
|
func genEtc(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
|
|
|
filename, err := format.FileNamingFormat(cfg.NamingFormat, api.Service.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:11:41 +08:00
|
|
|
service := api.Service
|
2020-11-13 23:01:19 +08:00
|
|
|
host, ok := util.GetAnnotationValue(service.Groups[0].Annotations, "server", "host")
|
2020-07-29 17:11:41 +08:00
|
|
|
if !ok {
|
|
|
|
host = "0.0.0.0"
|
|
|
|
}
|
2020-11-13 23:01:19 +08:00
|
|
|
port, ok := util.GetAnnotationValue(service.Groups[0].Annotations, "server", "port")
|
2020-07-29 17:11:41 +08:00
|
|
|
if !ok {
|
|
|
|
port = strconv.Itoa(defaultPort)
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:17:23 +08:00
|
|
|
return genFile(fileGenConfig{
|
|
|
|
dir: dir,
|
|
|
|
subdir: etcDir,
|
|
|
|
filename: fmt.Sprintf("%s.yaml", filename),
|
|
|
|
templateName: "etcTemplate",
|
|
|
|
category: category,
|
|
|
|
templateFile: etcTemplateFile,
|
|
|
|
builtinTemplate: etcTemplate,
|
|
|
|
data: map[string]string{
|
|
|
|
"serviceName": service.Name,
|
|
|
|
"host": host,
|
|
|
|
"port": port,
|
|
|
|
},
|
2020-07-29 17:11:41 +08:00
|
|
|
})
|
|
|
|
}
|