fix dockerfile generation

master
kevin 4 years ago
parent eb40c2731d
commit 7b17b3604a

@ -3,8 +3,8 @@ version := $(shell /bin/date "+%Y-%m-%d %H:%M")
build: build:
go build -ldflags="-s -w" -ldflags="-X 'main.BuildTime=$(version)'" goctl.go && upx goctl go build -ldflags="-s -w" -ldflags="-X 'main.BuildTime=$(version)'" goctl.go && upx goctl
mac: 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: 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: 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

@ -141,23 +141,26 @@ func createGoModFileIfNeed(dir string) {
} }
_, hasGoMod := util.FindGoModPath(dir) _, hasGoMod := util.FindGoModPath(dir)
if !hasGoMod { if hasGoMod {
gopath := os.Getenv("GOPATH") return
parent := path.Join(gopath, "src") }
pos := strings.Index(absDir, parent)
if pos < 0 { gopath := os.Getenv("GOPATH")
moduleName := absDir[len(filepath.Dir(absDir))+1:] parent := path.Join(gopath, "src")
cmd := exec.Command("go", "mod", "init", moduleName) pos := strings.Index(absDir, parent)
cmd.Dir = dir if pos >= 0 {
var stdout, stderr bytes.Buffer return
cmd.Stdout = &stdout }
cmd.Stderr = &stderr
err := cmd.Run() moduleName := absDir[len(filepath.Dir(absDir))+1:]
if err != nil { cmd := exec.Command("go", "mod", "init", moduleName)
fmt.Println(err.Error()) cmd.Dir = dir
} var stdout, stderr bytes.Buffer
outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes()) cmd.Stdout = &stdout
fmt.Printf(outStr + "\n" + errStr) 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)
} }

@ -1,6 +1,7 @@
package gen package gen
import ( import (
"path/filepath"
"strings" "strings"
"text/template" "text/template"
@ -9,11 +10,16 @@ import (
) )
func GenerateDockerfile(goFile string, args ...string) error { func GenerateDockerfile(goFile string, args ...string) error {
projPath, err := getFilePath(goFile) projPath, err := getFilePath(filepath.Dir(goFile))
if err != nil { if err != nil {
return err return err
} }
pos := strings.IndexByte(projPath, '/')
if pos >= 0 {
projPath = projPath[pos+1:]
}
out, err := util.CreateIfNotExist("Dockerfile") out, err := util.CreateIfNotExist("Dockerfile")
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save