From 7b17b3604ab17664ae42bb44fb9ccfa577174426 Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 31 Aug 2020 12:27:38 +0800 Subject: [PATCH] fix dockerfile generation --- tools/goctl/Makefile | 6 +++--- tools/goctl/api/gogen/gen.go | 39 +++++++++++++++++++---------------- tools/goctl/gen/dockerfile.go | 8 ++++++- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/tools/goctl/Makefile b/tools/goctl/Makefile index f4b34408..8f6f9001 100644 --- a/tools/goctl/Makefile +++ b/tools/goctl/Makefile @@ -3,8 +3,8 @@ version := $(shell /bin/date "+%Y-%m-%d %H:%M") build: go build -ldflags="-s -w" -ldflags="-X 'main.BuildTime=$(version)'" goctl.go && upx goctl mac: - GOOS=darwin go build -ldflags="-s -w" -ldflags="-X 'main.BuildTime=$(version)'" -o goctl-darwin goctl.go + GOOS=darwin go build -ldflags="-s -w" -ldflags="-X 'main.BuildTime=$(version)'" -o goctl-darwin goctl.go && upx goctl-darwin win: - GOOS=windows go build -ldflags="-s -w" -ldflags="-X 'main.BuildTime=$(version)'" -o goctl.exe goctl.go + GOOS=windows go build -ldflags="-s -w" -ldflags="-X 'main.BuildTime=$(version)'" -o goctl.exe goctl.go && upx goctl.exe linux: - GOOS=linux go build -ldflags="-s -w" -ldflags="-X 'main.BuildTime=$(version)'" -o goctl-linux goctl.go + GOOS=linux go build -ldflags="-s -w" -ldflags="-X 'main.BuildTime=$(version)'" -o goctl-linux goctl.go && upx goctl-linux diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index dcd2a160..d9442332 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -141,23 +141,26 @@ func createGoModFileIfNeed(dir string) { } _, hasGoMod := util.FindGoModPath(dir) - if !hasGoMod { - gopath := os.Getenv("GOPATH") - parent := path.Join(gopath, "src") - pos := strings.Index(absDir, parent) - if pos < 0 { - moduleName := absDir[len(filepath.Dir(absDir))+1:] - cmd := exec.Command("go", "mod", "init", moduleName) - cmd.Dir = dir - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - err := cmd.Run() - if err != nil { - fmt.Println(err.Error()) - } - outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes()) - fmt.Printf(outStr + "\n" + errStr) - } + if hasGoMod { + return + } + + gopath := os.Getenv("GOPATH") + parent := path.Join(gopath, "src") + pos := strings.Index(absDir, parent) + if pos >= 0 { + return + } + + moduleName := absDir[len(filepath.Dir(absDir))+1:] + cmd := exec.Command("go", "mod", "init", moduleName) + cmd.Dir = dir + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + if err = cmd.Run(); err != nil { + fmt.Println(err.Error()) } + outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes()) + fmt.Printf(outStr + "\n" + errStr) } diff --git a/tools/goctl/gen/dockerfile.go b/tools/goctl/gen/dockerfile.go index 1aa6fe3c..0e821063 100644 --- a/tools/goctl/gen/dockerfile.go +++ b/tools/goctl/gen/dockerfile.go @@ -1,6 +1,7 @@ package gen import ( + "path/filepath" "strings" "text/template" @@ -9,11 +10,16 @@ import ( ) func GenerateDockerfile(goFile string, args ...string) error { - projPath, err := getFilePath(goFile) + projPath, err := getFilePath(filepath.Dir(goFile)) if err != nil { return err } + pos := strings.IndexByte(projPath, '/') + if pos >= 0 { + projPath = projPath[pos+1:] + } + out, err := util.CreateIfNotExist("Dockerfile") if err != nil { return err