flag.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "init":
  51. case "g":
  52. err = generateCommandParse()
  53. case "compile":
  54. err = generateCompileParse()
  55. case "serve":
  56. err = generateServeParse()
  57. default:
  58. fmt.Printf("%q is not valid command.\n", Command)
  59. os.Exit(2)
  60. }
  61. return
  62. }
  63. func generateServeParse() (err error) {
  64. compileFlags = flag.NewFlagSet("serve", flag.ExitOnError)
  65. Mode = compileFlags.String("mode", "", "Defines the data source used to compile the project.")
  66. // Mode = compileFlags.String("mode", "", "Defines the data source used to compile the project.")
  67. // BuildProfile = compileFlags.String("build", "", "Profile of build functions.")
  68. compileFlags.Parse(os.Args[2:])
  69. if compileFlags.Parsed() {
  70. return
  71. }
  72. err = fmt.Errorf("Error....")
  73. return
  74. }
  75. func generateCompileParse() (err error) {
  76. compileFlags = flag.NewFlagSet("compile", flag.ExitOnError)
  77. Mode = compileFlags.String("mode", "", "Defines the data source used to compile the project.")
  78. BuildProfile = compileFlags.String("build", "", "Profile of build functions.")
  79. compileFlags.Parse(os.Args[2:])
  80. if compileFlags.Parsed() {
  81. return
  82. }
  83. err = fmt.Errorf("Error....")
  84. return
  85. }
  86. func generateCommandParse() (err error) {
  87. generateFlags = flag.NewFlagSet("g", flag.ExitOnError)
  88. command := os.Args[2]
  89. if fn, exist := GenerateCommandHandles[command]; exist {
  90. if err = fn(generateFlags); err != nil {
  91. return
  92. }
  93. if generateFlags.Parsed() {
  94. return
  95. }
  96. }
  97. err = fmt.Errorf("Error....")
  98. return
  99. }
  100. func handleResourceCommand(generateFlags *flag.FlagSet) (err error) {
  101. Command = "g.resource"
  102. generateFlags.StringVar(&GenerateResourceControl.Entity, "entity", "", "Entity ")
  103. generateFlags.StringVar(&GenerateResourceControl.Methods, "methods", "", "Comma-separated list of resource methods.")
  104. GenerateResourceControl.Name = common.LcFirst(os.Args[3])
  105. generateFlags.Parse(os.Args[4:])
  106. return
  107. }
  108. func handleEntityCommand(generateFlags *flag.FlagSet) (err error) {
  109. Command = "g.entity"
  110. generateFlags.StringVar(&GenerateEntityControl.Type, "type", "object", "Type of entity. [object, nested.object, abstract]")
  111. generateFlags.StringVar(&GenerateEntityControl.Properties, "properties", "", "Comma-separated list of structure properties.")
  112. GenerateEntityControl.Name = common.LcFirst(os.Args[3])
  113. generateFlags.Parse(os.Args[4:])
  114. return
  115. }