flow.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package approve_former
  2. import "strings"
  3. const flowTemplate = `apiVersion: argoproj.io/v1alpha1
  4. kind: WorkflowTemplate
  5. metadata:
  6. name: [[ .name ]]
  7. spec:
  8. entrypoint: approve-flow
  9. arguments:
  10. parameters:
  11. - name: object
  12. value: {}
  13. templates:
  14. - name: approve-flow
  15. inputs:
  16. parameters:
  17. - name: object
  18. dag:
  19. tasks:
  20. [[- if .childNodesContentLines ]]
  21. [[- range .childNodesContentLines ]]
  22. [[ . ]]
  23. [[- end ]]
  24. [[- else ]]
  25. - name: node
  26. template: approval
  27. [[- end ]]
  28. - name: approval
  29. suspend: {}
  30. inputs:
  31. parameters:
  32. - name: approve
  33. default: '通过'
  34. enum:
  35. - '通过'
  36. - '拒绝'
  37. - '忽略'
  38. outputs:
  39. parameters:
  40. - name: approve
  41. valueFrom:
  42. supplied: {}`
  43. type Flow struct {
  44. name string
  45. childNodes []Node
  46. }
  47. func NewFlow(name string, childNodes ...Node) *Flow {
  48. return &Flow{
  49. name: name,
  50. childNodes: childNodes,
  51. }
  52. }
  53. func (flow *Flow) GetDepends() string {
  54. return ""
  55. }
  56. func (flow *Flow) GetWhen() string {
  57. return ""
  58. }
  59. func (flow *Flow) AddNodes(childNodes ...Node) *Flow {
  60. if childNodes == nil || len(childNodes) == 0 {
  61. flow.childNodes = append(flow.childNodes, childNodes...)
  62. }
  63. return flow
  64. }
  65. func (flow *Flow) Render(_ Node) (string, error) {
  66. childNodesContents := make([]string, 0)
  67. for i, childNode := range flow.childNodes {
  68. var childNodeContent string
  69. if i == 0 {
  70. content, err := childNode.Render(nil)
  71. if err != nil {
  72. return "", err
  73. }
  74. childNodeContent = content
  75. } else {
  76. content, err := childNode.Render(flow.childNodes[i-1])
  77. if err != nil {
  78. return "", err
  79. }
  80. childNodeContent = content
  81. }
  82. childNodesContents = append(childNodesContents, strings.Split(childNodeContent, "\n")...)
  83. }
  84. return render("flow", flowTemplate, map[string]any{
  85. "name": flow.name,
  86. "childNodesContentLines": childNodesContents,
  87. })
  88. }