Ver código fonte

feat():metadata file path (#458)

* feat():metadata file path

myd

* feat():format code
EMQmyd 4 anos atrás
pai
commit
523eb4dd3e
3 arquivos alterados com 32 adições e 26 exclusões
  1. 19 12
      plugins/manager.go
  2. 2 2
      plugins/sinkMeta.go
  3. 11 12
      plugins/sourceMeta.go

+ 19 - 12
plugins/manager.go

@@ -212,10 +212,8 @@ func NewPluginManager() (*Manager, error) {
 			etcDir:    etcDir,
 			registry:  registry,
 		}
-		outerErr = readSourceMetaDir()
-		if nil == err {
-			outerErr = readSinkMetaDir()
-		}
+		readSourceMetaDir()
+		readSinkMetaDir()
 	})
 	return singleton, outerErr
 }
@@ -278,7 +276,15 @@ func (m *Manager) Register(t PluginType, j *Plugin) error {
 		}
 		return fmt.Errorf("fail to unzip file %s: %s", uri, err)
 	}
-	readSinkMetaFile(path.Join(m.pluginDir, PluginTypes[t], name+`.json`))
+	confDir, err := common.GetConfLoc()
+	if nil != err {
+		return err
+	}
+	if SINK == t {
+		readSinkMetaFile(path.Join(confDir, PluginTypes[t], name+`.json`))
+	} else if SOURCE == t {
+		readSourceMetaFile(path.Join(confDir, PluginTypes[t], name+`.json`))
+	}
 	m.registry.Store(t, name, version)
 	return nil
 }
@@ -296,14 +302,12 @@ func (m *Manager) Delete(t PluginType, name string, stop bool) error {
 	paths := []string{
 		path.Join(m.pluginDir, PluginTypes[t], soFile),
 	}
+	if confDir, err := common.GetConfLoc(); nil == err {
+		os.Remove(path.Join(confDir, PluginTypes[t], name+".json"))
+	}
 	if t == SOURCE {
 		paths = append(paths, path.Join(m.etcDir, PluginTypes[t], name+".yaml"))
 	}
-	if t == SINK {
-		//m.delMetadata(name)
-		metadataFile := path.Join(m.pluginDir, "sinks", name+".json")
-		os.Remove(metadataFile)
-	}
 	for _, p := range paths {
 		_, err := os.Stat(p)
 		if err == nil {
@@ -385,8 +389,11 @@ func (m *Manager) install(t PluginType, name string, src string) ([]string, stri
 			}
 			filenames = append(filenames, yamlPath)
 		} else if fileName == name+".json" {
-			soPath := path.Join(m.pluginDir, PluginTypes[t], fileName)
-			err = unzipTo(file, soPath)
+			confDir, err := common.GetConfLoc()
+			if nil != err {
+				return filenames, "", err
+			}
+			err = unzipTo(file, path.Join(confDir, PluginTypes[t], fileName))
 			if err != nil {
 				return filenames, "", err
 			}

+ 2 - 2
plugins/sinkMeta.go

@@ -145,12 +145,12 @@ func readSinkMetaDir() error {
 	for _, sink := range InternalSinks {
 		file := path.Join(confDir, "sinks", "internal", sink+".json")
 		common.Log.Infof("Loading metadata file for sink: %s", file)
-		meta := new(uiSink)
+		meta := new(fileSink)
 		err := common.ReadJsonUnmarshal(file, meta)
 		if nil != err {
 			return fmt.Errorf("Failed to load internal sink plugin:%s with err:%v", file, err)
 		}
-		tmpMap[sink+".json"] = meta
+		tmpMap[sink+".json"] = newUiSink(meta)
 	}
 	files, err := ioutil.ReadDir(dir)
 	if nil != err {

+ 11 - 12
plugins/sourceMeta.go

@@ -48,34 +48,37 @@ func newUiSource(fi *fileSource) *uiSource {
 
 var g_sourceProperty map[string]*sourceProperty
 
-func readSourceMetaFile(filePath string) (*sourceProperty, error) {
+func readSourceMetaFile(filePath string) error {
 	ptrMeta := new(fileSource)
 	err := common.ReadJsonUnmarshal(filePath, ptrMeta)
 	if nil != err || 0 == len(ptrMeta.ConfKeys) {
-		return nil, fmt.Errorf("file:%s err:%v", filePath, err)
+		return fmt.Errorf("file:%s err:%v", filePath, err)
 	}
 	if 0 == len(ptrMeta.ConfKeys["default"]) {
-		return nil, fmt.Errorf("not found default confKey %s", filePath)
+		return fmt.Errorf("not found default confKey %s", filePath)
 	}
 
 	yamlData := make(map[string]map[string]interface{})
 	filePath = strings.TrimSuffix(filePath, `.json`) + `.yaml`
 	err = common.ReadYamlUnmarshal(filePath, &yamlData)
 	if nil != err {
-		return nil, fmt.Errorf("file:%s err:%v", filePath, err)
+		return fmt.Errorf("file:%s err:%v", filePath, err)
 	}
 	if 0 == len(yamlData["default"]) {
-		return nil, fmt.Errorf("not found default confKey from %s", filePath)
+		return fmt.Errorf("not found default confKey from %s", filePath)
 	}
 
 	property := new(sourceProperty)
 	property.cf = yamlData
 	property.meta = newUiSource(ptrMeta)
 
-	return property, err
+	fileName := path.Base(filePath)
+	g_sourceProperty[fileName] = property
+	return err
 }
 
 func readSourceMetaDir() error {
+	g_sourceProperty = make(map[string]*sourceProperty)
 	confDir, err := common.GetConfLoc()
 	if nil != err {
 		return err
@@ -87,9 +90,7 @@ func readSourceMetaDir() error {
 		return err
 	}
 
-	tmpMap := make(map[string]*sourceProperty)
-	tmpMap["mqtt_source.json"], err = readSourceMetaFile(path.Join(confDir, "mqtt_source.json"))
-	if nil != err {
+	if err = readSourceMetaFile(path.Join(confDir, "mqtt_source.json")); nil != err {
 		return err
 	}
 
@@ -97,14 +98,12 @@ func readSourceMetaDir() error {
 		fileName := info.Name()
 		if strings.HasSuffix(fileName, ".json") {
 			filePath := path.Join(dir, fileName)
-			tmpMap[fileName], err = readSourceMetaFile(filePath)
-			if nil != err {
+			if err = readSourceMetaFile(filePath); nil != err {
 				return err
 			}
 			common.Log.Infof("sourceMeta file : %s", fileName)
 		}
 	}
-	g_sourceProperty = tmpMap
 	return nil
 }