add dockerfile generator
This commit is contained in:
@@ -2,16 +2,58 @@ package docker
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/gen"
|
"github.com/tal-tech/go-zero/tools/goctl/gen"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
etcDir = "etc"
|
||||||
|
yamlEtx = ".yaml"
|
||||||
|
)
|
||||||
|
|
||||||
func DockerCommand(c *cli.Context) error {
|
func DockerCommand(c *cli.Context) error {
|
||||||
goFile := c.String("go")
|
goFile := c.String("go")
|
||||||
if len(goFile) == 0 {
|
if len(goFile) == 0 {
|
||||||
return errors.New("-go can't be empty")
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
"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 {
|
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))
|
t := template.Must(template.New("dockerfile").Parse(dockerTemplate))
|
||||||
return t.Execute(out, map[string]string{
|
return t.Execute(out, map[string]string{
|
||||||
"projectName": vars.ProjectName,
|
"goRelPath": projPath,
|
||||||
"goRelPath": projPath,
|
"goFile": goFile,
|
||||||
"goFile": goFile,
|
"exeFile": util.FileNameWithoutExt(filepath.Base(goFile)),
|
||||||
"exeFile": util.FileNameWithoutExt(goFile),
|
"argument": builder.String(),
|
||||||
"argument": builder.String(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ ENV CGO_ENABLED 0
|
|||||||
ENV GOOS linux
|
ENV GOOS linux
|
||||||
ENV GOPROXY https://goproxy.cn,direct
|
ENV GOPROXY https://goproxy.cn,direct
|
||||||
|
|
||||||
WORKDIR $GOPATH/src/{{.projectName}}
|
WORKDIR /build/zero
|
||||||
COPY . .
|
COPY . .
|
||||||
|
COPY {{.goRelPath}}/etc /app/etc
|
||||||
RUN go build -ldflags="-s -w" -o /app/{{.exeFile}} {{.goRelPath}}/{{.goFile}}
|
RUN go build -ldflags="-s -w" -o /app/{{.exeFile}} {{.goRelPath}}/{{.goFile}}
|
||||||
|
|
||||||
|
|
||||||
@@ -22,6 +23,7 @@ ENV TZ Asia/Shanghai
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=builder /app/{{.exeFile}} /app/{{.exeFile}}
|
COPY --from=builder /app/{{.exeFile}} /app/{{.exeFile}}
|
||||||
|
COPY --from=builder /app/etc /app/etc
|
||||||
|
|
||||||
CMD ["./{{.exeFile}}"{{.argument}}]
|
CMD ["./{{.exeFile}}"{{.argument}}]
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
BuildVersion = "20201021"
|
BuildVersion = "20201108"
|
||||||
commands = []cli.Command{
|
commands = []cli.Command{
|
||||||
{
|
{
|
||||||
Name: "api",
|
Name: "api",
|
||||||
@@ -188,16 +188,12 @@ var (
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "docker",
|
Name: "docker",
|
||||||
Usage: "generate Dockerfile and Makefile",
|
Usage: "generate Dockerfile",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "go",
|
Name: "go",
|
||||||
Usage: "the file that contains main function",
|
Usage: "the file that contains main function",
|
||||||
},
|
},
|
||||||
cli.StringFlag{
|
|
||||||
Name: "namespace, n",
|
|
||||||
Usage: "which namespace of kubernetes to deploy the service",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Action: docker.DockerCommand,
|
Action: docker.DockerCommand,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user