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

View File

@@ -33,7 +33,9 @@ ENV TZ Asia/Shanghai
WORKDIR /app WORKDIR /app
COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}} 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}} {{end}}
CMD ["./{{.ExeFile}}"{{.Argument}}] CMD ["./{{.ExeFile}}"{{.Argument}}]
` `

View File

@@ -56,12 +56,10 @@ var (
cli.BoolFlag{ cli.BoolFlag{
Name: "iu", Name: "iu",
Usage: "ignore update", Usage: "ignore update",
Required: false,
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "stdin", Name: "stdin",
Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF", Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
Required: false,
}, },
}, },
Action: format.GoFormatApi, Action: format.GoFormatApi,
@@ -102,7 +100,6 @@ var (
}, },
cli.StringFlag{ cli.StringFlag{
Name: "style", Name: "style",
Required: false,
Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]", 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{ cli.StringFlag{
Name: "webapi", Name: "webapi",
Usage: "the web api file path", Usage: "the web api file path",
Required: false,
}, },
cli.StringFlag{ cli.StringFlag{
Name: "caller", Name: "caller",
Usage: "the web api caller", Usage: "the web api caller",
Required: false,
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "unwrap", Name: "unwrap",
Usage: "unwrap the webapi caller for import", Usage: "unwrap the webapi caller for import",
Required: false,
}, },
}, },
Action: tsgen.TsCommand, Action: tsgen.TsCommand,
@@ -205,7 +199,6 @@ var (
}, },
cli.StringFlag{ cli.StringFlag{
Name: "style", Name: "style",
Required: false,
Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]", 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", Name: "go",
Usage: "the file that contains main function", Usage: "the file that contains main function",
}, },
cli.IntFlag{
Name: "port",
Usage: "the port to expose, default none",
Value: 0,
},
}, },
Action: docker.DockerCommand, Action: docker.DockerCommand,
}, },
@@ -322,7 +320,6 @@ var (
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{ cli.StringFlag{
Name: "style", Name: "style",
Required: false,
Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]", Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
}, },
cli.BoolFlag{ cli.BoolFlag{
@@ -361,7 +358,6 @@ var (
}, },
cli.StringFlag{ cli.StringFlag{
Name: "style", Name: "style",
Required: false,
Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]", Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
}, },
cli.BoolFlag{ cli.BoolFlag{
@@ -395,7 +391,6 @@ var (
}, },
cli.StringFlag{ cli.StringFlag{
Name: "style", Name: "style",
Required: false,
Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]", Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
}, },
cli.BoolFlag{ cli.BoolFlag{
@@ -431,7 +426,6 @@ var (
}, },
cli.StringFlag{ cli.StringFlag{
Name: "style", Name: "style",
Required: false,
Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]", Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
}, },
cli.BoolFlag{ cli.BoolFlag{