diff --git a/manage/init.go b/manage/init.go deleted file mode 100644 index 236e4a6..0000000 --- a/manage/init.go +++ /dev/null @@ -1,56 +0,0 @@ -package manage - -var m *pluginManage - -func init() { - m = &pluginManage{ - plugins: make(map[string]*PluginInfo), - } -} - -func Load(p []*Config) error { - for _, c := range p { - err := Add(c) - if err != nil { - return err - } - } - return nil -} - -func Add(c *Config) error { - err := c.Validate() - if err != nil { - return err - } - return m.add(c) -} - -func Update(c *Config) error { - err := c.Validate() - if err != nil { - return err - } - err = Remove(c.Tag) - if err != nil { - return err - } - return m.add(c) -} - -func Get(tag string) (*PluginInfo, error) { - p, err := m.get(tag) - if err != nil { - return nil, err - } - return p, nil -} - -func Remove(tag string) error { - return m.remove(tag) -} - -func Close() { - m.close() - m = nil -} diff --git a/manage/manage.go b/manage/manage.go index 925a4fa..d810067 100644 --- a/manage/manage.go +++ b/manage/manage.go @@ -1,64 +1,60 @@ package manage -import ( - "fmt" - "gitea.cdlsxd.cn/sdk/plugin/shared" -) +var m *pluginManage -type PluginInfo struct { - Tag string - Impl shared.PluginService - cleanup func() +func init() { + m = &pluginManage{ + plugins: make(map[string]*PluginInfo), + } } -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, +func Load(p []*Config) error { + for _, c := range p { + err := Add(c) + if err != nil { + return err + } } return nil } -func (m *pluginManage) remove(tag string) error { - if m.exists(tag) { - m.plugins[tag].cleanup() - delete(m.plugins, tag) - return nil +func Add(c *Config) error { + err := c.Validate() + if err != nil { + return err } - return fmt.Errorf("插件不存在[%s]", tag) + return m.add(c) } -func (m *pluginManage) get(tag string) (*PluginInfo, error) { - if m.exists(tag) { - return m.plugins[tag], nil +func Update(c *Config) error { + err := c.Validate() + if err != nil { + return err } - return nil, fmt.Errorf("插件不存在[%s]", tag) + err = Remove(c.Tag) + if err != nil { + return err + } + return m.add(c) } -func (m *pluginManage) close() { - for _, info := range m.plugins { - if info.cleanup != nil { - info.cleanup() - } +func Get(tag string) (*PluginInfo, error) { + p, err := m.get(tag) + if err != nil { + return nil, err } + return p, nil +} + +func GetAll() map[string]*PluginInfo { + return m.plugins +} + +func Remove(tag string) error { + return m.remove(tag) +} + +func Close() { + m.close() + m = nil } diff --git a/manage/plugin.go b/manage/plugin.go new file mode 100644 index 0000000..5a35f9e --- /dev/null +++ b/manage/plugin.go @@ -0,0 +1,64 @@ +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() + } + } +}