func_meta.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "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 newUiFuncs(fi *fileFuncs) *uiFuncs {
  46. if nil == fi {
  47. return nil
  48. }
  49. uis := new(uiFuncs)
  50. uis.About = newAbout(fi.About)
  51. uis.Name = fi.Name
  52. for _, v := range fi.FiFuncs {
  53. ui := new(uiFunc)
  54. ui.Name = v.Name
  55. ui.Example = v.Example
  56. ui.Hint = newLanguage(v.Hint)
  57. uis.UiFuncs = append(uis.UiFuncs, ui)
  58. }
  59. return uis
  60. }
  61. var gFuncmetadata = make(map[string]*uiFuncs)
  62. func ReadFuncMetaDir(checker InstallChecker) error {
  63. confDir, err := conf.GetConfLoc()
  64. if nil != err {
  65. return err
  66. }
  67. dir := path.Join(confDir, "functions")
  68. files, err := ioutil.ReadDir(dir)
  69. if nil != err {
  70. return err
  71. }
  72. for _, file := range files {
  73. fname := file.Name()
  74. if !strings.HasSuffix(fname, ".json") {
  75. continue
  76. }
  77. if err := ReadFuncMetaFile(path.Join(dir, fname), checker(strings.TrimSuffix(fname, ".json"))); nil != err {
  78. return err
  79. }
  80. }
  81. return nil
  82. }
  83. func UninstallFunc(name string) {
  84. if ui, ok := gFuncmetadata[name+".json"]; ok {
  85. if nil != ui.About {
  86. ui.About.Installed = false
  87. }
  88. }
  89. }
  90. func ReadFuncMetaFile(filePath string, installed bool) error {
  91. fiName := path.Base(filePath)
  92. fis := new(fileFuncs)
  93. err := filex.ReadJsonUnmarshal(filePath, fis)
  94. if nil != err {
  95. return fmt.Errorf("filePath:%s err:%v", filePath, err)
  96. }
  97. if nil == fis.About {
  98. return fmt.Errorf("not found about of %s", filePath)
  99. } else {
  100. fis.About.Installed = installed
  101. }
  102. gFuncmetadata[fiName] = newUiFuncs(fis)
  103. conf.Log.Infof("funcMeta file : %s", fiName)
  104. return nil
  105. }
  106. func GetFunctions() (ret []*uiFuncs) {
  107. for _, v := range gFuncmetadata {
  108. ret = append(ret, v)
  109. }
  110. return ret
  111. }