-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginbundle_provider.go
47 lines (39 loc) · 1.2 KB
/
pluginbundle_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package plugin
import (
"fmt"
"git.golaxy.org/core/internal/exception"
"git.golaxy.org/core/util/iface"
)
// PluginProvider 插件提供者
type PluginProvider interface {
// GetPluginBundle 获取插件包
GetPluginBundle() PluginBundle
}
// Using 使用插件
func Using[T any](provider PluginProvider, name string) T {
if provider == nil {
panic(fmt.Errorf("%w: %w: provider is nil", ErrPlugin, exception.ErrArgs))
}
pluginInfo, ok := provider.GetPluginBundle().Get(name)
if !ok {
panic(fmt.Errorf("%w: plugin %q not installed", ErrPlugin, name))
}
if !pluginInfo.Active {
panic(fmt.Errorf("%w: plugin %q not actived", ErrPlugin, name))
}
return iface.Cache2Iface[T](pluginInfo.Face.Cache)
}
// Install 安装插件
func Install[T any](provider PluginProvider, plugin T, name ...string) {
if provider == nil {
panic(fmt.Errorf("%w: %w: provider is nil", ErrPlugin, exception.ErrArgs))
}
provider.GetPluginBundle().Install(iface.MakeFaceAny(plugin), name...)
}
// Uninstall 卸载插件
func Uninstall(provider PluginProvider, name string) {
if provider == nil {
panic(fmt.Errorf("%w: %w: provider is nil", ErrPlugin, exception.ErrArgs))
}
provider.GetPluginBundle().Uninstall(name)
}