/
commands.go
143 lines (127 loc) · 2.87 KB
/
commands.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package build
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/micro/cli/v2"
log "github.com/dreamlu/go-micro/v2/logger"
goplugin "github.com/dreamlu/go-micro/v2/plugin"
"github.com/dreamlu/micro/v2/plugin"
)
func build(ctx *cli.Context) error {
name := ctx.String("name")
path := ctx.String("path")
newfn := ctx.String("func")
typ := ctx.String("type")
out := ctx.String("output")
if len(name) == 0 {
// TODO return err
fmt.Println("specify --name of plugin")
os.Exit(1)
}
if len(typ) == 0 {
// TODO return err
fmt.Println("specify --type of plugin")
os.Exit(1)
}
// set the path
if len(path) == 0 {
// github.com/micro/go-plugins/broker/rabbitmq
// github.com/micro/go-plugins/micro/basic_auth
path = filepath.Join("github.com/micro/go-plugins", typ, name)
}
// set the newfn
if len(newfn) == 0 {
if typ == "micro" {
newfn = "NewPlugin"
} else {
newfn = "New" + strings.Title(typ)
}
}
if len(out) == 0 {
out = "./"
}
// create a .so file
if !strings.HasSuffix(out, ".so") {
out = filepath.Join(out, name+".so")
}
if err := goplugin.Build(out, &goplugin.Config{
Name: name,
Type: typ,
Path: path,
NewFunc: newfn,
}); err != nil {
// TODO return err
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Plugin %s generated at %s\n", name, out)
return nil
}
func pluginCommands() []*cli.Command {
return []*cli.Command{
{
Name: "build",
Usage: "Build a micro plugin",
Action: build,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "Name of the plugin e.g rabbitmq",
},
&cli.StringFlag{
Name: "type",
Usage: "Type of the plugin e.g broker",
},
&cli.StringFlag{
Name: "path",
Usage: "Import path of the plugin",
},
&cli.StringFlag{
Name: "func",
Usage: "New plugin function creator name e.g NewBroker",
},
&cli.StringFlag{
Name: "output, o",
Usage: "Output dir or file for the plugin",
},
},
},
}
}
// Commands returns license commands
func Commands() []*cli.Command {
return []*cli.Command{{
Name: "plugin",
Usage: "Plugin commands",
Subcommands: pluginCommands(),
}}
}
// returns a micro plugin which loads plugins
func Flags() plugin.Plugin {
return plugin.NewPlugin(
plugin.WithName("plugin"),
plugin.WithFlag(
&cli.StringSliceFlag{
Name: "plugin",
EnvVars: []string{"MICRO_PLUGIN"},
Usage: "Comma separated list of plugins e.g broker/rabbitmq, registry/etcd, micro/basic_auth, /path/to/plugin.so",
},
),
plugin.WithInit(func(ctx *cli.Context) error {
plugins := ctx.StringSlice("plugin")
if len(plugins) == 0 {
return nil
}
for _, p := range plugins {
if err := load(p); err != nil {
log.Errorf("Error loading plugin %s: %v", p, err)
return err
}
log.Infof("Loaded plugin %s\n", p)
}
return nil
}),
)
}