go-zero/tools/goctl/docker/docker.go

60 lines
1007 B
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package docker
import (
"errors"
2020-11-08 21:28:58 +08:00
"os"
"path/filepath"
"strings"
2020-07-29 17:11:41 +08:00
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/tools/goctl/gen"
2020-07-29 17:11:41 +08:00
"github.com/urfave/cli"
)
2020-11-08 21:28:58 +08:00
const (
etcDir = "etc"
yamlEtx = ".yaml"
)
2020-07-29 17:11:41 +08:00
func DockerCommand(c *cli.Context) error {
goFile := c.String("go")
2020-08-30 23:52:51 +08:00
if len(goFile) == 0 {
return errors.New("-go can't be empty")
2020-07-29 17:11:41 +08:00
}
2020-11-08 21:28:58 +08:00
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
2020-07-29 17:11:41 +08:00
}