Skip to content

Commit 8809069

Browse files
committed
auto discovery plugins
1 parent 7ace1f5 commit 8809069

File tree

7 files changed

+262
-23
lines changed

7 files changed

+262
-23
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ vendor/
5050
*.so
5151
/test/
5252
cmd/genfastjson/genfastjson
53+
/config/generated.go
54+
/config/generated_plugins.go

Makefile

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
SHELL=/bin/bash
22

33
# APP info
4-
APP_NAME?=${APP_NAME}
5-
APP_VERSION?=${APP_VERSION}
4+
APP_NAME?= framework
5+
APP_VERSION?= 1.0.0_SNAPSHOT
66
APP_CONFIG := $(APP_NAME).yml
77
APP_EOLDate ?= "2023-12-31 10:10:10"
88
APP_STATIC_FOLDER ?= .public
99
APP_STATIC_PACKAGE ?= public
1010
APP_UI_FOLDER ?= ui
11-
APP_PLUGIN_FOLDER ?= plugin
11+
APP_PLUGIN_FOLDER ?= plugins
1212

1313
# Get release version from environment
1414
ifneq "$(VERSION)" ""
@@ -85,6 +85,10 @@ build-cmd: config
8585
@$(MAKE) restore-generated-file
8686

8787

88+
update-plugins:
89+
(cd ~/go/src/infini.sh/framework/ && make build-cmd)
90+
(~/go/src/infini.sh/framework/bin/plugin-discovery -dir $(APP_PLUGIN_FOLDER) -pkg config -import_prefix infini.sh/$(APP_NAME) -out config/generated_plugins.go)
91+
8892
# used to build the binary for gdb debugging
8993
build-race: clean config update-vfs
9094
$(GOBUILDNCGO) -gcflags "-m -N -l" -race -o $(OUTPUT_DIR)/$(APP_NAME)

cmd/plugin-discovery/main.go

