flag.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package flag
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api"
  7. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  8. )
  9. var (
  10. GenerateCommandHandles = map[string]func(*flag.FlagSet) error{
  11. "entity": handleEntityCommand,
  12. "resource": handleResourceCommand,
  13. }
  14. Command string
  15. Mode, Out, BuildProfile *string
  16. String = flag.String
  17. Bool = flag.Bool
  18. Parse = flag.Parse
  19. Arg = flag.Arg
  20. Args = flag.Args
  21. NewFlagSet = flag.NewFlagSet
  22. ExitOnError = flag.ExitOnError
  23. PanicOnError = flag.PanicOnError
  24. ContinueOnError = flag.ContinueOnError
  25. GenerateEntityControl = struct {
  26. Name string
  27. Type string
  28. Properties string
  29. }{}
  30. GenerateResourceControl = struct {
  31. Name string
  32. Entity string
  33. Methods string
  34. }{}
  35. compileFlags, generateFlags *flag.FlagSet
  36. )
  37. type FlagSet = flag.FlagSet
  38. func Initialize() (err error) {
  39. defer func() {
  40. if err != nil {
  41. api.LogInfo(0, "eon [init|compile|serve] ...options")
  42. api.LogError(0, err.Error())
  43. flag.PrintDefaults()
  44. }
  45. }()
  46. Out = flag.String("out", "../build", "Target directory of the compilation.")
  47. // todo add list of valid commands on erro
  48. Command = os.Args[1]
  49. switch Command {
  50. case "g":
  51. err = generateCommandParse()
  52. case "compile":
  53. err = generateCompileParse()
  54. case "serve":
  55. err = generateServeParse()
  56. default:
  57. fmt.Printf("%q is not valid command.\n", Command)
  58. os.Exit(2)
  59. }
  60. return
  61. }
  62. func generateServeParse() (err error) {
  63. compileFlags = flag.NewFlagSet("serve", flag.ExitOnError)
  64. Mode = compileFlags.String("mode", "", "Defines the data source used to compile the project.")
  65. // Mode = compileFlags.String("mode", "", "Defines the data source used to compile the project.")
  66. // BuildProfile = compileFlags.String("build", "", "Profile of build functions.")
  67. compileFlags.Parse(os.Args[2:])
  68. if compileFlags.Parsed() {
  69. return
  70. }
  71. err = fmt.Errorf("Error....")
  72. return
  73. }
  74. func generateCompileParse() (err error) {
  75. compileFlags = flag.NewFlagSet("compile", flag.ExitOnError)
  76. Mode = compileFlags.String("mode", "", "Defines the data source used to compile the project.")
  77. BuildProfile = compileFlags.String("build", "", "Profile of build functions.")
  78. compileFlags.Parse(os.Args[2:])
  79. if compileFlags.Parsed() {
  80. return
  81. }
  82. err = fmt.Errorf("Error....")
  83. return
  84. }
  85. func generateCommandParse() (err error) {
  86. generateFlags = flag.NewFlagSet("g", flag.ExitOnError)
  87. command := os.Args[2]
  88. if fn, exist := GenerateCommandHandles[command]; exist {
  89. if err = fn(generateFlags); err != nil {
  90. return
  91. }
  92. if generateFlags.Parsed() {
  93. return
  94. }
  95. }
  96. err = fmt.Errorf("Error....")
  97. return
  98. }
  99. func handleResourceCommand(generateFlags *flag.FlagSet) (err error) {
  100. Command = "g.resource"
  101. generateFlags.StringVar(&GenerateResourceControl.Entity, "entity", "", "Entity ")
  102. generateFlags.StringVar(&GenerateResourceControl.Methods, "methods", "", "Comma-separated list of resource methods.")
  103. GenerateResourceControl.Name = common.LcFirst(os.Args[3])
  104. generateFlags.Parse(os.Args[4:])
  105. return
  106. }
  107. func handleEntityCommand(generateFlags *flag.FlagSet) (err error) {
  108. Command = "g.entity"
  109. generateFlags.StringVar(&GenerateEntityControl.Type, "type", "object", "Type of entity. [object, nested.object, abstract]")
  110. generateFlags.StringVar(&GenerateEntityControl.Properties, "properties", "", "Comma-separated list of structure properties.")
  111. GenerateEntityControl.Name = common.LcFirst(os.Args[3])
  112. generateFlags.Parse(os.Args[4:])
  113. return
  114. }