dockerfile.go 824 B

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