+244
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package main
19+
20+
import (
21+
"bufio"
22+
"bytes"
23+
"flag"
24+
"fmt"
25+
"infini.sh/framework/core/errors"
26+
"io/ioutil"
27+
"log"
28+
"os"
29+
"path/filepath"
30+
"regexp"
31+
"sort"
32+
"strings"
33+
"text/template"
34+
)
35+
36+
var usageText = `
37+
Usage: plugin-discovery [flags]
38+
plugin-discovery is a tool that auto discovery module or plugins, generate a go file that include in main.go
39+
Options:
40+
`[1:]
41+
42+
var (
43+
pkg string
44+
importPrefix string
45+
outFile string
46+
pluginDirs stringSliceFlag
47+
)
48+
49+
func init() {
50+
flag.Var(&pluginDirs, "dir", "Directory to search for plugins")
51+
flag.StringVar(&importPrefix, "import_prefix", "infini.sh/gateway/", "Prefix for generated package path")
52+
flag.StringVar(&pkg, "pkg", "config", "Package name for generated go file")
53+
flag.StringVar(&outFile, "out", "config/plugins.go", "Output filename")
54+
flag.Usage = usageFlag
55+
}
56+
57+
func main() {
58+
log.SetFlags(0)
59+
flag.Parse()
60+
61+
if len(pluginDirs) == 0 {
62+
log.Fatal("Dir is required")
63+
}
64+
65+
//// Get the current directories Go import path.
66+
//repo, err := devtools.GetProjectRepoInfo()
67+
//if err != nil {
68+
// log.Fatalf("Failed to determine import path: %v", err)
69+
//}
70+
71+
// Build import paths.
72+
var imports []string
73+
for _, dir := range pluginDirs {
74+
75+
//fmt.Println("handling dir:",dir)
76+
// Skip packages without an init() function because that cannot register
77+
// anything as a side-effect of being imported (e.g. filebeat/input/file).
78+
79+
//var foundInitMethod bool
80+
//goFiles, err := filepath.Glob(filepath.Join(dir, "*.go"))
81+
82+
libRegEx, e := regexp.Compile(".*.go$")
83+
if e != nil {
84+
log.Fatal(e)
85+
}
86+
87+
e = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
88+
89+
//fmt.Println(info.Name())
90+
if err == nil && libRegEx.MatchString(info.Name()) {
91+
92+
if strings.HasSuffix(info.Name(), "_test.go") {
93+
return nil
94+
}
95+
if hasInitMethod(filepath.Join(path)) {
96+
//foundInitMethod = true
97+
fmt.Println(path)
98+
//fmt.Println(info.Name())
99+
100+
imports = append(imports, filepath.ToSlash(
101+
filepath.Join(importPrefix, filepath.Dir(path))))
102+
103+
return nil
104+
}
105+
}
106+
return nil
107+
})
108+
if e != nil {
109+
log.Fatal(e)
110+
}
111+
112+
113+
//
114+
//goFiles, err := filepath.Glob(filepath.Join(dir, "*.go"))
115+
//if err != nil {
116+
// log.Fatalf("Failed checking for .go files in package dir: %v", err)
117+
//}
118+
//for _, f := range goFiles {
119+
// fmt.Println("go:",f)
120+
// // Skip test files
121+
// if strings.HasSuffix(f, "_test.go") {
122+
// continue
123+
// }
124+
// if hasInitMethod(f) {
125+
// foundInitMethod = true
126+
// break
127+
// }
128+
//}
129+
//if !foundInitMethod {
130+
// continue
131+
//}
132+
133+
//importDir := dir
134+
//if filepath.IsAbs(dir) {
135+
// // Make it relative to the current package if it's absolute.
136+
// importDir, err = filepath.Rel(devtools.CWD(), dir)
137+
// if err != nil {
138+
// log.Fatalf("Failure creating import for dir=%v: %v", dir, err)
139+
// }
140+
//}
141+
142+
//imports = append(imports, filepath.ToSlash(
143+
// filepath.Join(repo.ImportPath, importDir)))
144+
//
145+
//imports = append(imports, filepath.ToSlash(
146+
// filepath.Join("", importDir)))
147+
}
148+
149+
sort.Strings(imports)
150+
151+
fmt.Println("imports:",imports)
152+
153+
// Populate the template.
154+
var buf bytes.Buffer
155+
err := Template.Execute(&buf, Data{
156+
Package: pkg,
157+
Imports: imports,
158+
})
159+
if err != nil {
160+
log.Fatalf("Failed executing template: %v", err)
161+
}
162+
163+
// Create the output directory.
164+
if err = os.MkdirAll(filepath.Dir(outFile), 0755); err != nil {
165+
log.Fatalf("Failed to create output directory: %v", err)
166+
}
167+
168+
// Write the output file.
169+
if err = ioutil.WriteFile(outFile, buf.Bytes(), 0644); err != nil {
170+
log.Fatalf("Failed writing output file: %v", err)
171+
}
172+
}
173+
174+
func usageFlag() {
175+
fmt.Fprintf(os.Stderr, usageText)
176+
flag.PrintDefaults()
177+
}
178+
179+
var Template = template.Must(template.New("normalizations").Funcs(map[string]interface{}{
180+
"trim": strings.TrimSpace,
181+
}).Parse(
182+
`/// GENERATED CODE BY PLUGIN DISCOVERY- DO NOT EDIT.
183+
184+
package {{ .Package }}
185+
186+
import (
187+
{{- range $import := .Imports }}
188+
_ "{{ $import }}"
189+
{{- end }}
190+
)
191+
`[1:]))
192+
193+
type Data struct {
194+
Package string
195+
Imports []string
196+
}
197+
198+
//stringSliceFlag is a flag type that allows more than one value to be specified.
199+
type stringSliceFlag []string
200+
201+
func (f *stringSliceFlag) String() string { return strings.Join(*f, ",") }
202+
203+
func (f *stringSliceFlag) Set(value string) error {
204+
*f = append(*f, value)
205+
return nil
206+
}
207+
208+
// hasInitMethod returns true if the file contains 'func init()'.
209+
func hasInitMethod(file string) bool {
210+
211+
//fmt.Println("checking:",file)
212+
213+
f, err := os.Open(file)
214+
if err != nil {
215+
log.Fatalf("Failed to read from %v: %v", file, err)
216+
}
217+
defer f.Close()
218+
219+
var initSignature = []byte("func init()")
220+
scanner := bufio.NewScanner(f)
221+
for scanner.Scan() {
222+
if bytes.Contains(scanner.Bytes(), initSignature) {
223+
return true
224+
}
225+
}
226+
if err := scanner.Err(); err != nil {
227+
log.Fatalf("Failed scanning %v: %v", file, err)
228+
}
229+
return false
230+
}
231+
232+
233+
// FindFiles return a list of file matching the given glob patterns.
234+
func FindFiles(globs ...string) ([]string, error) {
235+
var configFiles []string
236+
for _, glob := range globs {
237+
files, err := filepath.Glob(glob)
238+
if err != nil {
239+
return nil, errors.Wrapf(err, "failed on glob %v", glob)
240+
}
241+
configFiles = append(configFiles, files...)
242+
}
243+
return configFiles, nil
244+
}

cmd/plugin/main.go

-17
This file was deleted.

core/pipeline/filter.go

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func (procs *Filters) Filter(ctx *fasthttp.RequestCtx) {
151151
func (procs *Filters) Name() string {
152152
return "filters"
153153
}
154+
154155
func (procs *Filters) String() string {
155156
var s []string
156157
for _, p := range procs.List {

core/pipeline/register.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,22 @@ type Constructor func(config *config.Config) (ProcessorBase, error)
243243
var registry = NewNamespace()
244244

245245
func RegisterProcessorPlugin(name string, constructor ProcessorConstructor) {
246-
log.Debugf("register processor: %s", name)
247246
err := registry.RegisterProcessor(name, constructor)
248247
if err != nil {
249248
panic(err)
250249
}
251250
}
252251

253252
func RegisterFilterPlugin(name string, constructor FilterConstructor) {
254-
log.Debugf("register filter: %s", name)
255-
err := registry.RegisterFilter(name, constructor)
253+
err := registry.RegisterFilter(name, constructor)
256254
if err != nil {
257255
panic(err)
258256
}
259257
}
258+
259+
260+
func GetFilterMetadata(){
261+
for k,_:=range registry.filterReg{
262+
log.Error(k)
263+
}
264+
}

framework.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)