goctl added

This commit is contained in:
kim
2020-07-29 17:11:41 +08:00
parent b1975d29a7
commit 121323b8c3
142 changed files with 10690 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package gen
import (
"strings"
"text/template"
"zero/tools/goctl/util"
"zero/tools/goctl/vars"
)
func GenerateDockerfile(goFile string, args ...string) error {
relPath, err := util.PathFromGoSrc()
if err != nil {
return err
}
out, err := util.CreateIfNotExist("Dockerfile")
if err != nil {
return err
}
defer out.Close()
var builder strings.Builder
for _, arg := range args {
builder.WriteString(`, "` + arg + `"`)
}
t := template.Must(template.New("dockerfile").Parse(dockerTemplate))
return t.Execute(out, map[string]string{
"projectName": vars.ProjectName,
"goRelPath": relPath,
"goFile": goFile,
"exeFile": util.FileNameWithoutExt(goFile),
"argument": builder.String(),
})
}

View File

@@ -0,0 +1,52 @@
package gen
import (
"strings"
"text/template"
"zero/tools/goctl/util"
)
func GenerateMakefile(goFile, namespace string) error {
relPath, err := util.PathFromGoSrc()
if err != nil {
return err
}
movePath, err := getMovePath()
if err != nil {
return err
}
out, err := util.CreateIfNotExist("Makefile")
if err != nil {
return err
}
defer out.Close()
t := template.Must(template.New("makefile").Parse(makefileTemplate))
return t.Execute(out, map[string]string{
"rootRelPath": movePath,
"relPath": relPath,
"exeFile": util.FileNameWithoutExt(goFile),
"namespace": namespace,
})
}
func getMovePath() (string, error) {
relPath, err := util.PathFromGoSrc()
if err != nil {
return "", err
}
var builder strings.Builder
for range strings.Split(relPath, "/") {
builder.WriteString("../")
}
if move := builder.String(); len(move) == 0 {
return ".", nil
} else {
return move, nil
}
}

View File

@@ -0,0 +1,44 @@
package gen
const (
dockerTemplate = `FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOOS linux
ENV GOPROXY https://goproxy.cn,direct
WORKDIR $GOPATH/src/{{.projectName}}
COPY . .
RUN go build -ldflags="-s -w" -o /app/{{.exeFile}} {{.goRelPath}}/{{.goFile}}
FROM alpine
RUN apk update --no-cache
RUN apk add --no-cache ca-certificates
RUN apk add --no-cache tzdata
ENV TZ Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/{{.exeFile}} /app/{{.exeFile}}
CMD ["./{{.exeFile}}"{{.argument}}]
`
makefileTemplate = `version := v$(shell /bin/date "+%y%m%d%H%M%S")
build:
docker pull alpine
docker pull golang:alpine
cd $(GOPATH)/src/xiao && docker build -t registry.cn-hangzhou.aliyuncs.com/xapp/{{.exeFile}}:$(version) . -f {{.relPath}}/Dockerfile
docker image prune --filter label=stage=gobuilder -f
push: build
docker push registry.cn-hangzhou.aliyuncs.com/xapp/{{.exeFile}}:$(version)
deploy: push
kubectl -n {{.namespace}} set image deployment/{{.exeFile}}-deployment {{.exeFile}}=registry-vpc.cn-hangzhou.aliyuncs.com/xapp/{{.exeFile}}:$(version)
`
)