-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugintypes.go
147 lines (142 loc) · 4.35 KB
/
plugintypes.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
package devportal
import (
"fmt"
"go/ast"
"go/token"
"log"
"strings"
)
// PluginType contains information about a kind of plugin
// that is supported by this system.
type PluginType struct {
ID string // lower-case, underscore_spaces
Name string // singular, human-readable name for this plugin type
CategoryTitle string // human-readable title for plugin listings
Description string // concise description of what these plugins are/do
// Name of package and function, as in a function call: "package.Function()"
Package string
Function string
// GetInfo takes a function call and is able to extract
// the relevant information from it into a Plugin.
// However, GetInfo is not able to label the Plugin
// with the type of plugin this is (initialization loop).
// The caller should do that.
GetInfo func(*token.FileSet, PluginCallExpr) (Plugin, error) `json:"-"`
}
// pluginTypes is the list of plugin types that are
// understood by the system.
var pluginTypes = []PluginType{
{
ID: "server_type",
Name: "Server Type",
CategoryTitle: "Server Types",
Description: "Things Caddy can serve",
Package: "caddy",
Function: "RegisterServerType",
GetInfo: func(fset *token.FileSet, call PluginCallExpr) (Plugin, error) {
var info Plugin
if len(call.CallExpr.Args) < 1 {
return info, fmt.Errorf("not enough arguments")
}
pname, err := staticEval(fset, call, call.CallExpr.Args[0])
if err != nil {
log.Println("ERROR:", err)
}
pluginName := pname
info.Name = strings.ToLower(pluginName)
return info, nil
},
},
{
ID: "generic",
Name: "Directive",
CategoryTitle: "Directives/Middleware",
Description: "Extra functionality with the Caddyfile",
Package: "caddy",
Function: "RegisterPlugin",
GetInfo: func(fset *token.FileSet, call PluginCallExpr) (Plugin, error) {
var info Plugin
if len(call.CallExpr.Args) != 2 {
return info, fmt.Errorf("not enough arguments")
}
pname, err := staticEval(fset, call, call.CallExpr.Args[0])
if err != nil {
log.Println("ERROR:", err)
}
pluginName := strings.Trim(pname, "\"")
elts := call.CallExpr.Args[1].(*ast.CompositeLit).Elts
for _, elt := range elts {
keyVal := elt.(*ast.KeyValueExpr)
if keyVal.Key.(*ast.Ident).Name == "ServerType" {
stname, err := staticEval(fset, call, keyVal.Value)
if err != nil {
log.Println("ERROR(2):", err)
}
pluginName = stname + "." + pluginName
}
}
info.Name = strings.ToLower(pluginName)
return info, nil
},
},
{
ID: "event_hook",
Name: "Event Hook",
CategoryTitle: "Event Hooks",
Description: "Plugins that are triggered by events",
Package: "caddy",
Function: "RegisterEventHook",
GetInfo: func(fset *token.FileSet, call PluginCallExpr) (Plugin, error) {
var info Plugin
if len(call.CallExpr.Args) < 1 {
return info, fmt.Errorf("not enough arguments")
}
pname, err := staticEval(fset, call, call.CallExpr.Args[0])
if err != nil {
log.Println("ERROR:", err)
}
info.Name = strings.ToLower("hook." + pname)
return info, nil
},
},
{
ID: "caddyfile_loader",
Name: "Caddyfile Loader",
CategoryTitle: "Caddyfile Loaders",
Description: "Ways to load the Caddyfile",
Package: "caddy",
Function: "RegisterCaddyfileLoader",
GetInfo: func(fset *token.FileSet, call PluginCallExpr) (Plugin, error) {
var info Plugin
if len(call.CallExpr.Args) < 1 {
return info, fmt.Errorf("not enough arguments")
}
pname, err := staticEval(fset, call, call.CallExpr.Args[0])
if err != nil {
log.Println("ERROR:", err)
}
info.Name = strings.ToLower(pname)
return info, nil
},
},
{
ID: "tls_dns_provider",
Name: "DNS Provider",
CategoryTitle: "DNS Providers",
Description: "Obtain certificates using DNS",
Package: "caddytls",
Function: "RegisterDNSProvider",
GetInfo: func(fset *token.FileSet, call PluginCallExpr) (Plugin, error) {
var info Plugin
if len(call.CallExpr.Args) < 1 {
return info, fmt.Errorf("not enough arguments")
}
pname, err := staticEval(fset, call, call.CallExpr.Args[0])
if err != nil {
log.Println("ERROR:", err)
}
info.Name = strings.ToLower("tls.dns." + pname)
return info, nil
},
},
}