template.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package docker
  2. import (
  3. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  4. "github.com/urfave/cli"
  5. )
  6. const (
  7. category = "docker"
  8. dockerTemplateFile = "docker.tpl"
  9. dockerTemplate = `FROM golang:alpine AS builder
  10. LABEL stage=gobuilder
  11. ENV CGO_ENABLED 0
  12. ENV GOOS linux
  13. {{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct
  14. {{end}}
  15. WORKDIR /build/zero
  16. ADD go.mod .
  17. ADD go.sum .
  18. RUN go mod download
  19. COPY . .
  20. {{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc
  21. {{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
  22. FROM alpine
  23. RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
  24. ENV TZ Asia/Shanghai
  25. WORKDIR /app
  26. COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}}
  27. COPY --from=builder /app/etc /app/etc{{end}}
  28. {{if .HasPort}}
  29. EXPOSE {{.Port}}
  30. {{end}}
  31. CMD ["./{{.ExeFile}}"{{.Argument}}]
  32. `
  33. )
  34. // Clean deletes all templates files
  35. func Clean() error {
  36. return util.Clean(category)
  37. }
  38. // GenTemplates creates docker template files
  39. func GenTemplates(_ *cli.Context) error {
  40. return initTemplate()
  41. }
  42. // Category returns the const string of docker category
  43. func Category() string {
  44. return category
  45. }
  46. // RevertTemplate recovers the deleted template files
  47. func RevertTemplate(name string) error {
  48. return util.CreateTemplate(category, name, dockerTemplate)
  49. }
  50. // Update deletes and creates new template files
  51. func Update() error {
  52. err := Clean()
  53. if err != nil {
  54. return err
  55. }
  56. return initTemplate()
  57. }
  58. func initTemplate() error {
  59. return util.InitTemplates(category, map[string]string{
  60. dockerTemplateFile: dockerTemplate,
  61. })
  62. }