plugin.go 3.5 KB

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