flow.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. templates:
  10. - name: approve-flow
  11. dag:
  12. tasks:
  13. [[- if .childNodesContentLines ]]
  14. [[- range .childNodesContentLines ]]
  15. [[ . ]]
  16. [[- end ]]
  17. [[- else ]]
  18. - name: node
  19. template: approval
  20. [[- end ]]
  21. - name: approval
  22. suspend: {}
  23. inputs:
  24. parameters:
  25. - name: approve
  26. default: '通过'
  27. enum:
  28. - '通过'
  29. - '拒绝'
  30. outputs:
  31. parameters:
  32. - name: approve
  33. valueFrom:
  34. supplied: {}`
  35. type Flow struct {
  36. Name string
  37. ChildNodes []Node
  38. }
  39. func NewFlow(name string, childNodes ...Node) *Flow {
  40. return &Flow{
  41. Name: name,
  42. ChildNodes: childNodes,
  43. }
  44. }
  45. func (flow *Flow) GetDepends() string {
  46. return ""
  47. }
  48. func (flow *Flow) GetWhen() string {
  49. return ""
  50. }
  51. func (flow *Flow) Render(_ Node) (string, error) {
  52. childNodesContents := make([]string, 0)
  53. for i, childNode := range flow.ChildNodes {
  54. var childNodeContent string
  55. if i == 0 {
  56. content, err := childNode.Render(nil)
  57. if err != nil {
  58. return "", err
  59. }
  60. childNodeContent = content
  61. } else {
  62. content, err := childNode.Render(flow.ChildNodes[i-1])
  63. if err != nil {
  64. return "", err
  65. }
  66. childNodeContent = content
  67. }
  68. childNodesContents = append(childNodesContents, strings.Split(childNodeContent, "\n")...)
  69. }
  70. return render("flow", flowTemplate, map[string]any{
  71. "name": flow.Name,
  72. "childNodesContentLines": childNodesContents,
  73. })
  74. }