-
Notifications
You must be signed in to change notification settings - Fork 0
/
genconfig.go
65 lines (56 loc) · 1.44 KB
/
genconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package gogen
import (
_ "embed"
"fmt"
"strings"
"github.com/wangliujing/goctl/api/spec"
"github.com/wangliujing/goctl/config"
"github.com/wangliujing/goctl/util/format"
"github.com/wangliujing/goctl/vars"
)
const (
configFile = "config"
jwtTemplate = ` struct {
AccessSecret string
AccessExpire int64
}
`
jwtTransTemplate = ` struct {
Secret string
PrevSecret string
}
`
)
//go:embed config.tpl
var configTemplate string
func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
filename, err := format.FileNamingFormat(cfg.NamingFormat, configFile)
if err != nil {
return err
}
authNames := getAuths(api)
var auths []string
for _, item := range authNames {
auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
}
jwtTransNames := getJwtTrans(api)
var jwtTransList []string
for _, item := range jwtTransNames {
jwtTransList = append(jwtTransList, fmt.Sprintf("%s %s", item, jwtTransTemplate))
}
authImportStr := fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL)
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"),
"jwtTrans": strings.Join(jwtTransList, "\n"),
},
})
}