12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package approve_former
- import (
- "bytes"
- "github.com/pkg/errors"
- "text/template"
- )
- const workflowTemplateTpl = `
- [[- $workflowTemplateName := .name -]]
- [[- $rootChildren := .children -]]
- apiVersion: argoproj.io/v1alpha1
- kind: WorkflowTemplate
- metadata:
- name: [[ $workflowTemplateName ]]
- spec:
- entrypoint: approve-flow
- arguments:
- parameters:
- - name: object
- value: {}
- templates:
- - name: approve-flow
- inputs:
- parameters:
- - name: object
- dag:
- tasks:
- [[ template "template" $rootChildren ]]
- - name: approval
- suspend: {}
- inputs:
- parameters:
- - name: approve
- default: '通过'
- enum:
- - '通过'
- - '拒绝'
- - '忽略'
- outputs:
- parameters:
- - name: approve
- valueFrom:
- supplied: {}
- [[- define "template" -]]
- - name: node
- template: approval
- [[- end -]]
- `
- func ParseWorkflowTemplate(name string, templateParams map[string]any) (string, error) {
- tpl := template.New(name)
- tpl.Delims("[[", "]]")
- tpl, err := tpl.Parse(workflowTemplateTpl)
- if err != nil {
- return "", errors.New(err.Error())
- }
- buffer := &bytes.Buffer{}
- err = tpl.Execute(buffer, templateParams)
- if err != nil {
- return "", errors.New(err.Error())
- }
- return buffer.String(), nil
- }
|