plugin.template 3.6 KB

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