funcMeta.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package plugins
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/common"
  5. "io/ioutil"
  6. "path"
  7. "strings"
  8. )
  9. type (
  10. fileFunc struct {
  11. Name string `json:"name"`
  12. Example string `json:"example"`
  13. Hint *fileLanguage `json:"hint"`
  14. }
  15. fileFuncs struct {
  16. About *fileAbout `json:"about"`
  17. FiFuncs []*fileFunc `json:"functions"`
  18. }
  19. uiFunc struct {
  20. Name string `json:"name"`
  21. Example string `json:"example"`
  22. Hint *language `json:"hint"`
  23. }
  24. uiFuncs struct {
  25. About *about `json:"about"`
  26. UiFuncs []*uiFunc `json:"functions"`
  27. }
  28. )
  29. func newUiFuncs(fi *fileFuncs) *uiFuncs {
  30. if nil == fi {
  31. return nil
  32. }
  33. uis := new(uiFuncs)
  34. uis.About = newAbout(fi.About)
  35. for _, v := range fi.FiFuncs {
  36. ui := new(uiFunc)
  37. ui.Name = v.Name
  38. ui.Example = v.Example
  39. ui.Hint = newLanguage(v.Hint)
  40. uis.UiFuncs = append(uis.UiFuncs, ui)
  41. }
  42. return uis
  43. }
  44. var g_funcMetadata []*uiFuncs
  45. func (m *Manager) readFuncMetaDir() error {
  46. confDir, err := common.GetConfLoc()
  47. if nil != err {
  48. return err
  49. }
  50. dir := path.Join(confDir, "functions")
  51. files, err := ioutil.ReadDir(dir)
  52. if nil != err {
  53. return err
  54. }
  55. for _, file := range files {
  56. fname := file.Name()
  57. if !strings.HasSuffix(fname, ".json") {
  58. continue
  59. }
  60. filePath := path.Join(dir, fname)
  61. fis := new(fileFuncs)
  62. err = common.ReadJsonUnmarshal(filePath, fis)
  63. if nil != err {
  64. return fmt.Errorf("fname:%s err:%v", fname, err)
  65. }
  66. if nil == fis.About {
  67. return fmt.Errorf("not found about of %s", filePath)
  68. } else {
  69. _, fis.About.Installed = m.registry.Get(FUNCTION, strings.TrimSuffix(fname, `.json`))
  70. }
  71. common.Log.Infof("funcMeta file : %s", fname)
  72. g_funcMetadata = append(g_funcMetadata, newUiFuncs(fis))
  73. }
  74. return nil
  75. }
  76. func (m *Manager) readFuncMetaFile(filePath string) error {
  77. fiName := path.Base(filePath)
  78. fis := new(fileFuncs)
  79. err := common.ReadJsonUnmarshal(filePath, fis)
  80. if nil != err {
  81. return fmt.Errorf("filePath:%s err:%v", filePath, err)
  82. }
  83. if nil == fis.About {
  84. return fmt.Errorf("not found about of %s", filePath)
  85. } else {
  86. _, fis.About.Installed = m.registry.Get(FUNCTION, strings.TrimSuffix(fiName, `.json`))
  87. }
  88. g_funcMetadata = append(g_funcMetadata, newUiFuncs(fis))
  89. common.Log.Infof("funcMeta file : %s", fiName)
  90. return nil
  91. }
  92. func GetFunctions() []*uiFuncs {
  93. return g_funcMetadata
  94. }