From 539215d7df1d2d06016faaa08b00be11f610226f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=A8=E6=9B=A6=E4=B8=AD?= <284236865@qq.com> Date: Sat, 6 Nov 2021 21:28:32 +0800 Subject: [PATCH] goctl docker command add -version (#1206) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feature(优化): 优化goctl goctl docker 命令新增version参数,指定builder golang 版本 * feature(优化): 优化goctl goctl docker 命令新增version参数,指定builder golang 版本 --- tools/goctl/docker/docker.go | 13 ++++++++++--- tools/goctl/docker/template.go | 2 +- tools/goctl/goctl.go | 4 ++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index c2befbac..df03dc09 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -30,6 +30,7 @@ type Docker struct { HasPort bool Port int Argument string + Version string } // DockerCommand provides the entry for goctl docker @@ -42,6 +43,11 @@ func DockerCommand(c *cli.Context) (err error) { goFile := c.String("go") home := c.String("home") + version := c.String("version") + + if len(version) > 0 { + version = version + "-" + } if len(home) > 0 { util.RegisterGoctlHome(home) @@ -57,7 +63,7 @@ func DockerCommand(c *cli.Context) (err error) { port := c.Int("port") if _, err := os.Stat(etcDir); os.IsNotExist(err) { - return generateDockerfile(goFile, port) + return generateDockerfile(goFile, port, version) } cfg, err := findConfig(goFile, etcDir) @@ -65,7 +71,7 @@ func DockerCommand(c *cli.Context) (err error) { return err } - if err := generateDockerfile(goFile, port, "-f", "etc/"+cfg); err != nil { + if err := generateDockerfile(goFile, port, version, "-f", "etc/"+cfg); err != nil { return err } @@ -106,7 +112,7 @@ func findConfig(file, dir string) (string, error) { return files[0], nil } -func generateDockerfile(goFile string, port int, args ...string) error { +func generateDockerfile(goFile string, port int, version string, args ...string) error { projPath, err := getFilePath(filepath.Dir(goFile)) if err != nil { return err @@ -147,6 +153,7 @@ func generateDockerfile(goFile string, port int, args ...string) error { HasPort: port > 0, Port: port, Argument: builder.String(), + Version: version, }) } diff --git a/tools/goctl/docker/template.go b/tools/goctl/docker/template.go index b21e2666..b4d721e6 100644 --- a/tools/goctl/docker/template.go +++ b/tools/goctl/docker/template.go @@ -8,7 +8,7 @@ import ( const ( category = "docker" dockerTemplateFile = "docker.tpl" - dockerTemplate = `FROM golang:alpine AS builder + dockerTemplate = `FROM golang:{{.Version}}alpine AS builder LABEL stage=gobuilder diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index f98c6587..191b4332 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -255,6 +255,10 @@ var commands = []cli.Command{ Name: "home", Usage: "the goctl home path of the template", }, + cli.StringFlag{ + Name: "version", + Usage: "the goctl builder golang image version", + }, }, Action: docker.DockerCommand, },