dockerfile.go 900 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package gen
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "text/template"
  6. "github.com/tal-tech/go-zero/tools/goctl/util"
  7. "github.com/tal-tech/go-zero/tools/goctl/vars"
  8. )
  9. func GenerateDockerfile(goFile string, args ...string) error {
  10. projPath, err := getFilePath(filepath.Dir(goFile))
  11. if err != nil {
  12. return err
  13. }
  14. pos := strings.IndexByte(projPath, '/')
  15. if pos >= 0 {
  16. projPath = projPath[pos+1:]
  17. }
  18. out, err := util.CreateIfNotExist("Dockerfile")
  19. if err != nil {
  20. return err
  21. }
  22. defer out.Close()
  23. var builder strings.Builder
  24. for _, arg := range args {
  25. builder.WriteString(`, "` + arg + `"`)
  26. }
  27. t := template.Must(template.New("dockerfile").Parse(dockerTemplate))
  28. return t.Execute(out, map[string]string{
  29. "projectName": vars.ProjectName,
  30. "goRelPath": projPath,
  31. "goFile": goFile,
  32. "exeFile": util.FileNameWithoutExt(goFile),
  33. "argument": builder.String(),
  34. })
  35. }