This repository has been archived by the owner on Aug 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
plugin.go
562 lines (485 loc) · 19.8 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package iris
import (
"sync"
"log"
"github.com/kataras/go-errors"
"github.com/kataras/go-fs"
)
var (
// errPluginAlreadyExists returns an error with message: 'Cannot activate the same plugin again, plugin '+plugin name[+plugin description]' is already exists'
errPluginAlreadyExists = errors.New("Cannot use the same plugin again, '%s[%s]' is already exists")
// errPluginActivate returns an error with message: 'While trying to activate plugin '+plugin name'. Trace: +specific error'
errPluginActivate = errors.New("While trying to activate plugin '%s'. Trace: %s")
// errPluginRemoveNoPlugins returns an error with message: 'No plugins are registed yet, you cannot remove a plugin from an empty list!'
errPluginRemoveNoPlugins = errors.New("No plugins are registed yet, you cannot remove a plugin from an empty list!")
// errPluginRemoveEmptyName returns an error with message: 'Plugin with an empty name cannot be removed'
errPluginRemoveEmptyName = errors.New("Plugin with an empty name cannot be removed")
// errPluginRemoveNotFound returns an error with message: 'Cannot remove a plugin which doesn't exists'
errPluginRemoveNotFound = errors.New("Cannot remove a plugin which doesn't exists")
)
type (
// Plugin just an empty base for plugins
// A Plugin can be added with: .Add(PreListenFunc(func(*Framework))) and so on... or
// .Add(myPlugin{},myPlugin2{}) which myPlugin is a struct with any of the methods below or
// .PostListen(func(*Framework)) and so on...
Plugin interface {
}
// pluginGetName implements the GetName() string method
pluginGetName interface {
// GetName has to returns the name of the plugin, a name is unique
// name has to be not dependent from other methods of the plugin,
// because it is being called even before the Activate
GetName() string
}
// pluginGetDescription implements the GetDescription() string method
pluginGetDescription interface {
// GetDescription has to returns the description of what the plugins is used for
GetDescription() string
}
// pluginActivate implements the Activate(pluginContainer) error method
pluginActivate interface {
// Activate called BEFORE the plugin being added to the plugins list,
// if Activate returns none nil error then the plugin is not being added to the list
// it is being called only one time
//
// PluginContainer parameter used to add other plugins if that's necessary by the plugin
Activate(PluginContainer) error
}
// pluginPreLookup implements the PreRoute(Route) method
pluginPreLookup interface {
// PreLookup called before register a route
PreLookup(Route)
}
// PreLookupFunc implements the simple function listener for the PreLookup(Route)
PreLookupFunc func(Route)
// pluginPreBuild implements the PreBuild(*Framework) method
pluginPreBuild interface {
// PreBuild it's being called once time, BEFORE the Server is started and before PreListen
// is used to do work before all other things are ready
// use this event if you want to add routes to your iris station
// or make any changes to the iris main configuration
// receiver is the station
PreBuild(*Framework)
}
// PreBuildFunc implements the simple function listener for the PreBuild(*Framework)
PreBuildFunc func(*Framework)
// pluginPreListen implements the PreListen(*Framework) method
pluginPreListen interface {
// PreListen it's being called only one time, BEFORE the Server is started (if .Listen called)
// is used to do work at the time all other things are ready to go
// receiver is the station
PreListen(*Framework)
}
// PreListenFunc implements the simple function listener for the PreListen(*Framework)
PreListenFunc func(*Framework)
// pluginPostListen implements the PostListen(*Framework) method
pluginPostListen interface {
// PostListen it's being called only one time, AFTER the Server is started (if .Listen called)
// parameter is the station
PostListen(*Framework)
}
// PostListenFunc implements the simple function listener for the PostListen(*Framework)
PostListenFunc func(*Framework)
// pluginPreClose implements the PreClose(*Framework) method
pluginPreClose interface {
// PreClose it's being called only one time, BEFORE the Iris .Close method
// any plugin cleanup/clear memory happens here
//
// The plugin is deactivated after this state
PreClose(*Framework)
}
// PreCloseFunc implements the simple function listener for the PreClose(*Framework)
PreCloseFunc func(*Framework)
// pluginPreDownload It's for the future, not being used, I need to create
// and return an ActivatedPlugin type which will have it's methods, and pass it on .Activate
// but now we return the whole pluginContainer, which I can't determinate which plugin tries to
// download something, so we will leave it here for the future.
pluginPreDownload interface {
// PreDownload it's being called every time a plugin tries to download something
//
// first parameter is the plugin
// second parameter is the download url
// must return a boolean, if false then the plugin is not permmited to download this file
PreDownload(plugin Plugin, downloadURL string) // bool
}
// PreDownloadFunc implements the simple function listener for the PreDownload(plugin,string)
PreDownloadFunc func(Plugin, string)
// PluginContainer is the interface which the pluginContainer should implements
PluginContainer interface {
Add(...Plugin) error
Remove(string) error
Len() int
GetName(Plugin) string
GetDescription(Plugin) string
GetByName(string) Plugin
Printf(string, ...interface{})
Fired(string) int
PreLookup(PreLookupFunc)
DoPreLookup(Route)
PreLookupFired() bool
PreBuild(PreBuildFunc)
DoPreBuild(*Framework)
PreBuildFired() bool
PreListen(PreListenFunc)
DoPreListen(*Framework)
DoPreListenParallel(*Framework)
PreListenFired() bool
PostListen(PostListenFunc)
DoPostListen(*Framework)
PostListenFired() bool
PreClose(PreCloseFunc)
DoPreClose(*Framework)
PreCloseFired() bool
PreDownload(PreDownloadFunc)
DoPreDownload(Plugin, string)
PreDownloadFired() bool
//
GetAll() []Plugin
// GetDownloader is the only one module that is used and fire listeners at the same time in this file
GetDownloader() PluginDownloadManager
} //Note: custom event callbacks, never used internaly by Iris, but if you need them use this: github.com/kataras/go-events
// PluginDownloadManager is the interface which the DownloadManager should implements
PluginDownloadManager interface {
DirectoryExists(string) bool
DownloadZip(string, string) (string, error)
Unzip(string, string) (string, error)
Remove(string) error
// install is just the flow of: downloadZip -> unzip -> removeFile(zippedFile)
// accepts 2 parameters
//
// first parameter is the remote url file zip
// second parameter is the target directory
// returns a string(installedDirectory) and an error
//
// (string) installedDirectory is the directory which the zip file had, this is the real installation path, you don't need to know what it's because these things maybe change to the future let's keep it to return the correct path.
// the installedDirectory is not empty when the installation is succed, the targetDirectory is not already exists and no error happens
// the installedDirectory is empty when the installation is already done by previous time or an error happens
Install(remoteFileZip string, targetDirectory string) (string, error)
}
// pluginDownloadManager is just a struch which exports the util's downloadZip, directoryExists, unzip methods, used by the plugins via the pluginContainer
pluginDownloadManager struct {
}
)
// convert the functions to plugin
// PreLookup called before register a route
func (fn PreLookupFunc) PreLookup(r Route) {
fn(r)
}
// PreBuild it's being called once time, BEFORE the Server is started and before PreListen
// is used to do work before all other things are ready
// use this event if you want to add routes to your iris station
// or make any changes to the iris main configuration
// receiver is the station
func (fn PreBuildFunc) PreBuild(station *Framework) {
fn(station)
}
// PreListen it's being called only one time, BEFORE the Server is started (if .Listen called)
// is used to do work at the time all other things are ready to go
// parameter is the station
func (fn PreListenFunc) PreListen(station *Framework) {
fn(station)
}
// PostListen it's being called only one time, AFTER the Server is started (if .Listen called)
// parameter is the station
func (fn PostListenFunc) PostListen(station *Framework) {
fn(station)
}
// PreClose it's being called only one time, BEFORE the Iris .Close method
// any plugin cleanup/clear memory happens here
//
// The plugin is deactivated after this state
func (fn PreCloseFunc) PreClose(station *Framework) {
fn(station)
}
// PreDownload it's being called every time a plugin tries to download something
//
// first parameter is the plugin
// second parameter is the download url
// must return a boolean, if false then the plugin is not permmited to download this file
func (fn PreDownloadFunc) PreDownload(pl Plugin, downloadURL string) {
fn(pl, downloadURL)
}
//
var _ PluginDownloadManager = &pluginDownloadManager{}
var _ PluginContainer = &pluginContainer{}
// DirectoryExists returns true if a given local directory exists
func (d *pluginDownloadManager) DirectoryExists(dir string) bool {
return fs.DirectoryExists(dir)
}
// DownloadZip downlodas a zip to the given local path location
func (d *pluginDownloadManager) DownloadZip(zipURL string, targetDir string) (string, error) {
return fs.DownloadZip(zipURL, targetDir, true)
}
// Unzip unzips a zip to the given local path location
func (d *pluginDownloadManager) Unzip(archive string, target string) (string, error) {
return fs.DownloadZip(archive, target, true)
}
// Remove deletes/removes/rm a file
func (d *pluginDownloadManager) Remove(filePath string) error {
return fs.RemoveFile(filePath)
}
// Install is just the flow of the: DownloadZip->Unzip->Remove the zip
func (d *pluginDownloadManager) Install(remoteFileZip string, targetDirectory string) (string, error) {
return fs.Install(remoteFileZip, targetDirectory, true)
}
// pluginContainer is the base container of all Iris, registed plugins
type pluginContainer struct {
activatedPlugins []Plugin
customEvents map[string][]func()
downloader *pluginDownloadManager
logger *log.Logger
mu *sync.Mutex
fired map[string]int // event/plugin type name and the times fired
}
// newPluginContainer receives a logger and returns a new PluginContainer
func newPluginContainer(l *log.Logger) PluginContainer {
return &pluginContainer{logger: l, fired: make(map[string]int, 0), mu: &sync.Mutex{}}
}
// Add activates the plugins and if succeed then adds it to the activated plugins list
func (p *pluginContainer) Add(plugins ...Plugin) error {
for _, plugin := range plugins {
if p.activatedPlugins == nil {
p.activatedPlugins = make([]Plugin, 0)
}
// Check if it's a plugin first, has Activate GetName
// Check if the plugin already exists
pName := p.GetName(plugin)
if pName != "" && p.GetByName(pName) != nil {
return errPluginAlreadyExists.Format(pName, p.GetDescription(plugin))
}
// Activate the plugin, if no error then add it to the plugins
if pluginObj, ok := plugin.(pluginActivate); ok {
tempPluginContainer := *p
err := pluginObj.Activate(&tempPluginContainer)
if err != nil {
return errPluginActivate.Format(pName, err.Error())
}
tempActivatedPluginsLen := len(tempPluginContainer.activatedPlugins)
if tempActivatedPluginsLen != len(p.activatedPlugins)+tempActivatedPluginsLen+1 { // see test: plugin_test.go TestPluginActivate && TestPluginActivationError
p.activatedPlugins = tempPluginContainer.activatedPlugins
}
}
// All ok, add it to the plugins list
p.activatedPlugins = append(p.activatedPlugins, plugin)
}
return nil
}
// Remove removes a plugin by it's name, if pluginName is empty "" or no plugin found with this name, then nothing is removed and a specific error is returned.
// This doesn't calls the PreClose method
func (p *pluginContainer) Remove(pluginName string) error {
if p.activatedPlugins == nil {
return errPluginRemoveNoPlugins
}
if pluginName == "" {
//return error: cannot delete an unamed plugin
return errPluginRemoveEmptyName
}
indexToRemove := -1
for i := range p.activatedPlugins {
if p.GetName(p.activatedPlugins[i]) == pluginName { // Note: if GetName is not implemented then the name is "" which is != with the plugiName, we checked this before.
indexToRemove = i
}
}
if indexToRemove == -1 { //if index stills -1 then no plugin was found with this name, just return an error. it is not a critical error.
return errPluginRemoveNotFound
}
p.activatedPlugins = append(p.activatedPlugins[:indexToRemove], p.activatedPlugins[indexToRemove+1:]...)
return nil
}
// Len returns the number of activate plugins
func (p *pluginContainer) Len() int {
return len(p.activatedPlugins)
}
// GetName returns the name of a plugin, if no GetName() implemented it returns an empty string ""
func (p *pluginContainer) GetName(plugin Plugin) string {
if pluginObj, ok := plugin.(pluginGetName); ok {
return pluginObj.GetName()
}
return ""
}
// GetDescription returns the name of a plugin, if no GetDescription() implemented it returns an empty string ""
func (p *pluginContainer) GetDescription(plugin Plugin) string {
if pluginObj, ok := plugin.(pluginGetDescription); ok {
return pluginObj.GetDescription()
}
return ""
}
// GetByName returns a plugin instance by it's name
func (p *pluginContainer) GetByName(pluginName string) Plugin {
if p.activatedPlugins == nil {
return nil
}
for i := range p.activatedPlugins {
if pluginObj, ok := p.activatedPlugins[i].(pluginGetName); ok {
if pluginObj.GetName() == pluginName {
return pluginObj
}
}
}
return nil
}
// GetAll returns all activated plugins
func (p *pluginContainer) GetAll() []Plugin {
return p.activatedPlugins
}
// GetDownloader returns the download manager
func (p *pluginContainer) GetDownloader() PluginDownloadManager {
// create it if and only if it used somewhere
if p.downloader == nil {
p.downloader = &pluginDownloadManager{}
}
return p.downloader
}
// Printf sends plain text to any registed logger (future), some plugins maybe want use this method
// maybe at the future I change it, instead of sync even-driven to async channels...
func (p *pluginContainer) Printf(format string, a ...interface{}) {
if p.logger != nil {
p.logger.Printf(format, a...) //for now just this.
}
}
// fire adds a fired event on the (statically type named) map and returns the new times
func (p *pluginContainer) fire(name string) int {
p.mu.Lock()
var times int
// maybe unnecessary but for clarity reasons
if t, found := p.fired[name]; found {
times = t
}
times++
p.fired[name] = times
p.mu.Unlock()
return times
}
// Fired receives an event name/plugin type and returns the times which this event is fired and how many plugins are fired this event,
// if zero then it's not fired at all
func (p *pluginContainer) Fired(name string) (times int) {
if t, found := p.fired[name]; found {
times = t
}
return
}
// PreLookup adds a PreLookup plugin-function to the plugin flow container
func (p *pluginContainer) PreLookup(fn PreLookupFunc) {
p.Add(fn)
}
// DoPreLookup raise all plugins which has the PreLookup method
func (p *pluginContainer) DoPreLookup(r Route) {
for i := range p.activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
if pluginObj, ok := p.activatedPlugins[i].(pluginPreLookup); ok {
// fire will add times to the number of events fired this event
p.fire("prelookup")
pluginObj.PreLookup(r)
}
}
}
// PreLookupFired returns true if PreLookup event/ plugin type is fired at least one time
func (p *pluginContainer) PreLookupFired() bool {
return p.Fired("prelookup") > 0
}
// PreBuild adds a PreBuild plugin-function to the plugin flow container
func (p *pluginContainer) PreBuild(fn PreBuildFunc) {
p.Add(fn)
}
// DoPreBuild raise all plugins that have the PreBuild method
func (p *pluginContainer) DoPreBuild(station *Framework) {
for i := range p.activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
if pluginObj, ok := p.activatedPlugins[i].(pluginPreBuild); ok {
pluginObj.PreBuild(station)
p.fire("prebuild")
}
}
}
// PreBuildFired returns true if PreBuild event/ plugin type is fired at least one time
func (p *pluginContainer) PreBuildFired() bool {
return p.Fired("prebuild") > 0
}
// PreListen adds a PreListen plugin-function to the plugin flow container
func (p *pluginContainer) PreListen(fn PreListenFunc) {
p.Add(fn)
}
// DoPreListen raise all plugins which has the PreListen method
func (p *pluginContainer) DoPreListen(station *Framework) {
for i := range p.activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
if pluginObj, ok := p.activatedPlugins[i].(pluginPreListen); ok {
pluginObj.PreListen(station)
p.fire("prelisten")
}
}
}
// DoPreListenParallel raise all PreListen plugins 'at the same time'
func (p *pluginContainer) DoPreListenParallel(station *Framework) {
var wg sync.WaitGroup
for _, plugin := range p.activatedPlugins {
wg.Add(1)
// check if this method exists on our plugin obj, these are optionaly and call it
go func(plugin Plugin) {
if pluginObj, ok := plugin.(pluginPreListen); ok {
pluginObj.PreListen(station)
p.fire("prelisten")
}
wg.Done()
}(plugin)
}
wg.Wait()
}
// PreListenFired returns true if PreListen or PreListenParallel event/ plugin type is fired at least one time
func (p *pluginContainer) PreListenFired() bool {
return p.Fired("prelisten") > 0
}
// PostListen adds a PostListen plugin-function to the plugin flow container
func (p *pluginContainer) PostListen(fn PostListenFunc) {
p.Add(fn)
}
// DoPostListen raise all plugins which has the DoPostListen method
func (p *pluginContainer) DoPostListen(station *Framework) {
for i := range p.activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
if pluginObj, ok := p.activatedPlugins[i].(pluginPostListen); ok {
pluginObj.PostListen(station)
p.fire("postlisten")
}
}
}
// PostListenFired returns true if PostListen event/ plugin type is fired at least one time
func (p *pluginContainer) PostListenFired() bool {
return p.Fired("postlisten") > 0
}
// PreClose adds a PreClose plugin-function to the plugin flow container
func (p *pluginContainer) PreClose(fn PreCloseFunc) {
p.Add(fn)
}
// DoPreClose raise all plugins which has the DoPreClose method
func (p *pluginContainer) DoPreClose(station *Framework) {
for i := range p.activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
if pluginObj, ok := p.activatedPlugins[i].(pluginPreClose); ok {
pluginObj.PreClose(station)
p.fire("preclose")
}
}
}
// PreCloseFired returns true if PreCLose event/ plugin type is fired at least one time
func (p *pluginContainer) PreCloseFired() bool {
return p.Fired("preclose") > 0
}
// PreDownload adds a PreDownload plugin-function to the plugin flow container
func (p *pluginContainer) PreDownload(fn PreDownloadFunc) {
p.Add(fn)
}
// DoPreDownload raise all plugins which has the DoPreDownload method
func (p *pluginContainer) DoPreDownload(pluginTryToDownload Plugin, downloadURL string) {
for i := range p.activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
if pluginObj, ok := p.activatedPlugins[i].(pluginPreDownload); ok {
pluginObj.PreDownload(pluginTryToDownload, downloadURL)
p.fire("predownload")
}
}
}
// PreDownloadFired returns true if PreDownload event/ plugin type is fired at least one time
func (p *pluginContainer) PreDownloadFired() bool {
return p.Fired("predownload") > 0
}