template.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. COPY {{.GoRelPath}}/etc /app/etc
  20. 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. COPY --from=builder /app/etc /app/etc
  27. CMD ["./{{.ExeFile}}"{{.Argument}}]
  28. `
  29. )
  30. func Clean() error {
  31. return util.Clean(category)
  32. }
  33. func GenTemplates(_ *cli.Context) error {
  34. return initTemplate()
  35. }
  36. func Category() string {
  37. return category
  38. }
  39. func RevertTemplate(name string) error {
  40. return util.CreateTemplate(category, name, dockerTemplate)
  41. }
  42. func Update() error {
  43. err := Clean()
  44. if err != nil {
  45. return err
  46. }
  47. return initTemplate()
  48. }
  49. func initTemplate() error {
  50. return util.InitTemplates(category, map[string]string{
  51. dockerTemplateFile: dockerTemplate,
  52. })
  53. }