flow.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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: front_json-flow
  9. arguments:
  10. parameters:
  11. - name: object
  12. value: {}
  13. templates:
  14. - name: front_json-flow
  15. inputs:
  16. parameters:
  17. - name: object
  18. steps:
  19. [[- if .childNodesContentLines ]]
  20. [[- range .childNodesContentLines ]]
  21. [[ . ]]
  22. [[- end ]]
  23. [[- else ]]
  24. - name: parse_flow
  25. template: approval
  26. [[- end ]]
  27. - name: approval
  28. suspend: {}
  29. inputs:
  30. parameters:
  31. - name: front_json
  32. default: '通过'
  33. enum:
  34. - '通过'
  35. - '拒绝'
  36. - '忽略'
  37. outputs:
  38. parameters:
  39. - name: front_json
  40. valueFrom:
  41. supplied: {}`
  42. type Flow struct {
  43. name string
  44. childNodes []Noder
  45. }
  46. func NewFlow(name string, childNodes ...Noder) *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) AddNodes(childNodes ...Noder) *Flow {
  59. if childNodes == nil || len(childNodes) == 0 {
  60. flow.childNodes = append(flow.childNodes, childNodes...)
  61. }
  62. return flow
  63. }
  64. func (flow *Flow) Render(_ Noder) (string, error) {
  65. childNodesContents := make([]string, 0)
  66. for i, childNode := range flow.childNodes {
  67. var childNodeContent string
  68. if i == 0 {
  69. content, err := childNode.Render(nil)
  70. if err != nil {
  71. return "", err
  72. }
  73. childNodeContent = content
  74. } else {
  75. content, err := childNode.Render(flow.childNodes[i-1])
  76. if err != nil {
  77. return "", err
  78. }
  79. childNodeContent = content
  80. }
  81. childNodesContents = append(childNodesContents, strings.Split(childNodeContent, "\n")...)
  82. }
  83. return render("flow", flowTemplate, map[string]any{
  84. "name": flow.name,
  85. "childNodesContentLines": childNodesContents,
  86. })
  87. }