remove duplicated code in goctl (#369)
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
package gogen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/format"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
@@ -39,38 +35,24 @@ func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
return err
|
||||
}
|
||||
|
||||
fp, created, err := util.MaybeCreateFile(dir, configDir, filename+".go")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !created {
|
||||
return nil
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
var authNames = getAuths(api)
|
||||
var auths []string
|
||||
for _, item := range authNames {
|
||||
auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
|
||||
}
|
||||
|
||||
var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)
|
||||
text, err := ctlutil.LoadTemplate(category, configTemplateFile, configTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.Must(template.New("configTemplate").Parse(text))
|
||||
buffer := new(bytes.Buffer)
|
||||
err = t.Execute(buffer, map[string]string{
|
||||
"authImport": authImportStr,
|
||||
"auth": strings.Join(auths, "\n"),
|
||||
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"),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
formatCode := formatCode(buffer.String())
|
||||
_, err = fp.WriteString(formatCode)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user