forked from zeromicro/go-zero
/
genconfig.go
46 lines (39 loc) · 877 Bytes
/
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
package gogen
import (
"bytes"
"fmt"
"text/template"
"github.com/wjames2000/go-zero/tools/goctl/api/util"
"github.com/wjames2000/go-zero/tools/goctl/vars"
)
const (
configFile = "config.go"
configTemplate = `package config
import {{.authImport}}
type Config struct {
rest.RestConf
}
`
)
func genConfig(dir string) error {
fp, created, err := util.MaybeCreateFile(dir, configDir, configFile)
if err != nil {
return err
}
if !created {
return nil
}
defer fp.Close()
var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)
t := template.Must(template.New("configTemplate").Parse(configTemplate))
buffer := new(bytes.Buffer)
err = t.Execute(buffer, map[string]string{
"authImport": authImportStr,
})
if err != nil {
return nil
}
formatCode := formatCode(buffer.String())
_, err = fp.WriteString(formatCode)
return err
}