From acd48f0abb445fa97ad8ce4871af8a97f5d61b23 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 12 Dec 2020 16:53:06 +0800 Subject: [PATCH] optimize dockerfile generation (#284) --- tools/goctl/docker/docker.go | 11 +++++++---- tools/goctl/docker/template.go | 6 +++--- tools/goctl/goctl.go | 5 ++--- tools/goctl/kube/deployment.go | 4 ++-- tools/goctl/kube/kube.go | 10 +++++++++- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index 0a525265..066cce5f 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -29,11 +29,16 @@ type Docker struct { ExeFile string HasPort bool Port int - HasArgs bool Argument string } -func DockerCommand(c *cli.Context) error { +func DockerCommand(c *cli.Context) (err error) { + defer func() { + if err == nil { + fmt.Println(aurora.Green("Done.")) + } + }() + goFile := c.String("go") if len(goFile) == 0 { return errors.New("-go can't be empty") @@ -60,7 +65,6 @@ func DockerCommand(c *cli.Context) error { projDir, ok := util.FindProjectPath(goFile) if ok { fmt.Printf("Hint: run \"docker build ...\" command in dir %q\n", projDir) - fmt.Println(aurora.Green("Done.")) } return nil @@ -135,7 +139,6 @@ func generateDockerfile(goFile string, port int, args ...string) error { ExeFile: util.FileNameWithoutExt(filepath.Base(goFile)), HasPort: port > 0, Port: port, - HasArgs: builder.Len() > 0, Argument: builder.String(), }) } diff --git a/tools/goctl/docker/template.go b/tools/goctl/docker/template.go index d55d1356..fedc82a7 100644 --- a/tools/goctl/docker/template.go +++ b/tools/goctl/docker/template.go @@ -22,7 +22,7 @@ ADD go.mod . ADD go.sum . RUN go mod download COPY . . -{{if .HasArgs}}COPY {{.GoRelPath}}/etc /app/etc +{{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc {{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}} @@ -32,8 +32,8 @@ RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata ENV TZ Asia/Shanghai WORKDIR /app -COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}} -{{if .HasArgs}}COPY --from=builder /app/etc /app/etc{{end}} +COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}} +COPY --from=builder /app/etc /app/etc{{end}} {{if .HasPort}} EXPOSE {{.Port}} {{end}} diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index a74d8572..21ac09cf 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -246,9 +246,8 @@ var ( Required: true, }, cli.StringFlag{ - Name: "secret", - Usage: "the image pull secret", - Required: true, + Name: "secret", + Usage: "the secret to image pull from registry", }, cli.IntFlag{ Name: "requestCpu", diff --git a/tools/goctl/kube/deployment.go b/tools/goctl/kube/deployment.go index 710393c6..63d26798 100644 --- a/tools/goctl/kube/deployment.go +++ b/tools/goctl/kube/deployment.go @@ -47,9 +47,9 @@ spec: volumeMounts: - name: timezone mountPath: /etc/localtime - imagePullSecrets: + {{if .Secret}}imagePullSecrets: - name: {{.Secret}} - volumes: + {{end}}volumes: - name: timezone hostPath: path: /usr/share/zoneinfo/Asia/Shanghai diff --git a/tools/goctl/kube/kube.go b/tools/goctl/kube/kube.go index 7dc1461f..9e646fa0 100644 --- a/tools/goctl/kube/kube.go +++ b/tools/goctl/kube/kube.go @@ -2,8 +2,10 @@ package kube import ( "errors" + "fmt" "text/template" + "github.com/logrusorgru/aurora" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/urfave/cli" ) @@ -53,7 +55,7 @@ func DeploymentCommand(c *cli.Context) error { defer out.Close() t := template.Must(template.New("deploymentTemplate").Parse(text)) - return t.Execute(out, Deployment{ + err = t.Execute(out, Deployment{ Name: c.String("name"), Namespace: c.String("namespace"), Image: c.String("image"), @@ -70,6 +72,12 @@ func DeploymentCommand(c *cli.Context) error { MinReplicas: c.Int("minReplicas"), MaxReplicas: c.Int("maxReplicas"), }) + if err != nil { + return err + } + + fmt.Println(aurora.Green("Done.")) + return nil } func Category() string {