go-zero/tools/goctl/docker/template.go

79 lines
1.6 KiB
Go
Raw Normal View History

2020-11-09 18:02:16 +08:00
package docker
2020-07-29 17:11:41 +08:00
2020-11-09 18:02:16 +08:00
import (
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
)
const (
category = "docker"
dockerTemplateFile = "docker.tpl"
dockerTemplate = `FROM golang:alpine AS builder
2020-07-29 17:11:41 +08:00
LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOOS linux
{{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct
{{end}}
2020-11-08 21:28:58 +08:00
WORKDIR /build/zero
ADD go.mod .
ADD go.sum .
RUN go mod download
2020-07-29 17:11:41 +08:00
COPY . .
2020-12-12 16:53:06 +08:00
{{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc
2020-12-11 20:31:31 +08:00
{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
2020-07-29 17:11:41 +08:00
FROM alpine
2020-12-10 16:21:06 +08:00
RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
2020-07-29 17:11:41 +08:00
ENV TZ Asia/Shanghai
WORKDIR /app
2020-12-12 16:53:06 +08:00
COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}}
COPY --from=builder /app/etc /app/etc{{end}}
{{if .HasPort}}
EXPOSE {{.Port}}
2020-12-11 20:31:31 +08:00
{{end}}
CMD ["./{{.ExeFile}}"{{.Argument}}]
2020-07-29 17:11:41 +08:00
`
2020-11-09 18:02:16 +08:00
)
// Clean deletes all templates files
2020-12-11 18:53:40 +08:00
func Clean() error {
return util.Clean(category)
}
// GenTemplates creates docker template files
2020-11-09 18:02:16 +08:00
func GenTemplates(_ *cli.Context) error {
2020-12-11 18:53:40 +08:00
return initTemplate()
}
// Category returns the const string of docker category
2020-12-11 18:53:40 +08:00
func Category() string {
return category
}
// RevertTemplate recovers the deleted template files
2020-12-11 18:53:40 +08:00
func RevertTemplate(name string) error {
return util.CreateTemplate(category, name, dockerTemplate)
}
// Update deletes and creates new template files
2020-12-11 18:53:40 +08:00
func Update() error {
err := Clean()
if err != nil {
return err
}
return initTemplate()
}
func initTemplate() error {
2020-11-09 18:02:16 +08:00
return util.InitTemplates(category, map[string]string{
dockerTemplateFile: dockerTemplate,
})
}