common.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package commands
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/flag"
  7. )
  8. const (
  9. EntitiesPath = "entities"
  10. ResourcesPath = "resources"
  11. EnvironmentPath = "env"
  12. )
  13. var (
  14. upperCaseRegex = regexp.MustCompile(`(\s*([A-Z]))`)
  15. Commands = map[string]func() error{
  16. "init": initialize,
  17. "compile": compile,
  18. "serve": serve,
  19. "g.entity": generateEntity,
  20. "g.resource": generateResource,
  21. }
  22. basePathFormats = map[bool]string{false: "./%s", true: "./%s/%s"}
  23. )
  24. func ExecuteWithFlags() (err error) {
  25. if err = flag.Initialize(); err != nil {
  26. return
  27. }
  28. command := flag.Command
  29. if callback, exist := Commands[command]; exist {
  30. err = callback()
  31. return
  32. }
  33. err = fmt.Errorf("The command '%s' is not valid", command)
  34. return
  35. }
  36. func basePath() string {
  37. return basePathWithComplement("")
  38. }
  39. func basePathWithComplement(complement string) string {
  40. return fmt.Sprintf(
  41. basePathFormats[complement != ""],
  42. project.ID,
  43. complement,
  44. )
  45. }
  46. func normalizePath(path string) string {
  47. path = upperCaseRegex.ReplaceAllStringFunc(path, func(m string) string {
  48. return fmt.Sprintf("-%s", strings.ToLower(strings.Trim(m, " ")))
  49. })
  50. path = strings.ReplaceAll(path, " ", "-")
  51. path = strings.Trim(path, "-")
  52. return path
  53. }