From 9eea311a4defa5b3155cffa69559e230dafdbfc2 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 18 Jun 2022 18:32:07 +0800 Subject: [PATCH] feat: support build Dockerfile from current dir (#2021) --- tools/goctl/docker/cmd.go | 2 ++ tools/goctl/docker/docker.go | 35 ++++++++++++++++++++++++++--------- tools/goctl/docker/docker.tpl | 2 +- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/tools/goctl/docker/cmd.go b/tools/goctl/docker/cmd.go index 00ed6d59..2a82d20a 100644 --- a/tools/goctl/docker/cmd.go +++ b/tools/goctl/docker/cmd.go @@ -3,6 +3,7 @@ package docker import "github.com/spf13/cobra" var ( + varExeName string varStringGo string varStringBase string varIntPort int @@ -21,6 +22,7 @@ var ( ) func init() { + Cmd.Flags().StringVar(&varExeName, "exe", "", "The executable name in the built image") Cmd.Flags().StringVar(&varStringGo, "go", "", "The file 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") diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index 0dbc42f4..15ad115a 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "path" "path/filepath" "strings" "text/template" @@ -24,6 +25,7 @@ const ( // Docker describes a dockerfile type Docker struct { Chinese bool + GoMainFrom string GoRelPath string GoFile string ExeFile string @@ -65,11 +67,7 @@ func dockerCommand(_ *cobra.Command, _ []string) (err error) { pathx.RegisterGoctlHome(home) } - if len(goFile) == 0 { - return errors.New("-go can't be empty") - } - - if !pathx.FileExists(goFile) { + if len(goFile) > 0 && !pathx.FileExists(goFile) { return fmt.Errorf("file %q not found", goFile) } @@ -126,9 +124,13 @@ func findConfig(file, dir string) (string, error) { } func generateDockerfile(goFile, base string, port int, version, timezone string, args ...string) error { - projPath, err := getFilePath(filepath.Dir(goFile)) - if err != nil { - return err + var projPath string + var err error + if len(goFile) > 0 { + projPath, err = getFilePath(filepath.Dir(goFile)) + if err != nil { + return err + } } if len(projPath) == 0 { @@ -151,12 +153,27 @@ func generateDockerfile(goFile, base string, port int, version, timezone string, builder.WriteString(`, "` + arg + `"`) } + var exeName string + if len(varExeName) > 0 { + exeName = varExeName + } else if len(goFile) > 0 { + exeName = pathx.FileNameWithoutExt(filepath.Base(goFile)) + } else { + absPath, err := filepath.Abs(projPath) + if err != nil { + return err + } + + exeName = filepath.Base(absPath) + } + t := template.Must(template.New("dockerfile").Parse(text)) return t.Execute(out, Docker{ Chinese: env.InChina(), + GoMainFrom: path.Join(projPath, goFile), GoRelPath: projPath, GoFile: goFile, - ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)), + ExeFile: exeName, BaseImage: base, HasPort: port > 0, Port: port, diff --git a/tools/goctl/docker/docker.tpl b/tools/goctl/docker/docker.tpl index 250a9dd4..d1b5ff44 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}} {{.GoMainFrom}} FROM {{.BaseImage}}