sourceMeta.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "sync"
  23. )
  24. type (
  25. fileSource struct {
  26. About *fileAbout `json:"about"`
  27. Libs []string `json:"libs"`
  28. ConfKeys map[string][]*fileField `json:"properties"`
  29. }
  30. uiSource struct {
  31. About *about `json:"about"`
  32. Libs []string `json:"libs"`
  33. ConfKeys map[string][]field `json:"properties"`
  34. }
  35. )
  36. func newUiSource(fi *fileSource) (*uiSource, error) {
  37. if nil == fi {
  38. return nil, nil
  39. }
  40. var err error
  41. ui := new(uiSource)
  42. ui.Libs = fi.Libs
  43. ui.About = newAbout(fi.About)
  44. ui.ConfKeys = make(map[string][]field)
  45. for k, fields := range fi.ConfKeys {
  46. if ui.ConfKeys[k], err = newField(fields); nil != err {
  47. return nil, err
  48. }
  49. }
  50. return ui, nil
  51. }
  52. var gSourcemetaLock = sync.RWMutex{}
  53. var gSourcemetadata = make(map[string]*uiSource)
  54. func UninstallSource(name string) {
  55. gSourcemetaLock.RLock()
  56. defer gSourcemetaLock.RUnlock()
  57. if v, ok := gSourcemetadata[name+".json"]; ok {
  58. if nil != v.About {
  59. v.About.Installed = false
  60. }
  61. }
  62. }
  63. func ReadSourceMetaFile(filePath string, installed bool) error {
  64. fileName := path.Base(filePath)
  65. if "mqtt_source.json" == fileName {
  66. fileName = "mqtt.json"
  67. }
  68. ptrMeta := new(fileSource)
  69. _ = filex.ReadJsonUnmarshal(filePath, ptrMeta)
  70. if nil == ptrMeta.About {
  71. return fmt.Errorf("not found about of %s", filePath)
  72. } else {
  73. ptrMeta.About.Installed = installed
  74. }
  75. meta, err := newUiSource(ptrMeta)
  76. if nil != err {
  77. return err
  78. }
  79. gSourcemetaLock.Lock()
  80. gSourcemetadata[fileName] = meta
  81. gSourcemetaLock.Unlock()
  82. loadConfigOperatorForSource(strings.TrimSuffix(fileName, `.json`))
  83. loadConfigOperatorForConnection(strings.TrimSuffix(fileName, `.json`))
  84. return err
  85. }
  86. func ReadSourceMetaDir(checker InstallChecker) error {
  87. confDir, err := conf.GetConfLoc()
  88. if nil != err {
  89. return err
  90. }
  91. dir := path.Join(confDir, "sources")
  92. infos, err := ioutil.ReadDir(dir)
  93. if nil != err {
  94. return err
  95. }
  96. if err = ReadSourceMetaFile(path.Join(confDir, "mqtt_source.json"), true); nil != err {
  97. return err
  98. }
  99. conf.Log.Infof("Loading metadata file for source : %s", "mqtt_source.json")
  100. for _, info := range infos {
  101. fileName := info.Name()
  102. if strings.HasSuffix(fileName, ".json") {
  103. filePath := path.Join(dir, fileName)
  104. if err = ReadSourceMetaFile(filePath, checker(strings.TrimSuffix(fileName, ".json"))); nil != err {
  105. return err
  106. }
  107. conf.Log.Infof("Loading metadata file for source : %s", fileName)
  108. }
  109. }
  110. return nil
  111. }
  112. func GetSourceMeta(sourceName, language string) (ptrSourceProperty *uiSource, err error) {
  113. gSourcemetaLock.RLock()
  114. defer gSourcemetaLock.RUnlock()
  115. v, found := gSourcemetadata[sourceName+`.json`]
  116. if !found {
  117. return nil, fmt.Errorf(`%s%s`, getMsg(language, source, "not_found_plugin"), sourceName)
  118. }
  119. ui := new(uiSource)
  120. *ui = *v
  121. return ui, nil
  122. }
  123. func GetSourcesPlugins() (sources []*pluginfo) {
  124. gSourcemetaLock.RLock()
  125. defer gSourcemetaLock.RUnlock()
  126. for fileName, v := range gSourcemetadata {
  127. node := new(pluginfo)
  128. node.Name = strings.TrimSuffix(fileName, `.json`)
  129. if nil == v {
  130. continue
  131. }
  132. if nil == v.About {
  133. continue
  134. }
  135. node.About = v.About
  136. i := 0
  137. for ; i < len(sources); i++ {
  138. if node.Name <= sources[i].Name {
  139. sources = append(sources, node)
  140. copy(sources[i+1:], sources[i:])
  141. sources[i] = node
  142. break
  143. }
  144. }
  145. if len(sources) == i {
  146. sources = append(sources, node)
  147. }
  148. }
  149. return sources
  150. }