소스 검색

add dockerfile into template

kevin 4 년 전
부모
커밋
cb9075b737
5개의 변경된 파일78개의 추가작업 그리고 70개의 파일을 삭제
  1. 57 2
      tools/goctl/docker/docker.go
  2. 17 2
      tools/goctl/docker/template.go
  3. 0 40
      tools/goctl/gen/dockerfile.go
  4. 0 26
      tools/goctl/gen/filepath.go
  5. 4 0
      tools/goctl/tpl/templates.go

+ 57 - 2
tools/goctl/docker/docker.go

@@ -5,8 +5,10 @@ import (
 	"os"
 	"path/filepath"
 	"strings"
+	"text/template"
 
-	"github.com/tal-tech/go-zero/tools/goctl/gen"
+	"github.com/tal-tech/go-zero/tools/goctl/util"
+	ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
 	"github.com/urfave/cli"
 )
 
@@ -26,7 +28,7 @@ func DockerCommand(c *cli.Context) error {
 		return err
 	}
 
-	return gen.GenerateDockerfile(goFile, "-f", "etc/"+cfg)
+	return generateDockerfile(goFile, "-f", "etc/"+cfg)
 }
 
 func findConfig(file, dir string) (string, error) {
@@ -57,3 +59,56 @@ func findConfig(file, dir string) (string, error) {
 
 	return files[0], nil
 }
+
+func generateDockerfile(goFile string, args ...string) error {
+	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
+	}
+	defer out.Close()
+
+	text, err := ctlutil.LoadTemplate(category, dockerTemplateFile, dockerTemplate)
+	if err != nil {
+		return err
+	}
+
+	var builder strings.Builder
+	for _, arg := range args {
+		builder.WriteString(`, "` + arg + `"`)
+	}
+
+	t := template.Must(template.New("dockerfile").Parse(text))
+	return t.Execute(out, map[string]string{
+		"goRelPath": projPath,
+		"goFile":    goFile,
+		"exeFile":   util.FileNameWithoutExt(filepath.Base(goFile)),
+		"argument":  builder.String(),
+	})
+}
+
+func getFilePath(file string) (string, error) {
+	wd, err := os.Getwd()
+	if err != nil {
+		return "", err
+	}
+
+	projPath, ok := util.FindGoModPath(filepath.Join(wd, file))
+	if !ok {
+		projPath, err = util.PathFromGoSrc()
+		if err != nil {
+			return "", errors.New("no go.mod found, or not in GOPATH")
+		}
+	}
+
+	return projPath, nil
+}

+ 17 - 2
tools/goctl/gen/template.go → tools/goctl/docker/template.go

@@ -1,6 +1,14 @@
-package gen
+package docker
 
-const dockerTemplate = `FROM golang:alpine AS builder
+import (
+	"github.com/tal-tech/go-zero/tools/goctl/util"
+	"github.com/urfave/cli"
+)
+
+const (
+	category           = "docker"
+	dockerTemplateFile = "docker.tpl"
+	dockerTemplate     = `FROM golang:alpine AS builder
 
 LABEL stage=gobuilder
 
@@ -27,3 +35,10 @@ COPY --from=builder /app/etc /app/etc
 
 CMD ["./{{.exeFile}}"{{.argument}}]
 `
+)
+
+func GenTemplates(_ *cli.Context) error {
+	return util.InitTemplates(category, map[string]string{
+		dockerTemplateFile: dockerTemplate,
+	})
+}

+ 0 - 40
tools/goctl/gen/dockerfile.go

@@ -1,40 +0,0 @@
-package gen
-
-import (
-	"path/filepath"
-	"strings"
-	"text/template"
-
-	"github.com/tal-tech/go-zero/tools/goctl/util"
-)
-
-func GenerateDockerfile(goFile string, args ...string) error {
-	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
-	}
-	defer out.Close()
-
-	var builder strings.Builder
-	for _, arg := range args {
-		builder.WriteString(`, "` + arg + `"`)
-	}
-
-	t := template.Must(template.New("dockerfile").Parse(dockerTemplate))
-	return t.Execute(out, map[string]string{
-		"goRelPath": projPath,
-		"goFile":    goFile,
-		"exeFile":   util.FileNameWithoutExt(filepath.Base(goFile)),
-		"argument":  builder.String(),
-	})
-}

+ 0 - 26
tools/goctl/gen/filepath.go

@@ -1,26 +0,0 @@
-package gen
-
-import (
-	"errors"
-	"os"
-	"path/filepath"
-
-	"github.com/tal-tech/go-zero/tools/goctl/util"
-)
-
-func getFilePath(file string) (string, error) {
-	wd, err := os.Getwd()
-	if err != nil {
-		return "", err
-	}
-
-	projPath, ok := util.FindGoModPath(filepath.Join(wd, file))
-	if !ok {
-		projPath, err = util.PathFromGoSrc()
-		if err != nil {
-			return "", errors.New("no go.mod found, or not in GOPATH")
-		}
-	}
-
-	return projPath, nil
-}

+ 4 - 0
tools/goctl/tpl/templates.go

@@ -6,6 +6,7 @@ import (
 	"github.com/logrusorgru/aurora"
 	"github.com/tal-tech/go-zero/core/errorx"
 	"github.com/tal-tech/go-zero/tools/goctl/api/gogen"
+	"github.com/tal-tech/go-zero/tools/goctl/docker"
 	modelgen "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
 	rpcgen "github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
 	"github.com/tal-tech/go-zero/tools/goctl/util"
@@ -25,6 +26,9 @@ func GenTemplates(ctx *cli.Context) error {
 		func() error {
 			return rpcgen.GenTemplates(ctx)
 		},
+		func() error {
+			return docker.GenTemplates(ctx)
+		},
 	); err != nil {
 		return err
 	}