From c686c93fb54569b621bf61feda456b653b99bb54 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 11 Dec 2020 20:31:31 +0800 Subject: [PATCH] fix dockerfile generation bug (#277) --- tools/goctl/docker/docker.go | 21 ++++++++++++++------- tools/goctl/docker/template.go | 8 ++++---- tools/goctl/goctl.go | 2 +- tools/goctl/util/path.go | 1 - 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index 6ac1f5fc..04ed9be5 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -16,9 +16,10 @@ import ( ) const ( - etcDir = "etc" - yamlEtx = ".yaml" - cstOffset = 60 * 60 * 8 // 8 hours offset for Chinese Standard Time + dockerfileName = "Dockerfile" + etcDir = "etc" + yamlEtx = ".yaml" + cstOffset = 60 * 60 * 8 // 8 hours offset for Chinese Standard Time ) type Docker struct { @@ -26,6 +27,7 @@ type Docker struct { GoRelPath string GoFile string ExeFile string + HasArgs bool Argument string } @@ -96,12 +98,16 @@ func generateDockerfile(goFile string, args ...string) error { return err } - pos := strings.IndexByte(projPath, '/') - if pos >= 0 { - projPath = projPath[pos+1:] + if len(projPath) == 0 { + projPath = "." + } else { + pos := strings.IndexByte(projPath, os.PathSeparator) + if pos >= 0 { + projPath = projPath[pos+1:] + } } - out, err := util.CreateIfNotExist("Dockerfile") + out, err := util.CreateIfNotExist(dockerfileName) if err != nil { return err } @@ -124,6 +130,7 @@ func generateDockerfile(goFile string, args ...string) error { GoRelPath: projPath, GoFile: goFile, ExeFile: util.FileNameWithoutExt(filepath.Base(goFile)), + HasArgs: builder.Len() > 0, Argument: builder.String(), }) } diff --git a/tools/goctl/docker/template.go b/tools/goctl/docker/template.go index d9036848..ecf63dc4 100644 --- a/tools/goctl/docker/template.go +++ b/tools/goctl/docker/template.go @@ -22,8 +22,8 @@ ADD go.mod . ADD go.sum . RUN go mod download COPY . . -COPY {{.GoRelPath}}/etc /app/etc -RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}} +{{if .HasArgs}}COPY {{.GoRelPath}}/etc /app/etc +{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}} FROM alpine @@ -33,8 +33,8 @@ ENV TZ Asia/Shanghai WORKDIR /app COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}} -COPY --from=builder /app/etc /app/etc - +{{if .HasArgs}}COPY --from=builder /app/etc /app/etc +{{end}} CMD ["./{{.ExeFile}}"{{.Argument}}] ` ) diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 053a2b71..17987295 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -27,7 +27,7 @@ import ( ) var ( - BuildVersion = "20201125" + BuildVersion = "1.1.1" commands = []cli.Command{ { Name: "api", diff --git a/tools/goctl/util/path.go b/tools/goctl/util/path.go index 6f9e812d..0ed6b333 100644 --- a/tools/goctl/util/path.go +++ b/tools/goctl/util/path.go @@ -60,7 +60,6 @@ func FindGoModPath(dir string) (string, bool) { var hasGoMod = false for { if FileExists(filepath.Join(tempPath, goModeIdentifier)) { - tempPath = filepath.Dir(tempPath) rootPath = strings.TrimPrefix(absDir[len(tempPath):], "/") hasGoMod = true break