add EXPOSE in dockerfile generation (#281)

This commit is contained in:
Kevin Wan
2020-12-12 08:18:01 +08:00
committed by GitHub
parent b56cc8e459
commit 4d13dda605
3 changed files with 38 additions and 37 deletions

View File

@@ -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(),
})

View File

@@ -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}}]
`

View File

@@ -56,12 +56,10 @@ var (
cli.BoolFlag{
Name: "iu",
Usage: "ignore update",
Required: false,
},
cli.BoolFlag{
Name: "stdin",
Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
Required: false,
},
},
Action: format.GoFormatApi,
@@ -102,7 +100,6 @@ var (
},
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]",
},
},
@@ -138,17 +135,14 @@ var (
cli.StringFlag{
Name: "webapi",
Usage: "the web api file path",
Required: false,
},
cli.StringFlag{
Name: "caller",
Usage: "the web api caller",
Required: false,
},
cli.BoolFlag{
Name: "unwrap",
Usage: "unwrap the webapi caller for import",
Required: false,
},
},
Action: tsgen.TsCommand,
@@ -205,7 +199,6 @@ var (
},
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]",
},
},
@@ -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,
},
@@ -322,7 +320,6 @@ var (
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]",
},
cli.BoolFlag{
@@ -361,7 +358,6 @@ var (
},
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]",
},
cli.BoolFlag{
@@ -395,7 +391,6 @@ var (
},
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]",
},
cli.BoolFlag{
@@ -431,7 +426,6 @@ var (
},
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]",
},
cli.BoolFlag{