flow.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. outputs:
  38. parameters:
  39. - name: approve
  40. valueFrom:
  41. supplied: {}`
  42. type Flow struct {
  43. Name string
  44. ChildNodes []Node
  45. }
  46. func NewFlow(name string, childNodes ...Node) *Flow {
  47. return &Flow{
  48. Name: name,
  49. ChildNodes: childNodes,
  50. }
  51. }
  52. func (flow *Flow) GetDepends() string {
  53. return ""
  54. }
  55. func (flow *Flow) GetWhen() string {
  56. return ""
  57. }
  58. func (flow *Flow) Render(_ Node) (string, error) {
  59. childNodesContents := make([]string, 0)
  60. for i, childNode := range flow.ChildNodes {
  61. var childNodeContent string
  62. if i == 0 {
  63. content, err := childNode.Render(nil)
  64. if err != nil {
  65. return "", err
  66. }
  67. childNodeContent = content
  68. } else {
  69. content, err := childNode.Render(flow.ChildNodes[i-1])
  70. if err != nil {
  71. return "", err
  72. }
  73. childNodeContent = content
  74. }
  75. childNodesContents = append(childNodesContents, strings.Split(childNodeContent, "\n")...)
  76. }
  77. return render("flow", flowTemplate, map[string]any{
  78. "name": flow.Name,
  79. "childNodesContentLines": childNodesContents,
  80. })
  81. }