Эх сурвалжийг харах

add EXPOSE in dockerfile generation (#281)

Kevin Wan 4 жил өмнө
parent
commit
4d13dda605

+ 8 - 3
tools/goctl/docker/docker.go

@@ -27,6 +27,8 @@ type Docker struct {
 	GoRelPath string
 	GoFile    string
 	ExeFile   string
+	HasPort   bool
+	Port      int
 	HasArgs   bool
 	Argument  string
 }
@@ -41,8 +43,9 @@ func DockerCommand(c *cli.Context) error {
 		return fmt.Errorf("file %q not found", goFile)
 	}
 
+	port := c.Int("port")
 	if _, err := os.Stat(etcDir); os.IsNotExist(err) {
-		return generateDockerfile(goFile)
+		return generateDockerfile(goFile, port)
 	}
 
 	cfg, err := findConfig(goFile, etcDir)
@@ -50,7 +53,7 @@ func DockerCommand(c *cli.Context) error {
 		return err
 	}
 
-	if err := generateDockerfile(goFile, "-f", "etc/"+cfg); err != nil {
+	if err := generateDockerfile(goFile, port, "-f", "etc/"+cfg); err != nil {
 		return err
 	}
 
@@ -92,7 +95,7 @@ func findConfig(file, dir string) (string, error) {
 	return files[0], nil
 }
 
-func generateDockerfile(goFile string, args ...string) error {
+func generateDockerfile(goFile string, port int, args ...string) error {
 	projPath, err := getFilePath(filepath.Dir(goFile))
 	if err != nil {
 		return err
@@ -130,6 +133,8 @@ func generateDockerfile(goFile string, args ...string) error {
 		GoRelPath: projPath,
 		GoFile:    goFile,
 		ExeFile:   util.FileNameWithoutExt(filepath.Base(goFile)),
+		HasPort:   port > 0,
+		Port:      port,
 		HasArgs:   builder.Len() > 0,
 		Argument:  builder.String(),
 	})

+ 3 - 1
tools/goctl/docker/template.go

@@ -33,7 +33,9 @@ ENV TZ Asia/Shanghai
 
 WORKDIR /app
 COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}
-{{if .HasArgs}}COPY --from=builder /app/etc /app/etc
+{{if .HasArgs}}COPY --from=builder /app/etc /app/etc{{end}}
+{{if .HasPort}}
+EXPOSE {{.Port}}
 {{end}}
 CMD ["./{{.ExeFile}}"{{.Argument}}]
 `

+ 27 - 33
tools/goctl/goctl.go

@@ -54,14 +54,12 @@ var (
 							Usage: "the format target dir",
 						},
 						cli.BoolFlag{
-							Name:     "iu",
-							Usage:    "ignore update",
-							Required: false,
+							Name:  "iu",
+							Usage: "ignore update",
 						},
 						cli.BoolFlag{
-							Name:     "stdin",
-							Usage:    "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
-							Required: false,
+							Name:  "stdin",
+							Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
 						},
 					},
 					Action: format.GoFormatApi,
@@ -101,9 +99,8 @@ var (
 							Usage: "the api file",
 						},
 						cli.StringFlag{
-							Name:     "style",
-							Required: false,
-							Usage:    "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
+							Name:  "style",
+							Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
 						},
 					},
 					Action: gogen.GoCommand,
@@ -136,19 +133,16 @@ var (
 							Usage: "the api file",
 						},
 						cli.StringFlag{
-							Name:     "webapi",
-							Usage:    "the web api file path",
-							Required: false,
+							Name:  "webapi",
+							Usage: "the web api file path",
 						},
 						cli.StringFlag{
-							Name:     "caller",
-							Usage:    "the web api caller",
-							Required: false,
+							Name:  "caller",
+							Usage: "the web api caller",
 						},
 						cli.BoolFlag{
-							Name:     "unwrap",
-							Usage:    "unwrap the webapi caller for import",
-							Required: false,
+							Name:  "unwrap",
+							Usage: "unwrap the webapi caller for import",
 						},
 					},
 					Action: tsgen.TsCommand,
@@ -204,9 +198,8 @@ var (
 							Usage: "the api file",
 						},
 						cli.StringFlag{
-							Name:     "style",
-							Required: false,
-							Usage:    "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
+							Name:  "style",
+							Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
 						},
 					},
 					Action: plugin.PluginCommand,
@@ -221,6 +214,11 @@ var (
 					Name:  "go",
 					Usage: "the file that contains main function",
 				},
+				cli.IntFlag{
+					Name:  "port",
+					Usage: "the port to expose, default none",
+					Value: 0,
+				},
 			},
 			Action: docker.DockerCommand,
 		},
@@ -321,9 +319,8 @@ var (
 					Usage: `generate rpc demo service`,
 					Flags: []cli.Flag{
 						cli.StringFlag{
-							Name:     "style",
-							Required: false,
-							Usage:    "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
+							Name:  "style",
+							Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
 						},
 						cli.BoolFlag{
 							Name:  "idea",
@@ -360,9 +357,8 @@ var (
 							Usage: `the target path of the code`,
 						},
 						cli.StringFlag{
-							Name:     "style",
-							Required: false,
-							Usage:    "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
+							Name:  "style",
+							Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
 						},
 						cli.BoolFlag{
 							Name:  "idea",
@@ -394,9 +390,8 @@ var (
 									Usage: "the target dir",
 								},
 								cli.StringFlag{
-									Name:     "style",
-									Required: false,
-									Usage:    "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
+									Name:  "style",
+									Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
 								},
 								cli.BoolFlag{
 									Name:  "cache, c",
@@ -430,9 +425,8 @@ var (
 									Usage: "the target dir",
 								},
 								cli.StringFlag{
-									Name:     "style",
-									Required: false,
-									Usage:    "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
+									Name:  "style",
+									Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
 								},
 								cli.BoolFlag{
 									Name:  "idea",