docker.go 1007 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package docker
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/tal-tech/go-zero/tools/goctl/gen"
  8. "github.com/urfave/cli"
  9. )
  10. const (
  11. etcDir = "etc"
  12. yamlEtx = ".yaml"
  13. )
  14. func DockerCommand(c *cli.Context) error {
  15. goFile := c.String("go")
  16. if len(goFile) == 0 {
  17. return errors.New("-go can't be empty")
  18. }
  19. cfg, err := findConfig(goFile, etcDir)
  20. if err != nil {
  21. return err
  22. }
  23. return gen.GenerateDockerfile(goFile, "-f", "etc/"+cfg)
  24. }
  25. func findConfig(file, dir string) (string, error) {
  26. var files []string
  27. err := filepath.Walk(dir, func(path string, f os.FileInfo, _ error) error {
  28. if !f.IsDir() {
  29. if filepath.Ext(f.Name()) == yamlEtx {
  30. files = append(files, f.Name())
  31. }
  32. }
  33. return nil
  34. })
  35. if err != nil {
  36. return "", err
  37. }
  38. if len(files) == 0 {
  39. return "", errors.New("no yaml file")
  40. }
  41. name := strings.TrimSuffix(filepath.Base(file), ".go")
  42. for _, f := range files {
  43. if strings.Index(f, name) == 0 {
  44. return f, nil
  45. }
  46. }
  47. return files[0], nil
  48. }