flow.go 2.5 KB

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