docker.go 2.8 KB

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