add EXPOSE in dockerfile generation (#281)

master
Kevin Wan 4 years ago committed by GitHub
parent b56cc8e459
commit 4d13dda605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

@ -54,14 +54,12 @@ var (
Usage: "the format target dir", Usage: "the format target dir",
}, },
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,
@ -101,9 +99,8 @@ var (
Usage: "the api file", Usage: "the api file",
}, },
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]",
}, },
}, },
Action: gogen.GoCommand, Action: gogen.GoCommand,
@ -136,19 +133,16 @@ var (
Usage: "the api file", Usage: "the api file",
}, },
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,
@ -204,9 +198,8 @@ var (
Usage: "the api file", Usage: "the api file",
}, },
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]",
}, },
}, },
Action: plugin.PluginCommand, Action: plugin.PluginCommand,
@ -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,
}, },
@ -321,9 +319,8 @@ var (
Usage: `generate rpc demo service`, Usage: `generate rpc demo service`,
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{
Name: "idea", Name: "idea",
@ -360,9 +357,8 @@ var (
Usage: `the target path of the code`, Usage: `the target path of the code`,
}, },
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{
Name: "idea", Name: "idea",
@ -394,9 +390,8 @@ var (
Usage: "the target dir", Usage: "the target dir",
}, },
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{
Name: "cache, c", Name: "cache, c",
@ -430,9 +425,8 @@ var (
Usage: "the target dir", Usage: "the target dir",
}, },
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{
Name: "idea", Name: "idea",

Loading…
Cancel
Save