|
|
@@ -1,21 +1,25 @@
|
|
|
package approve_former
|
|
|
|
|
|
+import "strings"
|
|
|
+
|
|
|
const flowTemplate = `apiVersion: argoproj.io/v1alpha1
|
|
|
kind: WorkflowTemplate
|
|
|
metadata:
|
|
|
- name: {{ .name }}
|
|
|
+ name: [[ .name ]]
|
|
|
spec:
|
|
|
entrypoint: approve-flow
|
|
|
templates:
|
|
|
- name: approve-flow
|
|
|
dag:
|
|
|
tasks:
|
|
|
- {{- if .nodeContent }}
|
|
|
- {{ .nodeContent }}
|
|
|
- {{- else }}
|
|
|
+ [[- if .childNodesContentLines ]]
|
|
|
+ [[- range .childNodesContentLines ]]
|
|
|
+ [[ . ]]
|
|
|
+ [[- end ]]
|
|
|
+ [[- else ]]
|
|
|
- name: node
|
|
|
template: approval
|
|
|
- {{- end }}
|
|
|
+ [[- end ]]
|
|
|
|
|
|
- name: approval
|
|
|
suspend: {}
|
|
|
@@ -34,30 +38,59 @@ spec:
|
|
|
`
|
|
|
|
|
|
type Flow struct {
|
|
|
- Name string
|
|
|
- Nodes []Node
|
|
|
+ Name string
|
|
|
+ ChildNodes []Node
|
|
|
}
|
|
|
|
|
|
-func NewFlow(name string, nodes ...Node) *Flow {
|
|
|
+func NewFlow(name string, childNodes ...Node) *Flow {
|
|
|
return &Flow{
|
|
|
- Name: name,
|
|
|
- Nodes: nodes,
|
|
|
+ Name: name,
|
|
|
+ ChildNodes: childNodes,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (flow *Flow) Render() (string, error) {
|
|
|
- nodeContents := make([]string, 0)
|
|
|
- for _, node := range flow.Nodes {
|
|
|
- nodeContent, err := node.Render()
|
|
|
- if err != nil {
|
|
|
- return "", err
|
|
|
+func (flow *Flow) GetName() string {
|
|
|
+ return flow.Name
|
|
|
+}
|
|
|
+
|
|
|
+func (flow *Flow) GetChildNodes() []Node {
|
|
|
+ return flow.ChildNodes
|
|
|
+}
|
|
|
+
|
|
|
+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()
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ childNodeContent = content
|
|
|
+ } else {
|
|
|
+ upperNodes := make([]Node, 0)
|
|
|
+ upperChildNodes := flow.ChildNodes[i-1].GetChildNodes()
|
|
|
+ if len(upperChildNodes) == 0 {
|
|
|
+ upperNodes = append(upperNodes, flow.ChildNodes[i-1])
|
|
|
+ } else {
|
|
|
+ upperNodes = append(upperNodes, upperChildNodes...)
|
|
|
+ }
|
|
|
+
|
|
|
+ content, err := childNode.Render(upperNodes...)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ childNodeContent = content
|
|
|
}
|
|
|
|
|
|
- nodeContents = append(nodeContents, nodeContent)
|
|
|
+ childNodesContents = append(childNodesContents, strings.Split(childNodeContent, "\n")...)
|
|
|
}
|
|
|
|
|
|
return render("flow", flowTemplate, map[string]any{
|
|
|
- "name": flow.Name,
|
|
|
- "nodes": nodeContents,
|
|
|
+ "name": flow.Name,
|
|
|
+ "childNodesContentLines": childNodesContents,
|
|
|
})
|
|
|
}
|