genconfig.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package gogen
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  6. "github.com/tal-tech/go-zero/tools/goctl/config"
  7. "github.com/tal-tech/go-zero/tools/goctl/util/format"
  8. "github.com/tal-tech/go-zero/tools/goctl/vars"
  9. )
  10. const (
  11. configFile = "config"
  12. configTemplate = `package config
  13. import {{.authImport}}
  14. type Config struct {
  15. rest.RestConf
  16. {{.auth}}
  17. }
  18. `
  19. jwtTemplate = ` struct {
  20. AccessSecret string
  21. AccessExpire int64
  22. }
  23. `
  24. )
  25. func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
  26. filename, err := format.FileNamingFormat(cfg.NamingFormat, configFile)
  27. if err != nil {
  28. return err
  29. }
  30. var authNames = getAuths(api)
  31. var auths []string
  32. for _, item := range authNames {
  33. auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
  34. }
  35. var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL)
  36. return genFile(fileGenConfig{
  37. dir: dir,
  38. subdir: configDir,
  39. filename: filename + ".go",
  40. templateName: "configTemplate",
  41. category: category,
  42. templateFile: configTemplateFile,
  43. builtinTemplate: configTemplate,
  44. data: map[string]string{
  45. "authImport": authImportStr,
  46. "auth": strings.Join(auths, "\n"),
  47. },
  48. })
  49. }