123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package approve_former
- import (
- "k8s.io/apimachinery/pkg/util/uuid"
- "strings"
- )
- const flowTemplate = `apiVersion: argoproj.io/v1alpha1
- kind: WorkflowTemplate
- metadata:
- name: [[ .name ]]
- spec:
- entrypoint: front-json-flow
- arguments:
- parameters:
- - name: flow-name
- value: [[ .flowName ]]
- - name: flow-type
- value: [[ .flowType ]]
- - name: flow-business-object
- value: |
- [[ .flowBusinessObject ]]
- templates:
- - name: front-json-flow
- steps:
- [[- if .childNodesContentLines ]]
- [[- range .childNodesContentLines ]]
- [[ . ]]
- [[- end ]]
- [[- else ]]
- - name: parse-flow
- template: approval
- [[- end ]]
- - name: approval
- suspend: {}
- inputs:
- parameters:
- - name: front-json
- default: '通过'
- enum:
- - '通过'
- - '拒绝'
- - '忽略'
- outputs:
- parameters:
- - name: front-json
- valueFrom:
- supplied: {}`
- type FlowBaseInfo struct {
- Name string
- Type string
- BusinessObject string
- }
- type Flow struct {
- BaseInfo *FlowBaseInfo
- childNodes []Noder
- }
- func NewFlow(baseInfo *FlowBaseInfo, childNodes ...Noder) *Flow {
- return &Flow{
- BaseInfo: baseInfo,
- childNodes: childNodes,
- }
- }
- func (flow *Flow) GetDepends() string {
- return ""
- }
- func (flow *Flow) GetWhen() string {
- return ""
- }
- func (flow *Flow) AddNodes(childNodes ...Noder) *Flow {
- if childNodes == nil || len(childNodes) == 0 {
- return flow
- }
- flow.childNodes = append(flow.childNodes, childNodes...)
- return flow
- }
- func (flow *Flow) Render(_ Noder) (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-" + uuid.NewUUID()[:8],
- "flowName": flow.BaseInfo.Name,
- "flowType": flow.BaseInfo.Type,
- "flowBusinessObject": flow.BaseInfo.BusinessObject,
- "childNodesContentLines": childNodesContents,
- })
- }
|