package approve_former import "strings" const flowTemplate = `apiVersion: argoproj.io/v1alpha1 kind: WorkflowTemplate metadata: name: [[ .name ]] spec: entrypoint: approve-flow arguments: parameters: - name: object value: {} templates: - name: approve-flow inputs: parameters: - name: object dag: tasks: [[- if .childNodesContentLines ]] [[- range .childNodesContentLines ]] [[ . ]] [[- end ]] [[- else ]] - name: node template: approval [[- end ]] - name: approval suspend: {} inputs: parameters: - name: approve default: '通过' enum: - '通过' - '拒绝' - '忽略' outputs: parameters: - name: approve valueFrom: supplied: {}` type Flow struct { name string childNodes []Node } func NewFlow(name string, childNodes ...Node) *Flow { return &Flow{ name: name, childNodes: childNodes, } } func (flow *Flow) GetDepends() string { return "" } func (flow *Flow) GetWhen() string { return "" } func (flow *Flow) AddNodes(childNodes ...Node) *Flow { if childNodes == nil || len(childNodes) == 0 { flow.childNodes = append(flow.childNodes, childNodes...) } return flow } func (flow *Flow) Render(_ Node) (string, error) { childNodesContents := make([]string, 0) for i, childNode := range flow.childNodes { var childNodeContent string if i == 0 { content, err := childNode.Render(nil) if err != nil { return "", err } childNodeContent = content } else { content, err := childNode.Render(flow.childNodes[i-1]) if err != nil { return "", err } childNodeContent = content } childNodesContents = append(childNodesContents, strings.Split(childNodeContent, "\n")...) } return render("flow", flowTemplate, map[string]any{ "name": flow.name, "childNodesContentLines": childNodesContents, }) }