func_meta.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 meta
  15. import (
  16. "fmt"
  17. "os"
  18. "path"
  19. "strings"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/internal/pkg/filex"
  22. )
  23. type (
  24. fileFunc struct {
  25. Name string `json:"name"`
  26. Example string `json:"example"`
  27. Hint *fileLanguage `json:"hint"`
  28. Aggregate bool `json:"aggregate"`
  29. ArgsFields []*fileField `json:"args"`
  30. Node *fileNode `json:"node"`
  31. Outputs []interface{} `json:"outputs"`
  32. }
  33. fileFuncs struct {
  34. About *fileAbout `json:"about"`
  35. Name string `json:"name"`
  36. Version string `json:"version"`
  37. FiFuncs []*fileFunc `json:"functions"`
  38. }
  39. uiFunc struct {
  40. Name string `json:"name"`
  41. Example string `json:"example"`
  42. Hint *language `json:"hint"`
  43. Aggregate bool `json:"aggregate"`
  44. ArgsFields []*fileField `json:"args"`
  45. Node *node `json:"node"`
  46. Outputs []interface{} `json:"outputs"`
  47. }
  48. uiFuncs struct {
  49. About *about `json:"about"`
  50. Name string `json:"name"`
  51. Version string `json:"version"`
  52. UiFuncs []*uiFunc `json:"functions"`
  53. }
  54. )
  55. func newUiFuncs(fi *fileFuncs) *uiFuncs {
  56. if nil == fi {
  57. return nil
  58. }
  59. uis := new(uiFuncs)
  60. uis.About = newAbout(fi.About)
  61. uis.Name = fi.Name
  62. for _, v := range fi.FiFuncs {
  63. ui := new(uiFunc)
  64. ui.Name = v.Name
  65. ui.Example = v.Example
  66. ui.Hint = newLanguage(v.Hint)
  67. ui.Aggregate = v.Aggregate
  68. ui.ArgsFields = v.ArgsFields
  69. ui.Node = newNode(v.Node)
  70. ui.Outputs = make([]interface{}, len(v.Outputs))
  71. for k, field := range v.Outputs {
  72. ui.Outputs[k] = field
  73. }
  74. uis.UiFuncs = append(uis.UiFuncs, ui)
  75. }
  76. return uis
  77. }
  78. var gFuncmetadata = make(map[string]*uiFuncs)
  79. func ReadFuncMetaDir(checker InstallChecker) error {
  80. confDir, err := conf.GetConfLoc()
  81. if nil != err {
  82. return err
  83. }
  84. dir := path.Join(confDir, "functions")
  85. files, err := os.ReadDir(dir)
  86. if nil != err {
  87. return err
  88. }
  89. for _, file := range files {
  90. fname := file.Name()
  91. if !strings.HasSuffix(fname, ".json") {
  92. continue
  93. }
  94. if err := ReadFuncMetaFile(path.Join(dir, fname), checker(strings.TrimSuffix(fname, ".json"))); nil != err {
  95. return err
  96. }
  97. }
  98. return nil
  99. }
  100. func UninstallFunc(name string) {
  101. if ui, ok := gFuncmetadata[name+".json"]; ok {
  102. if nil != ui.About {
  103. ui.About.Installed = false
  104. }
  105. }
  106. }
  107. func ReadFuncMetaFile(filePath string, installed bool) error {
  108. fiName := path.Base(filePath)
  109. fis := new(fileFuncs)
  110. err := filex.ReadJsonUnmarshal(filePath, fis)
  111. if nil != err {
  112. return fmt.Errorf("filePath:%s err:%v", filePath, err)
  113. }
  114. if nil == fis.About {
  115. return fmt.Errorf("not found about of %s", filePath)
  116. } else {
  117. fis.About.Installed = installed
  118. }
  119. gFuncmetadata[fiName] = newUiFuncs(fis)
  120. conf.Log.Infof("funcMeta file : %s", fiName)
  121. return nil
  122. }
  123. func GetFunctions() (ret []*uiFuncs) {
  124. for _, v := range gFuncmetadata {
  125. ret = append(ret, v)
  126. }
  127. return ret
  128. }