funcMeta.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package plugin
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/pkg/filex"
  19. "io/ioutil"
  20. "path"
  21. "strings"
  22. )
  23. type (
  24. fileFunc struct {
  25. Name string `json:"name"`
  26. Example string `json:"example"`
  27. Hint *fileLanguage `json:"hint"`
  28. }
  29. fileFuncs struct {
  30. About *fileAbout `json:"about"`
  31. Name string `json:"name"`
  32. FiFuncs []*fileFunc `json:"functions"`
  33. }
  34. uiFunc struct {
  35. Name string `json:"name"`
  36. Example string `json:"example"`
  37. Hint *language `json:"hint"`
  38. }
  39. uiFuncs struct {
  40. About *about `json:"about"`
  41. Name string `json:"name"`
  42. UiFuncs []*uiFunc `json:"functions"`
  43. }
  44. )
  45. func isInternalFunc(fiName string) bool {
  46. internal := []string{`accumulateWordCount.json`, `countPlusOne.json`, `echo.json`, `internal.json`, "windows.json", "image.json", "geohash.json"}
  47. for _, v := range internal {
  48. if v == fiName {
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. func newUiFuncs(fi *fileFuncs) *uiFuncs {
  55. if nil == fi {
  56. return nil
  57. }
  58. uis := new(uiFuncs)
  59. uis.About = newAbout(fi.About)
  60. uis.Name = fi.Name
  61. for _, v := range fi.FiFuncs {
  62. ui := new(uiFunc)
  63. ui.Name = v.Name
  64. ui.Example = v.Example
  65. ui.Hint = newLanguage(v.Hint)
  66. uis.UiFuncs = append(uis.UiFuncs, ui)
  67. }
  68. return uis
  69. }
  70. var gFuncmetadata map[string]*uiFuncs
  71. func (m *Manager) readFuncMetaDir() error {
  72. gFuncmetadata = make(map[string]*uiFuncs)
  73. confDir, err := conf.GetConfLoc()
  74. if nil != err {
  75. return err
  76. }
  77. dir := path.Join(confDir, "functions")
  78. files, err := ioutil.ReadDir(dir)
  79. if nil != err {
  80. return err
  81. }
  82. for _, file := range files {
  83. fname := file.Name()
  84. if !strings.HasSuffix(fname, ".json") {
  85. continue
  86. }
  87. if err := m.readFuncMetaFile(path.Join(dir, fname)); nil != err {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. func (m *Manager) uninstalFunc(name string) {
  94. if ui, ok := gFuncmetadata[name+".json"]; ok {
  95. if nil != ui.About {
  96. ui.About.Installed = false
  97. }
  98. }
  99. }
  100. func (m *Manager) readFuncMetaFile(filePath string) error {
  101. fiName := path.Base(filePath)
  102. fis := new(fileFuncs)
  103. err := filex.ReadJsonUnmarshal(filePath, fis)
  104. if nil != err {
  105. return fmt.Errorf("filePath:%s err:%v", filePath, err)
  106. }
  107. if nil == fis.About {
  108. return fmt.Errorf("not found about of %s", filePath)
  109. } else if isInternalFunc(fiName) {
  110. fis.About.Installed = true
  111. } else {
  112. _, fis.About.Installed = m.registry.Get(FUNCTION, strings.TrimSuffix(fiName, `.json`))
  113. }
  114. gFuncmetadata[fiName] = newUiFuncs(fis)
  115. conf.Log.Infof("funcMeta file : %s", fiName)
  116. return nil
  117. }
  118. func GetFunctions() (ret []*uiFuncs) {
  119. for _, v := range gFuncmetadata {
  120. ret = append(ret, v)
  121. }
  122. return ret
  123. }