plugin/manage/manage.go

65 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-08-30 18:02:15 +08:00
package manage
import (
"fmt"
2024-08-30 18:03:34 +08:00
"gitea.cdlsxd.cn/sdk/plugin/shared"
2024-08-30 18:02:15 +08:00
)
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 m.exists(tag) {
return m.plugins[tag], nil
}
return nil, fmt.Errorf("插件不存在[%s]", tag)
}
func (m *pluginManage) close() {
for _, info := range m.plugins {
if info.cleanup != nil {
info.cleanup()
}
}
}