cobrax.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package cobrax
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. "github.com/spf13/pflag"
  6. "github.com/zeromicro/go-zero/tools/goctl/internal/flags"
  7. )
  8. type Option func(*cobra.Command)
  9. func WithRunE(runE func(*cobra.Command, []string) error) Option {
  10. return func(cmd *cobra.Command) {
  11. cmd.RunE = runE
  12. }
  13. }
  14. func WithRun(run func(*cobra.Command, []string)) Option {
  15. return func(cmd *cobra.Command) {
  16. cmd.Run = run
  17. }
  18. }
  19. func WithArgs(arg cobra.PositionalArgs) Option {
  20. return func(command *cobra.Command) {
  21. command.Args = arg
  22. }
  23. }
  24. func WithHidden() Option {
  25. return func(command *cobra.Command) {
  26. command.Hidden = true
  27. }
  28. }
  29. type Command struct {
  30. *cobra.Command
  31. }
  32. type FlagSet struct {
  33. *pflag.FlagSet
  34. }
  35. func (f *FlagSet) StringVar(p *string, name string) {
  36. f.StringVarWithDefaultValue(p, name, "")
  37. }
  38. func (f *FlagSet) StringVarWithDefaultValue(p *string, name string, value string) {
  39. f.FlagSet.StringVar(p, name, value, "")
  40. }
  41. func (f *FlagSet) StringVarP(p *string, name, shorthand string) {
  42. f.StringVarPWithDefaultValue(p, name, shorthand, "")
  43. }
  44. func (f *FlagSet) StringVarPWithDefaultValue(p *string, name, shorthand string, value string) {
  45. f.FlagSet.StringVarP(p, name, shorthand, value, "")
  46. }
  47. func (f *FlagSet) BoolVar(p *bool, name string) {
  48. f.BoolVarWithDefaultValue(p, name, false)
  49. }
  50. func (f *FlagSet) BoolVarWithDefaultValue(p *bool, name string, value bool) {
  51. f.FlagSet.BoolVar(p, name, value, "")
  52. }
  53. func (f *FlagSet) BoolVarP(p *bool, name, shorthand string) {
  54. f.BoolVarPWithDefaultValue(p, name, shorthand, false)
  55. }
  56. func (f *FlagSet) BoolVarPWithDefaultValue(p *bool, name, shorthand string, value bool) {
  57. f.FlagSet.BoolVarP(p, name, shorthand, value, "")
  58. }
  59. func (f *FlagSet) IntVar(p *int, name string) {
  60. f.IntVarWithDefaultValue(p, name, 0)
  61. }
  62. func (f *FlagSet) IntVarWithDefaultValue(p *int, name string, value int) {
  63. f.FlagSet.IntVar(p, name, value, "")
  64. }
  65. func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string) {
  66. f.FlagSet.StringSliceVarP(p, name, shorthand, []string{}, "")
  67. }
  68. func (f *FlagSet) StringSliceVarPWithDefaultValue(p *[]string, name, shorthand string, value []string) {
  69. f.FlagSet.StringSliceVarP(p, name, shorthand, value, "")
  70. }
  71. func (f *FlagSet) StringSliceVar(p *[]string, name string) {
  72. f.StringSliceVarWithDefaultValue(p, name, []string{})
  73. }
  74. func (f *FlagSet) StringSliceVarWithDefaultValue(p *[]string, name string, value []string) {
  75. f.FlagSet.StringSliceVar(p, name, value, "")
  76. }
  77. func NewCommand(use string, opts ...Option) *Command {
  78. c := &Command{
  79. Command: &cobra.Command{
  80. Use: use,
  81. },
  82. }
  83. for _, opt := range opts {
  84. opt(c.Command)
  85. }
  86. return c
  87. }
  88. func (c *Command) AddCommand(cmds ...*Command) {
  89. for _, cmd := range cmds {
  90. c.Command.AddCommand(cmd.Command)
  91. }
  92. }
  93. func (c *Command) Flags() *FlagSet {
  94. set := c.Command.Flags()
  95. return &FlagSet{
  96. FlagSet: set,
  97. }
  98. }
  99. func (c *Command) PersistentFlags() *FlagSet {
  100. set := c.Command.PersistentFlags()
  101. return &FlagSet{
  102. FlagSet: set,
  103. }
  104. }
  105. func (c *Command) MustInit() {
  106. commands := append([]*cobra.Command{c.Command}, getCommandsRecursively(c.Command)...)
  107. for _, command := range commands {
  108. commandKey := getCommandName(command)
  109. if len(command.Short) == 0 {
  110. command.Short = flags.Get(commandKey + ".short")
  111. }
  112. if len(command.Long) == 0 {
  113. command.Long = flags.Get(commandKey + ".long")
  114. }
  115. if len(command.Example) == 0 {
  116. command.Example = flags.Get(commandKey + ".example")
  117. }
  118. command.Flags().VisitAll(func(flag *pflag.Flag) {
  119. flag.Usage = flags.Get(fmt.Sprintf("%s.%s", commandKey, flag.Name))
  120. })
  121. command.PersistentFlags().VisitAll(func(flag *pflag.Flag) {
  122. flag.Usage = flags.Get(fmt.Sprintf("%s.%s", commandKey, flag.Name))
  123. })
  124. }
  125. }
  126. func getCommandName(cmd *cobra.Command) string {
  127. if cmd.HasParent() {
  128. return getCommandName(cmd.Parent()) + "." + cmd.Name()
  129. }
  130. return cmd.Name()
  131. }
  132. func getCommandsRecursively(parent *cobra.Command) []*cobra.Command {
  133. var commands []*cobra.Command
  134. for _, cmd := range parent.Commands() {
  135. commands = append(commands, cmd)
  136. commands = append(commands, getCommandsRecursively(cmd)...)
  137. }
  138. return commands
  139. }