65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
|
package manage
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"gitea.cdlsxd.cn/sdk/plugin/shared"
|
||
|
)
|
||
|
|
||
|
type PluginInfo struct {
|
||
|
Tag string
|
||
|
Impl shared.PluginService
|
||
|
cleanup func()
|
||
|
}
|
||
|
|
||
|
type pluginManage struct {
|
||
|
plugins map[string]*PluginInfo
|
||
|
}
|
||
|
|
||
|
func (m *pluginManage) exists(tag string) bool {
|
||
|
if _, ok := m.plugins[tag]; ok {
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (m *pluginManage) add(c *Config) error {
|
||
|
if m.exists(c.Tag) {
|
||
|
return fmt.Errorf("插件已存在[%s]", c.Tag)
|
||
|
}
|
||
|
impl, cleanup, err := newClient(c)
|
||
|
if err != nil {
|
||
|
cleanup()
|
||
|
return err
|
||
|
}
|
||
|
m.plugins[c.Tag] = &PluginInfo{
|
||
|
Tag: c.Tag,
|
||
|
Impl: impl,
|
||
|
cleanup: cleanup,
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *pluginManage) remove(tag string) error {
|
||
|
if m.exists(tag) {
|
||
|
m.plugins[tag].cleanup()
|
||
|
delete(m.plugins, tag)
|
||
|
return nil
|
||
|
}
|
||
|
return fmt.Errorf("插件不存在[%s]", tag)
|
||
|
}
|
||
|
|
||
|
func (m *pluginManage) get(tag string) (*PluginInfo, error) {
|
||
|
if p, ok := m.plugins[tag]; ok {
|
||
|
return p, nil
|
||
|
}
|
||
|
return nil, fmt.Errorf("插件不存在[%s]", tag)
|
||
|
}
|
||
|
|
||
|
func (m *pluginManage) close() {
|
||
|
for _, info := range m.plugins {
|
||
|
if info.cleanup != nil {
|
||
|
info.cleanup()
|
||
|
}
|
||
|
}
|
||
|
}
|