docker.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package docker
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "text/template"
  8. "time"
  9. "github.com/tal-tech/go-zero/tools/goctl/util"
  10. ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
  11. "github.com/urfave/cli"
  12. )
  13. const (
  14. etcDir = "etc"
  15. yamlEtx = ".yaml"
  16. cstOffset = 60 * 60 * 8 // 8 hours offset for Chinese Standard Time
  17. )
  18. type Docker struct {
  19. Chinese bool
  20. GoRelPath string
  21. GoFile string
  22. ExeFile string
  23. Argument string
  24. }
  25. func DockerCommand(c *cli.Context) error {
  26. goFile := c.String("go")
  27. if len(goFile) == 0 {
  28. return errors.New("-go can't be empty")
  29. }
  30. cfg, err := findConfig(goFile, etcDir)
  31. if err != nil {
  32. return err
  33. }
  34. return generateDockerfile(goFile, "-f", "etc/"+cfg)
  35. }
  36. func findConfig(file, dir string) (string, error) {
  37. var files []string
  38. err := filepath.Walk(dir, func(path string, f os.FileInfo, _ error) error {
  39. if !f.IsDir() {
  40. if filepath.Ext(f.Name()) == yamlEtx {
  41. files = append(files, f.Name())
  42. }
  43. }
  44. return nil
  45. })
  46. if err != nil {
  47. return "", err
  48. }
  49. if len(files) == 0 {
  50. return "", errors.New("no yaml file")
  51. }
  52. name := strings.TrimSuffix(filepath.Base(file), ".go")
  53. for _, f := range files {
  54. if strings.Index(f, name) == 0 {
  55. return f, nil
  56. }
  57. }
  58. return files[0], nil
  59. }
  60. func generateDockerfile(goFile string, args ...string) error {
  61. projPath, err := getFilePath(filepath.Dir(goFile))
  62. if err != nil {
  63. return err
  64. }
  65. pos := strings.IndexByte(projPath, '/')
  66. if pos >= 0 {
  67. projPath = projPath[pos+1:]
  68. }
  69. out, err := util.CreateIfNotExist("Dockerfile")
  70. if err != nil {
  71. return err
  72. }
  73. defer out.Close()
  74. text, err := ctlutil.LoadTemplate(category, dockerTemplateFile, dockerTemplate)
  75. if err != nil {
  76. return err
  77. }
  78. var builder strings.Builder
  79. for _, arg := range args {
  80. builder.WriteString(`, "` + arg + `"`)
  81. }
  82. _, offset := time.Now().Zone()
  83. t := template.Must(template.New("dockerfile").Parse(text))
  84. return t.Execute(out, Docker{
  85. Chinese: offset == cstOffset,
  86. GoRelPath: projPath,
  87. GoFile: goFile,
  88. ExeFile: util.FileNameWithoutExt(filepath.Base(goFile)),
  89. Argument: builder.String(),
  90. })
  91. }
  92. func getFilePath(file string) (string, error) {
  93. wd, err := os.Getwd()
  94. if err != nil {
  95. return "", err
  96. }
  97. projPath, ok := util.FindGoModPath(filepath.Join(wd, file))
  98. if !ok {
  99. projPath, err = util.PathFromGoSrc()
  100. if err != nil {
  101. return "", errors.New("no go.mod found, or not in GOPATH")
  102. }
  103. }
  104. return projPath, nil
  105. }