flow.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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) Render(_ Node) (string, error) {
  60. childNodesContents := make([]string, 0)
  61. for i, childNode := range flow.ChildNodes {
  62. var childNodeContent string
  63. if i == 0 {
  64. content, err := childNode.Render(nil)
  65. if err != nil {
  66. return "", err
  67. }
  68. childNodeContent = content
  69. } else {
  70. content, err := childNode.Render(flow.ChildNodes[i-1])
  71. if err != nil {
  72. return "", err
  73. }
  74. childNodeContent = content
  75. }
  76. childNodesContents = append(childNodesContents, strings.Split(childNodeContent, "\n")...)
  77. }
  78. return render("flow", flowTemplate, map[string]any{
  79. "name": flow.Name,
  80. "childNodesContentLines": childNodesContents,
  81. })
  82. }