util.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package gogen
  2. import (
  3. "fmt"
  4. goformat "go/format"
  5. "io"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "strings"
  10. "github.com/tal-tech/go-zero/core/collection"
  11. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  12. "github.com/tal-tech/go-zero/tools/goctl/api/util"
  13. goctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
  14. )
  15. const goModeIdentifier = "go.mod"
  16. func getParentPackage(dir string) (string, error) {
  17. absDir, err := filepath.Abs(dir)
  18. if err != nil {
  19. return "", err
  20. }
  21. absDir = strings.ReplaceAll(absDir, `\`, `/`)
  22. var rootPath string
  23. var tempPath = absDir
  24. var hasGoMod = false
  25. for {
  26. if goctlutil.FileExists(filepath.Join(tempPath, goModeIdentifier)) {
  27. tempPath = filepath.Dir(tempPath)
  28. rootPath = absDir[len(tempPath)+1:]
  29. hasGoMod = true
  30. break
  31. }
  32. if tempPath == filepath.Dir(tempPath) {
  33. break
  34. }
  35. tempPath = filepath.Dir(tempPath)
  36. if tempPath == string(filepath.Separator) {
  37. break
  38. }
  39. }
  40. if hasGoMod {
  41. return rootPath, nil
  42. }
  43. gopath := os.Getenv("GOPATH")
  44. parent := path.Join(gopath, "src")
  45. pos := strings.Index(absDir, parent)
  46. if pos < 0 {
  47. fmt.Printf("%s not in go.mod project path, or not in GOPATH of %s directory\n", absDir, gopath)
  48. tempPath = filepath.Dir(absDir)
  49. rootPath = absDir[len(tempPath)+1:]
  50. } else {
  51. rootPath = absDir[len(parent)+1:]
  52. }
  53. return rootPath, nil
  54. }
  55. func writeIndent(writer io.Writer, indent int) {
  56. for i := 0; i < indent; i++ {
  57. fmt.Fprint(writer, "\t")
  58. }
  59. }
  60. func writeProperty(writer io.Writer, name, tp, tag, comment string, indent int) error {
  61. writeIndent(writer, indent)
  62. var err error
  63. if len(comment) > 0 {
  64. comment = strings.TrimPrefix(comment, "//")
  65. comment = "//" + comment
  66. _, err = fmt.Fprintf(writer, "%s %s %s %s\n", strings.Title(name), tp, tag, comment)
  67. } else {
  68. _, err = fmt.Fprintf(writer, "%s %s %s\n", strings.Title(name), tp, tag)
  69. }
  70. return err
  71. }
  72. func getAuths(api *spec.ApiSpec) []string {
  73. var authNames = collection.NewSet()
  74. for _, g := range api.Service.Groups {
  75. if value, ok := util.GetAnnotationValue(g.Annotations, "server", "jwt"); ok {
  76. authNames.Add(value)
  77. }
  78. if value, ok := util.GetAnnotationValue(g.Annotations, "server", "signature"); ok {
  79. authNames.Add(value)
  80. }
  81. }
  82. return authNames.KeysStr()
  83. }
  84. func formatCode(code string) string {
  85. ret, err := goformat.Source([]byte(code))
  86. if err != nil {
  87. return code
  88. }
  89. return string(ret)
  90. }