main.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "text/template"
  8. "gopkg.in/src-d/hercules.v3"
  9. "github.com/fatih/camelcase"
  10. )
  11. //go:generate go run embed.go
  12. func main() {
  13. var outputPath, name, varname, _flag, pkg string
  14. var printVersion bool
  15. flag.StringVar(&name, "n", "", "Name of the plugin, CamelCase. Required.")
  16. flag.StringVar(&outputPath, "o", "", "Output path of the generated plugin code. If not " +
  17. "specified, inferred from -n.")
  18. flag.StringVar(&varname, "varname", "", "Name of the plugin instance variable, If not " +
  19. "specified, inferred from -n.")
  20. flag.StringVar(&_flag, "flag", "", "Name of the plugin activation cmdline flag, If not " +
  21. "specified, inferred from -varname.")
  22. flag.BoolVar(&printVersion, "version", false, "Print version information and exit.")
  23. flag.StringVar(&pkg, "package", "contrib", "Name of the package.")
  24. flag.Parse()
  25. if printVersion {
  26. fmt.Printf("Version: 3\nGit: %s\n", hercules.GIT_HASH)
  27. return
  28. }
  29. if name == "" {
  30. fmt.Fprintln(os.Stderr, "-n must be specified")
  31. flag.PrintDefaults()
  32. os.Exit(1)
  33. }
  34. splitted := camelcase.Split(name)
  35. if outputPath == "" {
  36. outputPath = strings.ToLower(strings.Join(splitted, "_")) + ".go"
  37. }
  38. gen := template.Must(template.New("plugin").Parse(PLUGIN_TEMPLATE_SOURCE))
  39. outFile, err := os.Create(outputPath)
  40. if err != nil {
  41. panic(err)
  42. }
  43. defer outFile.Close()
  44. if varname == "" {
  45. varname = strings.ToLower(splitted[0])
  46. }
  47. if _flag == "" {
  48. _flag = varname
  49. }
  50. dict := map[string]string{"name": name, "varname": varname, "flag": _flag, "package": pkg}
  51. err = gen.Execute(outFile, dict)
  52. if err != nil {
  53. panic(err)
  54. }
  55. }