/
plugin.go
381 lines (328 loc) · 10.7 KB
/
plugin.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package cmd
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/dsrvlabs/vatz/manager/config"
"github.com/dsrvlabs/vatz/manager/plugin"
"github.com/jedib0t/go-pretty/v6/table"
)
/*
TODO list for plugin command.
- How to implement package repository?
- How to manage installed plugins?
- Install / Remove / Update plugin.
- How to execute?
- Pass args.
- Start / Stop
*/
var (
statusCommand = &cobra.Command{
Use: "status",
Short: "Get statuses of Plugin",
PreRunE: func(cmd *cobra.Command, args []string) error {
_, err := config.InitConfig(configFile)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
statusRequest := fmt.Sprintf("%s/v1/plugin_status", vatzRPC)
req, err := http.NewRequest(http.MethodGet, statusRequest, nil)
if err != nil {
return err
}
cli := http.Client{}
resp, err := cli.Do(req)
if err != nil {
log.Error().Str("module", "plugin").Err(err)
return err
}
log.Debug().Str("module", "plugin").Msgf("Plugin(s) status is requested to %s.", statusRequest)
respData, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Str("module", "plugin").Err(err)
return err
}
statusResp := struct {
Status string `json:"status"`
PluginStatus []struct {
Status string `json:"status"`
PluginName string `json:"pluginName"`
} `json:"pluginStatus"`
}{}
err = json.Unmarshal(respData, &statusResp)
if err != nil {
log.Error().Str("module", "plugin").Err(err)
return err
}
fmt.Println("***** Plugin Status *****")
for i, plugin := range statusResp.PluginStatus {
fmt.Printf("%d: %s [%s]\n", i+1, plugin.PluginName, plugin.Status)
}
return nil
},
}
installCommand = &cobra.Command{
Use: "install",
Short: "Install new plugin",
Args: cobra.ExactArgs(2), // TODO: Can I check real git repo?
Example: "vatz plugin install <plugin's githubAddress> <pluginName>",
PreRunE: func(cmd *cobra.Command, args []string) error {
_, err := config.InitConfig(configFile)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
const defaultVersion = "latest"
pluginDir, err := config.GetConfig().Vatz.AbsoluteHomePath()
if err != nil {
return err
}
log.Debug().Str("module", "plugin").Msgf("Install new plugin %s at %s.", args[0], pluginDir)
pluginVersion := defaultVersion
if viper.GetString("plugin_version") != "" {
pluginVersion = viper.GetString("plugin_version")
}
log.Debug().Str("module", "plugin").Msgf("Installing plugin version is %s.", pluginVersion)
mgr := plugin.NewManager(pluginDir)
err = mgr.Install(args[0], args[1], pluginVersion)
if err != nil {
log.Error().Str("module", "plugin").Err(err)
return err
}
log.Info().Str("module", "plugin").Msgf("A new plugin %s is successfully installed.", args[1])
return nil
},
}
uninstallCommand = &cobra.Command{
Use: "uninstall",
Short: "Uninstall plugin from plugin registry",
Args: cobra.ExactArgs(1), // TODO: Can I check real git repo?
Example: "vatz plugin uninstall <pluginName>",
PreRunE: func(cmd *cobra.Command, args []string) error {
_, err := config.InitConfig(configFile)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
pluginDir, err := config.GetConfig().Vatz.AbsoluteHomePath()
if err != nil {
return err
}
log.Debug().Str("module", "plugin").Msgf("Uninstall a plugin %s from %s", args[0], pluginDir)
// TODO: Handle invalid repo name.
mgr := plugin.NewManager(pluginDir)
pluginExist := false
plugins, err := mgr.List()
if err != nil {
return err
}
for _, plugin := range plugins {
if plugin.Name == args[0] {
pluginExist = true
break
}
}
if !pluginExist {
log.Error().Str("module", "plugin").Msgf("There's no plugin with the name %s from the installed plugin list. ", args[0])
return errors.New("Please confirm plugin name again.")
}
if err = mgr.Uninstall(args[0]); err != nil {
log.Error().Str("module", "plugin").Err(err)
return err
}
log.Info().Str("module", "plugin").Msgf("Plugin %s is successfully uninstalled from %s", args[0], pluginDir)
return nil
},
}
startCommand = &cobra.Command{
Use: "start",
Short: "Start installed plugin",
Example: "vatz plugin start --plugin <pluginName> or vatz plugin start --plugin <pluginName> --args <arguments>",
PreRunE: func(cmd *cobra.Command, args []string) error {
_, err := config.InitConfig(configFile)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
pluginName := viper.GetString("start_plugin")
exeArgs := viper.GetString("start_args")
pluginDir, err := config.GetConfig().Vatz.AbsoluteHomePath()
if err != nil {
return err
}
log.Info().Str("module", "plugin").Msgf("Start plugin %s %s", pluginName, exeArgs)
logfile := viper.GetString("start_log")
if logfile == "" {
logfile = fmt.Sprintf("%s/%s.log", pluginDir, pluginName)
}
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Info().Str("module", "plugin").Err(err)
return err
}
log.Debug().Str("module", "plugin").Msgf("Plugin log redirect to %s", logfile)
mgr := plugin.NewManager(pluginDir)
return mgr.Start(pluginName, exeArgs, f)
},
}
stopCommand = &cobra.Command{
Use: "stop",
Short: "Stop running plugin",
Example: "vatz plugin stop --plugin <pluginName>",
PreRunE: func(cmd *cobra.Command, args []string) error {
_, err := config.InitConfig(configFile)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
pluginName := viper.GetString("stop_plugin")
pluginDir, err := config.GetConfig().Vatz.AbsoluteHomePath()
if err != nil {
return err
}
log.Info().Str("module", "plugin").Msgf("Stop plugin %s", pluginName)
mgr := plugin.NewManager(pluginDir)
return mgr.Stop(pluginName)
},
}
enableCommand = &cobra.Command{
Use: "enable",
Short: "Enable plugin",
Example: "vatz plugin enable --plugin <pluginName>",
PreRunE: func(cmd *cobra.Command, args []string) error {
_, err := config.InitConfig(configFile)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
pluginName := viper.GetString("enable_plugin")
pluginDir, err := config.GetConfig().Vatz.AbsoluteHomePath()
if err != nil {
return err
}
log.Debug().Str("module", "plugin").Msgf("enable installed plugin %s at %s", pluginName, pluginDir)
mgr := plugin.NewManager(pluginDir)
err = mgr.SetEnabled(pluginName, true)
if err != nil {
log.Error().Str("module", "plugin").Err(err)
return err
}
return nil
},
}
disableCommand = &cobra.Command{
Use: "disable",
Short: "Disable plugin",
Example: "vatz plugin disable --plugin <pluginName>",
PreRunE: func(cmd *cobra.Command, args []string) error {
_, err := config.InitConfig(configFile)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
pluginName := viper.GetString("disable_plugin")
pluginDir, err := config.GetConfig().Vatz.AbsoluteHomePath()
if err != nil {
return err
}
log.Debug().Str("module", "plugin").Msgf("disable installed plugin %s at %s", pluginName, pluginDir)
mgr := plugin.NewManager(pluginDir)
err = mgr.SetEnabled(pluginName, false)
if err != nil {
log.Error().Str("module", "plugin").Err(err)
return err
}
return nil
},
}
listCommand = &cobra.Command{
Use: "list",
Short: "List installed plugin",
Example: "vatz plugin list",
PreRunE: func(cmd *cobra.Command, args []string) error {
_, err := config.InitConfig(configFile)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
pluginDir, err := config.GetConfig().Vatz.AbsoluteHomePath()
if err != nil {
return err
}
log.Debug().Str("module", "plugin").Msgf("List plugins")
mgr := plugin.NewManager(pluginDir)
plugins, err := mgr.List()
if err != nil {
log.Error().Str("module", "plugin").Err(err)
return err
}
w := table.NewWriter()
w.SetOutputMirror(os.Stdout)
w.AppendHeader(table.Row{"Name", "Is Enabled", "Install Date", "Repository", "Version"})
for _, plugin := range plugins {
dateStr := plugin.InstalledAt.Format("2006-01-02 15:04:05")
w.AppendRow([]interface{}{
plugin.Name, plugin.IsEnabled, dateStr, plugin.Repository, plugin.Version,
})
}
w.Render()
return nil
},
}
)
func createPluginCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "plugin",
Short: "Plugin commands",
}
cmd.PersistentFlags().StringVar(&configFile, "config", defaultFlagConfig, "VATZ config file.")
statusCommand.PersistentFlags().StringVar(&vatzRPC, "rpc", defaultRPC, "RPC address of Vatz")
startCommand.PersistentFlags().StringP("plugin", "p", "", "Installed plugin name")
startCommand.PersistentFlags().StringP("args", "a", "", "Arguments")
startCommand.PersistentFlags().StringP("log", "l", "", "Logfile")
err := viper.BindPFlag("start_plugin", startCommand.PersistentFlags().Lookup("plugin"))
if err != nil {
log.Error().Str("module", "plugin").Err(err)
}
err = viper.BindPFlag("start_args", startCommand.PersistentFlags().Lookup("args"))
if err != nil {
log.Error().Str("module", "plugin").Err(err)
}
err = viper.BindPFlag("start_log", startCommand.PersistentFlags().Lookup("log"))
if err != nil {
log.Error().Str("module", "plugin").Err(err)
}
stopCommand.PersistentFlags().StringP("plugin", "p", "", "Installed plugin name")
err = viper.BindPFlag("stop_plugin", stopCommand.PersistentFlags().Lookup("plugin"))
if err != nil {
log.Error().Str("module", "plugin").Err(err)
}
installCommand.PersistentFlags().StringP("version", "v", "", "Installed plugin version")
err = viper.BindPFlag("plugin_version", installCommand.PersistentFlags().Lookup("version"))
if err != nil {
log.Error().Str("module", "plugin").Err(err)
}
enableCommand.PersistentFlags().StringP("plugin", "p", "", "Installed plugin name")
err = viper.BindPFlag("enable_plugin", enableCommand.PersistentFlags().Lookup("plugin"))
if err != nil {
log.Error().Str("module", "plugin").Err(err)
}
disableCommand.PersistentFlags().StringP("plugin", "p", "", "Installed plugin name")
err = viper.BindPFlag("disable_plugin", disableCommand.PersistentFlags().Lookup("plugin"))
if err != nil {
log.Error().Str("module", "plugin").Err(err)
}
commands := []*cobra.Command{
statusCommand,
installCommand,
uninstallCommand,
startCommand,
stopCommand,
enableCommand,
disableCommand,
listCommand,
}
for _, pluginCmd := range commands {
cmd.AddCommand(pluginCmd)
}
return cmd
}