Skip to content

Commit

Permalink
fix dockerfile generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Aug 31, 2020
1 parent eb40c27 commit 7b17b36
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
6 changes: 3 additions & 3 deletions tools/goctl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 21 additions & 18 deletions tools/goctl/api/gogen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 7 additions & 1 deletion tools/goctl/gen/dockerfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gen

import (
"path/filepath"
"strings"
"text/template"

Expand All @@ -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
Expand Down

0 comments on commit 7b17b36

Please sign in to comment.