plugin.template 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Hercules plugin "{{.name}}"
  2. //
  3. // How to build: execute "make" *OR*
  4. //
  5. // 1. Update the Protocol Buffers definition as needed, regenerate {{.protogo}}
  6. //
  7. // PATH=$PATH:$GOPATH/bin protoc --gogo_out={{.outdir}} --proto_path={{.outdir}} {{.proto}}
  8. //
  9. // 2. Build {{.shlib}}
  10. //
  11. // go build -buildmode=plugin {{.output}} {{.protogo}}
  12. //
  13. // Step (1) requires GoGo Protobuf https://github.com/gogo/protobuf
  14. //
  15. // Usage:
  16. //
  17. // hercules -plugin {{.shlib}} -{{.flag}}
  18. // hercules -plugin {{.shlib}} -help
  19. package {{.package}}
  20. import (
  21. "io"
  22. "github.com/gogo/protobuf/proto"
  23. "gopkg.in/src-d/go-git.v4"
  24. "gopkg.in/src-d/hercules.v3"
  25. )
  26. // {{.name}} contains the intermediate state which is mutated by Consume(). It should implement
  27. // hercules.LeafPipelineItem.
  28. type {{.name}} struct {
  29. }
  30. // {{.name}}Result is returned by Finalize() and represents the analysis result.
  31. type {{.name}}Result struct {
  32. }
  33. // Analysis' name in the graph is usually the same as the type's name, however, does not have to.
  34. func ({{.varname}} *{{.name}}) Name() string {
  35. return "{{.name}}"
  36. }
  37. // LeafPipelineItem-s normally do not act as intermediate nodes and thus we return an empty slice.
  38. func ({{.varname}} *{{.name}}) Provides() []string {
  39. return []string{}
  40. }
  41. // Requires returns the list of dependencies which must be supplied in Consume().
  42. func ({{.varname}} *{{.name}}) Requires() []string {
  43. arr := [...]string{/* insert dependencies here */}
  44. return arr[:]
  45. }
  46. // ListConfigurationOptions tells the engine which parameters can be changed through the command
  47. // line.
  48. func ({{.varname}} *{{.name}}) ListConfigurationOptions() []hercules.ConfigurationOption {
  49. opts := [...]hercules.ConfigurationOption{ /* {
  50. Name: "ParameterName",
  51. Description: "Parameter's description.",
  52. Flag: "my-cmdline-flag",
  53. Type: hercules.BoolConfigurationOption,
  54. Default: false}, */
  55. }
  56. return opts[:]
  57. }
  58. // Flag returns the command line switch which activates the analysis.
  59. func ({{.varname}} *{{.name}}) Flag() string {
  60. return "{{.flag}}"
  61. }
  62. // Configure applies the parameters specified in the command line. Map keys correspond to "Name".
  63. func ({{.varname}} *{{.name}}) Configure(facts map[string]interface{}) {
  64. }
  65. // Initialize resets the internal temporary data structures and prepares the object for Consume().
  66. func ({{.varname}} *{{.name}}) Initialize(repository *git.Repository) {
  67. }
  68. // Consume is called for every commit in the sequence.
  69. func ({{.varname}} *{{.name}}) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  70. return nil, nil
  71. }
  72. // Finalize produces the result of the analysis. No more Consume() calls are expected afterwards.
  73. func ({{.varname}} *{{.name}}) Finalize() interface{} {
  74. result := {{.name}}Result{}
  75. // insert code here
  76. return result
  77. }
  78. // Serialize converts the result from Finalize() to either Protocol Buffers or YAML.
  79. func ({{.varname}} *{{.name}}) Serialize(result interface{}, binary bool, writer io.Writer) error {
  80. {{.varname}}Result := result.({{.name}}Result)
  81. if binary {
  82. return {{.varname}}.serializeBinary(&{{.varname}}Result, writer)
  83. }
  84. {{.varname}}.serializeText(&{{.varname}}Result, writer)
  85. return nil
  86. }
  87. func ({{.varname}} *{{.name}}) serializeText(result *{{.name}}Result, writer io.Writer) {
  88. // write YAML to writer
  89. }
  90. func ({{.varname}} *{{.name}}) serializeBinary(result *{{.name}}Result, writer io.Writer) error {
  91. message := {{.name}}ResultMessage{
  92. // fill me
  93. }
  94. serialized, err := proto.Marshal(&message)
  95. if err != nil {
  96. return err
  97. }
  98. writer.Write(serialized)
  99. return nil
  100. }
  101. func init() {
  102. hercules.Registry.Register(&{{.name}}{})
  103. }