From 289a32575729117572f91e5f0be9cb29462e07de Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 30 May 2022 13:26:58 +0800 Subject: [PATCH] chore: refine docker for better compatible with package main (#1944) * chore: refine docker for better compatible with package main * chore: default to current dir on goctl docker command --- tools/goctl/docker/cmd.go | 2 +- tools/goctl/docker/docker.go | 7 ++++++- tools/goctl/docker/docker.tpl | 2 +- tools/goctl/model/sql/template/model.go | 4 ++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tools/goctl/docker/cmd.go b/tools/goctl/docker/cmd.go index 00ed6d59..dbb81682 100644 --- a/tools/goctl/docker/cmd.go +++ b/tools/goctl/docker/cmd.go @@ -21,7 +21,7 @@ var ( ) func init() { - Cmd.Flags().StringVar(&varStringGo, "go", "", "The file that contains main function") + Cmd.Flags().StringVar(&varStringGo, "go", ".", "The directory that contains main function") Cmd.Flags().StringVar(&varStringBase, "base", "scratch", "The base image to build the docker image, default scratch") Cmd.Flags().IntVar(&varIntPort, "port", 0, "The port to expose, default none") Cmd.Flags().StringVar(&varStringHome, "home", "", "The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority") diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index 0dbc42f4..500d5aa5 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -151,12 +151,17 @@ func generateDockerfile(goFile, base string, port int, version, timezone string, builder.WriteString(`, "` + arg + `"`) } + absGoPath, err := filepath.Abs(goFile) + if err != nil { + return err + } + t := template.Must(template.New("dockerfile").Parse(text)) return t.Execute(out, Docker{ Chinese: env.InChina(), GoRelPath: projPath, GoFile: goFile, - ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)), + ExeFile: filepath.Base(absGoPath), BaseImage: base, HasPort: port > 0, Port: port, diff --git a/tools/goctl/docker/docker.tpl b/tools/goctl/docker/docker.tpl index 250a9dd4..f9516d9e 100644 --- a/tools/goctl/docker/docker.tpl +++ b/tools/goctl/docker/docker.tpl @@ -15,7 +15,7 @@ ADD go.sum . RUN go mod download COPY . . {{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc -{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}} +{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}} FROM {{.BaseImage}} diff --git a/tools/goctl/model/sql/template/model.go b/tools/goctl/model/sql/template/model.go index c134c5b9..66183fde 100644 --- a/tools/goctl/model/sql/template/model.go +++ b/tools/goctl/model/sql/template/model.go @@ -46,10 +46,10 @@ package {{.pkg}} {{.vars}} {{.types}} {{.new}} -{{.insert}} +{{.delete}} {{.find}} +{{.insert}} {{.update}} -{{.delete}} {{.extraMethod}} {{.tableName}} `, util.DoNotEditHead)