plugin.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package plugin
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. "strings"
  14. "git.i2edu.net/i2/go-zero/tools/goctl/api/parser"
  15. "git.i2edu.net/i2/go-zero/tools/goctl/api/spec"
  16. "git.i2edu.net/i2/go-zero/tools/goctl/rpc/execx"
  17. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  18. "github.com/urfave/cli"
  19. )
  20. const pluginArg = "_plugin"
  21. // Plugin defines an api plugin
  22. type Plugin struct {
  23. Api *spec.ApiSpec
  24. ApiFilePath string
  25. Style string
  26. Dir string
  27. }
  28. // PluginCommand is the entry of goctl api plugin
  29. func PluginCommand(c *cli.Context) error {
  30. ex, err := os.Executable()
  31. if err != nil {
  32. panic(err)
  33. }
  34. plugin := c.String("plugin")
  35. if len(plugin) == 0 {
  36. return errors.New("missing plugin")
  37. }
  38. transferData, err := prepareArgs(c)
  39. if err != nil {
  40. return err
  41. }
  42. bin, args := getPluginAndArgs(plugin)
  43. bin, download, err := getCommand(bin)
  44. if err != nil {
  45. return err
  46. }
  47. if download {
  48. defer func() {
  49. _ = os.Remove(bin)
  50. }()
  51. }
  52. content, err := execx.Run(bin+" "+args, filepath.Dir(ex), bytes.NewBuffer(transferData))
  53. if err != nil {
  54. return err
  55. }
  56. fmt.Println(content)
  57. return nil
  58. }
  59. func prepareArgs(c *cli.Context) ([]byte, error) {
  60. apiPath := c.String("api")
  61. var transferData Plugin
  62. if len(apiPath) > 0 && util.FileExists(apiPath) {
  63. api, err := parser.Parse(apiPath)
  64. if err != nil {
  65. return nil, err
  66. }
  67. transferData.Api = api
  68. }
  69. absApiFilePath, err := filepath.Abs(apiPath)
  70. if err != nil {
  71. return nil, err
  72. }
  73. transferData.ApiFilePath = absApiFilePath
  74. dirAbs, err := filepath.Abs(c.String("dir"))
  75. if err != nil {
  76. return nil, err
  77. }
  78. transferData.Dir = dirAbs
  79. transferData.Style = c.String("style")
  80. data, err := json.Marshal(transferData)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return data, nil
  85. }
  86. func getCommand(arg string) (string, bool, error) {
  87. p, err := exec.LookPath(arg)
  88. if err == nil {
  89. abs, err := filepath.Abs(p)
  90. if err != nil {
  91. return "", false, err
  92. }
  93. return abs, false, nil
  94. }
  95. defaultErr := errors.New("invalid plugin value " + arg)
  96. if strings.HasPrefix(arg, "http") {
  97. items := strings.Split(arg, "/")
  98. if len(items) == 0 {
  99. return "", false, defaultErr
  100. }
  101. filename, err := filepath.Abs(pluginArg + items[len(items)-1])
  102. if err != nil {
  103. return "", false, err
  104. }
  105. err = downloadFile(filename, arg)
  106. if err != nil {
  107. return "", false, err
  108. }
  109. os.Chmod(filename, os.ModePerm)
  110. return filename, true, nil
  111. }
  112. return arg, false, nil
  113. }
  114. func downloadFile(filepath, url string) error {
  115. resp, err := http.Get(url)
  116. if err != nil {
  117. return err
  118. }
  119. defer func() {
  120. _ = resp.Body.Close()
  121. }()
  122. out, err := os.Create(filepath)
  123. if err != nil {
  124. return err
  125. }
  126. defer func() {
  127. _ = out.Close()
  128. }()
  129. _, err = io.Copy(out, resp.Body)
  130. return err
  131. }
  132. // NewPlugin returns contextual resources when written in other languages
  133. func NewPlugin() (*Plugin, error) {
  134. var plugin Plugin
  135. content, err := ioutil.ReadAll(os.Stdin)
  136. if err != nil {
  137. return nil, err
  138. }
  139. var info struct {
  140. ApiFilePath string
  141. Style string
  142. Dir string
  143. }
  144. err = json.Unmarshal(content, &info)
  145. if err != nil {
  146. return nil, err
  147. }
  148. plugin.ApiFilePath = info.ApiFilePath
  149. plugin.Style = info.Style
  150. plugin.Dir = info.Dir
  151. api, err := parser.Parse(info.ApiFilePath)
  152. if err != nil {
  153. return nil, err
  154. }
  155. plugin.Api = api
  156. return &plugin, nil
  157. }
  158. func getPluginAndArgs(arg string) (string, string) {
  159. i := strings.Index(arg, "=")
  160. if i <= 0 {
  161. return arg, ""
  162. }
  163. return trimQuote(arg[:i]), trimQuote(arg[i+1:])
  164. }
  165. func trimQuote(in string) string {
  166. in = strings.Trim(in, `"`)
  167. in = strings.Trim(in, `'`)
  168. in = strings.Trim(in, "`")
  169. return in
  170. }