registry.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package core
  2. import (
  3. "fmt"
  4. "reflect"
  5. "sort"
  6. "strings"
  7. "unsafe"
  8. "github.com/spf13/cobra"
  9. "github.com/spf13/pflag"
  10. )
  11. // PipelineItemRegistry contains all the known PipelineItem-s.
  12. type PipelineItemRegistry struct {
  13. provided map[string][]reflect.Type
  14. registered map[string]reflect.Type
  15. flags map[string]reflect.Type
  16. featureFlags arrayFeatureFlags
  17. }
  18. // Register adds another PipelineItem to the registry.
  19. func (registry *PipelineItemRegistry) Register(example PipelineItem) {
  20. t := reflect.TypeOf(example)
  21. registry.registered[example.Name()] = t
  22. if fpi, ok := example.(LeafPipelineItem); ok {
  23. registry.flags[fpi.Flag()] = t
  24. }
  25. for _, dep := range example.Provides() {
  26. ts := registry.provided[dep]
  27. if ts == nil {
  28. ts = []reflect.Type{}
  29. }
  30. ts = append(ts, t)
  31. registry.provided[dep] = ts
  32. }
  33. }
  34. // Summon searches for PipelineItem-s which provide the specified entity or named after
  35. // the specified string. It materializes all the found types and returns them.
  36. func (registry *PipelineItemRegistry) Summon(providesOrName string) []PipelineItem {
  37. if registry.provided == nil {
  38. return nil
  39. }
  40. ts := registry.provided[providesOrName]
  41. var items []PipelineItem
  42. for _, t := range ts {
  43. items = append(items, reflect.New(t.Elem()).Interface().(PipelineItem))
  44. }
  45. if t, exists := registry.registered[providesOrName]; exists {
  46. items = append(items, reflect.New(t.Elem()).Interface().(PipelineItem))
  47. }
  48. return items
  49. }
  50. // GetLeaves returns all LeafPipelineItem-s registered.
  51. func (registry *PipelineItemRegistry) GetLeaves() []LeafPipelineItem {
  52. keys := []string{}
  53. for key := range registry.flags {
  54. keys = append(keys, key)
  55. }
  56. sort.Strings(keys)
  57. items := []LeafPipelineItem{}
  58. for _, key := range keys {
  59. items = append(items, reflect.New(registry.flags[key].Elem()).Interface().(LeafPipelineItem))
  60. }
  61. return items
  62. }
  63. // GetPlumbingItems returns all non-LeafPipelineItem-s registered.
  64. func (registry *PipelineItemRegistry) GetPlumbingItems() []PipelineItem {
  65. keys := []string{}
  66. for key := range registry.registered {
  67. keys = append(keys, key)
  68. }
  69. sort.Strings(keys)
  70. items := []PipelineItem{}
  71. for _, key := range keys {
  72. iface := reflect.New(registry.registered[key].Elem()).Interface()
  73. if _, ok := iface.(LeafPipelineItem); !ok {
  74. items = append(items, iface.(PipelineItem))
  75. }
  76. }
  77. return items
  78. }
  79. type orderedFeaturedItems []FeaturedPipelineItem
  80. func (ofi orderedFeaturedItems) Len() int {
  81. return len([]FeaturedPipelineItem(ofi))
  82. }
  83. func (ofi orderedFeaturedItems) Less(i, j int) bool {
  84. cofi := []FeaturedPipelineItem(ofi)
  85. return cofi[i].Name() < cofi[j].Name()
  86. }
  87. func (ofi orderedFeaturedItems) Swap(i, j int) {
  88. cofi := []FeaturedPipelineItem(ofi)
  89. cofi[i], cofi[j] = cofi[j], cofi[i]
  90. }
  91. // GetFeaturedItems returns all FeaturedPipelineItem-s registered.
  92. func (registry *PipelineItemRegistry) GetFeaturedItems() map[string][]FeaturedPipelineItem {
  93. features := map[string][]FeaturedPipelineItem{}
  94. for _, t := range registry.registered {
  95. if fiface, ok := reflect.New(t.Elem()).Interface().(FeaturedPipelineItem); ok {
  96. for _, f := range fiface.Features() {
  97. list := features[f]
  98. if list == nil {
  99. list = []FeaturedPipelineItem{}
  100. }
  101. list = append(list, fiface)
  102. features[f] = list
  103. }
  104. }
  105. }
  106. for _, vals := range features {
  107. sort.Sort(orderedFeaturedItems(vals))
  108. }
  109. return features
  110. }
  111. var pathFlagTypeMasquerade bool
  112. // EnablePathFlagTypeMasquerade changes the type of all "path" command line arguments from "string"
  113. // to "path". This operation cannot be canceled and is intended to be used for better --help output.
  114. func EnablePathFlagTypeMasquerade() {
  115. pathFlagTypeMasquerade = true
  116. }
  117. type pathValue struct {
  118. origin pflag.Value
  119. }
  120. func wrapPathValue(val pflag.Value) pflag.Value {
  121. return &pathValue{val}
  122. }
  123. func (s *pathValue) Set(val string) error {
  124. return s.origin.Set(val)
  125. }
  126. func (s *pathValue) Type() string {
  127. if pathFlagTypeMasquerade {
  128. return "path"
  129. }
  130. return "string"
  131. }
  132. func (s *pathValue) String() string {
  133. return s.origin.String()
  134. }
  135. // PathifyFlagValue changes the type of a string command line argument to "path".
  136. func PathifyFlagValue(flag *pflag.Flag) {
  137. flag.Value = wrapPathValue(flag.Value)
  138. }
  139. type arrayFeatureFlags struct {
  140. // Flags contains the features activated through the command line.
  141. Flags []string
  142. // Choices contains all registered features.
  143. Choices map[string]bool
  144. }
  145. func (acf *arrayFeatureFlags) String() string {
  146. return strings.Join([]string(acf.Flags), ", ")
  147. }
  148. func (acf *arrayFeatureFlags) Set(value string) error {
  149. if _, exists := acf.Choices[value]; !exists {
  150. return fmt.Errorf("feature \"%s\" is not registered", value)
  151. }
  152. acf.Flags = append(acf.Flags, value)
  153. return nil
  154. }
  155. func (acf *arrayFeatureFlags) Type() string {
  156. return "string"
  157. }
  158. // AddFlags inserts the cmdline options from PipelineItem.ListConfigurationOptions(),
  159. // FeaturedPipelineItem().Features() and LeafPipelineItem.Flag() into the global "flag" parser
  160. // built into the Go runtime.
  161. // Returns the "facts" which can be fed into PipelineItem.Configure() and the dictionary of
  162. // runnable analysis (LeafPipelineItem) choices. E.g. if "BurndownAnalysis" was activated
  163. // through "-burndown" cmdline argument, this mapping would contain ["BurndownAnalysis"] = *true.
  164. func (registry *PipelineItemRegistry) AddFlags(flagSet *pflag.FlagSet) (
  165. map[string]interface{}, map[string]*bool) {
  166. flags := map[string]interface{}{}
  167. deployed := map[string]*bool{}
  168. for name, it := range registry.registered {
  169. formatHelp := func(desc string) string {
  170. return fmt.Sprintf("%s [%s]", desc, name)
  171. }
  172. itemIface := reflect.New(it.Elem()).Interface()
  173. for _, opt := range itemIface.(PipelineItem).ListConfigurationOptions() {
  174. var iface interface{}
  175. getPtr := func() unsafe.Pointer {
  176. return unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface))
  177. }
  178. switch opt.Type {
  179. case BoolConfigurationOption:
  180. iface = interface{}(true)
  181. ptr := (**bool)(getPtr())
  182. *ptr = flagSet.Bool(opt.Flag, opt.Default.(bool), formatHelp(opt.Description))
  183. case IntConfigurationOption:
  184. iface = interface{}(0)
  185. ptr := (**int)(getPtr())
  186. *ptr = flagSet.Int(opt.Flag, opt.Default.(int), formatHelp(opt.Description))
  187. case StringConfigurationOption, PathConfigurationOption:
  188. iface = interface{}("")
  189. ptr := (**string)(getPtr())
  190. *ptr = flagSet.String(opt.Flag, opt.Default.(string), formatHelp(opt.Description))
  191. if opt.Type == PathConfigurationOption {
  192. err := cobra.MarkFlagFilename(flagSet, opt.Flag)
  193. if err != nil {
  194. panic(err)
  195. }
  196. PathifyFlagValue(flagSet.Lookup(opt.Flag))
  197. }
  198. case FloatConfigurationOption:
  199. iface = interface{}(float32(0))
  200. ptr := (**float32)(getPtr())
  201. *ptr = flagSet.Float32(opt.Flag, opt.Default.(float32), formatHelp(opt.Description))
  202. case StringsConfigurationOption:
  203. iface = interface{}([]string{})
  204. ptr := (**[]string)(getPtr())
  205. *ptr = flagSet.StringSlice(opt.Flag, opt.Default.([]string), formatHelp(opt.Description))
  206. }
  207. flags[opt.Name] = iface
  208. }
  209. if fpi, ok := itemIface.(FeaturedPipelineItem); ok {
  210. for _, f := range fpi.Features() {
  211. registry.featureFlags.Choices[f] = true
  212. }
  213. }
  214. if fpi, ok := itemIface.(LeafPipelineItem); ok {
  215. deployed[fpi.Name()] = flagSet.Bool(
  216. fpi.Flag(), false, fmt.Sprintf("Runs %s analysis.", fpi.Name()))
  217. }
  218. }
  219. {
  220. // Pipeline flags
  221. iface := interface{}("")
  222. ptr1 := (**string)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  223. *ptr1 = flagSet.String("dump-dag", "", "Write the pipeline DAG to a Graphviz file.")
  224. flags[ConfigPipelineDAGPath] = iface
  225. PathifyFlagValue(flagSet.Lookup("dump-dag"))
  226. iface = interface{}(true)
  227. ptr2 := (**bool)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  228. *ptr2 = flagSet.Bool("dry-run", false, "Do not run any analyses - only resolve the DAG. "+
  229. "Useful for --dump-dag or --dump-plan.")
  230. flags[ConfigPipelineDryRun] = iface
  231. iface = interface{}(true)
  232. ptr3 := (**bool)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  233. *ptr3 = flagSet.Bool("dump-plan", false, "Print the pipeline execution plan to stderr.")
  234. flags[ConfigPipelineDumpPlan] = iface
  235. iface = interface{}(0)
  236. ptr4 := (**int)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  237. *ptr4 = flagSet.Int("hibernation-distance", 0,
  238. "Minimum number of actions between two sequential usages of a branch to activate "+
  239. "the hibernation optimization (cpu-memory trade-off). 0 disables.")
  240. flags[ConfigPipelineHibernationDistance] = iface
  241. iface = interface{}(true)
  242. ptr5 := (**bool)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  243. *ptr5 = flagSet.Bool("print-actions", false, "Print the executed actions to stderr.")
  244. flags[ConfigPipelinePrintActions] = iface
  245. }
  246. var features []string
  247. for f := range registry.featureFlags.Choices {
  248. features = append(features, f)
  249. }
  250. flagSet.Var(&registry.featureFlags, "feature",
  251. fmt.Sprintf("Enables the items which depend on the specified features. Can be specified "+
  252. "multiple times. Available features: [%s] (see --feature below).",
  253. strings.Join(features, ", ")))
  254. return flags, deployed
  255. }
  256. // Registry contains all known pipeline item types.
  257. var Registry = &PipelineItemRegistry{
  258. provided: map[string][]reflect.Type{},
  259. registered: map[string]reflect.Type{},
  260. flags: map[string]reflect.Type{},
  261. featureFlags: arrayFeatureFlags{Flags: []string{}, Choices: map[string]bool{}},
  262. }