template.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package docker
  2. import (
  3. "github.com/tal-tech/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{{end}}
  14. WORKDIR /build/zero
  15. ADD go.mod .
  16. ADD go.sum .
  17. RUN go mod download
  18. COPY . .
  19. {{if .HasArgs}}COPY {{.GoRelPath}}/etc /app/etc
  20. {{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
  21. FROM alpine
  22. RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
  23. ENV TZ Asia/Shanghai
  24. WORKDIR /app
  25. COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}
  26. {{if .HasArgs}}COPY --from=builder /app/etc /app/etc{{end}}
  27. {{if .HasPort}}
  28. EXPOSE {{.Port}}
  29. {{end}}
  30. CMD ["./{{.ExeFile}}"{{.Argument}}]
  31. `
  32. )
  33. func Clean() error {
  34. return util.Clean(category)
  35. }
  36. func GenTemplates(_ *cli.Context) error {
  37. return initTemplate()
  38. }
  39. func Category() string {
  40. return category
  41. }
  42. func RevertTemplate(name string) error {
  43. return util.CreateTemplate(category, name, dockerTemplate)
  44. }
  45. func Update() error {
  46. err := Clean()
  47. if err != nil {
  48. return err
  49. }
  50. return initTemplate()
  51. }
  52. func initTemplate() error {
  53. return util.InitTemplates(category, map[string]string{
  54. dockerTemplateFile: dockerTemplate,
  55. })
  56. }