|
| 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 | +} |
0 commit comments