diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index f3098fda..0c97f63e 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -2,16 +2,58 @@ package docker import ( "errors" + "os" + "path/filepath" + "strings" "github.com/tal-tech/go-zero/tools/goctl/gen" "github.com/urfave/cli" ) +const ( + etcDir = "etc" + yamlEtx = ".yaml" +) + func DockerCommand(c *cli.Context) error { goFile := c.String("go") if len(goFile) == 0 { return errors.New("-go can't be empty") } - return gen.GenerateDockerfile(goFile, "-f", "etc/config.yaml") + cfg, err := findConfig(goFile, etcDir) + if err != nil { + return err + } + + return gen.GenerateDockerfile(goFile, "-f", "etc/"+cfg) +} + +func findConfig(file, dir string) (string, error) { + var files []string + err := filepath.Walk(dir, func(path string, f os.FileInfo, _ error) error { + if !f.IsDir() { + if filepath.Ext(f.Name()) == yamlEtx { + files = append(files, f.Name()) + } + } + + return nil + }) + if err != nil { + return "", err + } + + if len(files) == 0 { + return "", errors.New("no yaml file") + } + + name := strings.TrimSuffix(filepath.Base(file), ".go") + for _, f := range files { + if strings.Index(f, name) == 0 { + return f, nil + } + } + + return files[0], nil } diff --git a/tools/goctl/gen/dockerfile.go b/tools/goctl/gen/dockerfile.go index 0e821063..6523b5be 100644 --- a/tools/goctl/gen/dockerfile.go +++ b/tools/goctl/gen/dockerfile.go @@ -6,7 +6,6 @@ import ( "text/template" "github.com/tal-tech/go-zero/tools/goctl/util" - "github.com/tal-tech/go-zero/tools/goctl/vars" ) func GenerateDockerfile(goFile string, args ...string) error { @@ -33,10 +32,9 @@ func GenerateDockerfile(goFile string, args ...string) error { t := template.Must(template.New("dockerfile").Parse(dockerTemplate)) return t.Execute(out, map[string]string{ - "projectName": vars.ProjectName, - "goRelPath": projPath, - "goFile": goFile, - "exeFile": util.FileNameWithoutExt(goFile), - "argument": builder.String(), + "goRelPath": projPath, + "goFile": goFile, + "exeFile": util.FileNameWithoutExt(filepath.Base(goFile)), + "argument": builder.String(), }) } diff --git a/tools/goctl/gen/template.go b/tools/goctl/gen/template.go index d3086fb2..ce79bb23 100644 --- a/tools/goctl/gen/template.go +++ b/tools/goctl/gen/template.go @@ -8,8 +8,9 @@ ENV CGO_ENABLED 0 ENV GOOS linux ENV GOPROXY https://goproxy.cn,direct -WORKDIR $GOPATH/src/{{.projectName}} +WORKDIR /build/zero COPY . . +COPY {{.goRelPath}}/etc /app/etc RUN go build -ldflags="-s -w" -o /app/{{.exeFile}} {{.goRelPath}}/{{.goFile}} @@ -22,6 +23,7 @@ ENV TZ Asia/Shanghai WORKDIR /app COPY --from=builder /app/{{.exeFile}} /app/{{.exeFile}} +COPY --from=builder /app/etc /app/etc CMD ["./{{.exeFile}}"{{.argument}}] ` diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 171b1a68..74ee6f12 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -25,7 +25,7 @@ import ( ) var ( - BuildVersion = "20201021" + BuildVersion = "20201108" commands = []cli.Command{ { Name: "api", @@ -188,16 +188,12 @@ var ( }, { Name: "docker", - Usage: "generate Dockerfile and Makefile", + Usage: "generate Dockerfile", Flags: []cli.Flag{ cli.StringFlag{ Name: "go", Usage: "the file that contains main function", }, - cli.StringFlag{ - Name: "namespace, n", - Usage: "which namespace of kubernetes to deploy the service", - }, }, Action: docker.DockerCommand, },