Переглянути джерело

feat(plugin): use struct as register param

ngjaying 5 роки тому
батько
коміт
f40059ab9f
2 змінених файлів з 30 додано та 16 видалено
  1. 24 14
      plugins/manager.go
  2. 6 2
      plugins/manager_test.go

+ 24 - 14
plugins/manager.go

@@ -17,6 +17,12 @@ import (
 	"unicode"
 )
 
+type Plugin struct {
+	Name     string `json:"name"`
+	File     string `json:"file"`
+	Callback string `json:"callback"`
+}
+
 type PluginType int
 
 const (
@@ -31,8 +37,6 @@ var (
 	singleton     *Manager
 )
 
-type OnRegistered func()
-
 //Registry is append only because plugin cannot delete or reload. To delete a plugin, restart the server to reindex
 type Registry struct {
 	sync.RWMutex
@@ -121,7 +125,12 @@ func findAll(t PluginType, pluginDir string) (result []string, err error) {
 	return
 }
 
-func (m *Manager) Register(t PluginType, name string, uri string, callback OnRegistered) error {
+func (m *Manager) List(t PluginType) (result []string, err error) {
+	return m.registry.List(t), nil
+}
+
+func (m *Manager) Register(t PluginType, j *Plugin) error {
+	name, uri, cb := j.Name, j.File, j.Callback
 	//Validation
 	name = strings.Trim(name, " ")
 	if name == "" {
@@ -139,15 +148,7 @@ func (m *Manager) Register(t PluginType, name string, uri string, callback OnReg
 	zipPath := path.Join(m.pluginDir, name+".zip")
 	var unzipFiles []string
 	//clean up: delete zip file and unzip files in error
-	defer func() {
-		os.Remove(zipPath)
-		if t == SOURCE && len(unzipFiles) == 1 { //source that only copy so file
-			os.Remove(unzipFiles[0])
-		} else if (t == SOURCE && len(unzipFiles) == 2) || (t != SOURCE && len(unzipFiles) == 1) { //no error
-			m.registry.Store(t, name)
-			callback()
-		}
-	}()
+	defer os.Remove(zipPath)
 	//download
 	err := downloadFile(zipPath, uri)
 	if err != nil {
@@ -156,12 +157,17 @@ func (m *Manager) Register(t PluginType, name string, uri string, callback OnReg
 	//unzip and copy to destination
 	unzipFiles, err = m.unzipAndCopy(t, name, zipPath)
 	if err != nil {
+		if t == SOURCE && len(unzipFiles) == 1 { //source that only copy so file
+			os.Remove(unzipFiles[0])
+		}
 		return fmt.Errorf("fail to unzip file %s: %s", uri, err)
 	}
-	return nil
+
+	m.registry.Store(t, name)
+	return callback(cb)
 }
 
-func (m *Manager) Delete(t PluginType, name string, callback OnRegistered) (result error) {
+func (m *Manager) Delete(t PluginType, name string) (result error) {
 	name = strings.Trim(name, " ")
 	if name == "" {
 		return fmt.Errorf("invalid name %s: should not be empty", name)
@@ -319,3 +325,7 @@ func lcFirst(str string) string {
 	}
 	return ""
 }
+
+func callback(u string) error {
+	return nil
+}

+ 6 - 2
plugins/manager_test.go

@@ -68,7 +68,11 @@ func TestManager_Register(t *testing.T) {
 
 	fmt.Printf("The test bucket size is %d.\n\n", len(data))
 	for i, tt := range data {
-		err = manager.Register(tt.t, tt.n, tt.u, func() {})
+		err = manager.Register(tt.t, &Plugin{
+			Name:     tt.n,
+			File:     tt.u,
+			Callback: "",
+		})
 		if !reflect.DeepEqual(tt.err, err) {
 			t.Errorf("%d: error mismatch:\n  exp=%s\n  got=%s\n\n", i, tt.err, err)
 		} else if tt.err == nil {
@@ -105,7 +109,7 @@ func TestManager_Delete(t *testing.T) {
 	fmt.Printf("The test bucket size is %d.\n\n", len(data))
 
 	for i, p := range data {
-		err = manager.Delete(p.t, p.n, func() {})
+		err = manager.Delete(p.t, p.n)
 		if err != nil {
 			t.Errorf("%d: delete error : %s\n\n", i, err)
 		}