Dan Willemsen | fdeb724 | 2015-07-24 16:53:27 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "flag" |
| 20 | "fmt" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
| 24 | "text/template" |
| 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | output = flag.String("o", "", "output filename") |
| 29 | pkg = flag.String("p", "main", "package name") |
| 30 | ) |
| 31 | |
| 32 | func main() { |
| 33 | flag.Parse() |
| 34 | |
| 35 | if flag.NArg() == 0 { |
| 36 | fmt.Fprintln(os.Stderr, "error: must pass at least one input") |
| 37 | os.Exit(1) |
| 38 | } |
| 39 | |
| 40 | buf := &bytes.Buffer{} |
| 41 | |
| 42 | err := pluginTmpl.Execute(buf, struct { |
| 43 | Package string |
| 44 | Plugins []string |
| 45 | }{ |
| 46 | filepath.Base(*pkg), |
| 47 | flag.Args(), |
| 48 | }) |
| 49 | if err != nil { |
| 50 | panic(err) |
| 51 | } |
| 52 | |
| 53 | err = ioutil.WriteFile(*output, buf.Bytes(), 0666) |
| 54 | if err != nil { |
| 55 | panic(err) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | var pluginTmpl = template.Must(template.New("pluginloader").Parse(` |
| 60 | package {{.Package}} |
| 61 | |
| 62 | import ( |
| 63 | {{range .Plugins}} |
| 64 | _ "{{.}}" |
| 65 | {{end}} |
| 66 | ) |
| 67 | `)) |