Skip to content

Commit

Permalink
add EXPOSE in dockerfile generation (zeromicro#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Dec 12, 2020
1 parent b56cc8e commit 4d13dda
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
11 changes: 8 additions & 3 deletions tools/goctl/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Docker struct {
GoRelPath string
GoFile string
ExeFile string
HasPort bool
Port int
HasArgs bool
Argument string
}
Expand All @@ -41,16 +43,17 @@ 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)
if err != nil {
return err
}

if err := generateDockerfile(goFile, "-f", "etc/"+cfg); err != nil {
if err := generateDockerfile(goFile, port, "-f", "etc/"+cfg); err != nil {
return err
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
})
Expand Down
4 changes: 3 additions & 1 deletion tools/goctl/docker/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}]
`
Expand Down
60 changes: 27 additions & 33 deletions tools/goctl/goctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 4d13dda

Please sign in to comment.