/
base.go
81 lines (65 loc) · 1.74 KB
/
base.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package plugin
import (
"github.com/pubgo/lug/entry"
"github.com/pubgo/lug/logutil"
"github.com/pubgo/lug/types"
"github.com/pubgo/lug/vars"
"github.com/pubgo/x/try"
"github.com/pubgo/xlog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
)
var _ = Plugin(&Base{})
type Base struct {
Name string
OnMiddleware func() types.Middleware
OnInit func(ent entry.Entry)
OnCommands func(cmd *cobra.Command)
OnFlags func(flags *pflag.FlagSet)
OnWatch func(name string, resp *types.WatchResp)
OnVars func(w func(name string, data func() interface{}))
OnCodec func(name string, resp *types.WatchResp) (map[string]interface{}, error)
}
func (p *Base) String() string { return p.Name }
func (p *Base) Init(ent entry.Entry) (err error) {
return try.Try(func() {
if p.OnMiddleware != nil {
ent.Middleware(p.OnMiddleware())
}
if p.OnInit != nil {
xlog.Infof("plugin [%s] init", p.Name)
p.OnInit(ent)
}
if p.OnInit != nil && p.OnVars != nil {
p.OnVars(vars.Watch)
}
})
}
func (p *Base) Watch(name string, r *types.WatchResp) (err error) {
if p.OnWatch == nil {
return
}
xlog.Infof("plugin [%s] watch", p.Name)
return try.Try(func() { p.OnWatch(name, r) })
}
func (p *Base) Commands() *cobra.Command {
if p.OnCommands == nil {
return nil
}
cmd := &cobra.Command{Use: p.Name}
try.Catch(func() { p.OnCommands(cmd) }, func(err error) {
zap.L().Error("commands callback", logutil.Err(err))
})
return cmd
}
func (p *Base) Flags() *pflag.FlagSet {
flags := pflag.NewFlagSet(p.Name, pflag.PanicOnError)
if p.OnFlags == nil {
return flags
}
try.Catch(func() { p.OnFlags(flags) }, func(err error) {
zap.L().Fatal("flags callback", zap.Any("err", err))
})
return flags
}