-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree.go
88 lines (79 loc) · 2.01 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package devtools
import (
"fmt"
localworkflows "github.com/khulnasoft-lab/go-application-framework/pkg/local_workflows"
"github.com/khulnasoft-lab/go-application-framework/pkg/workflow"
"github.com/spf13/cobra"
)
type cmdTree struct {
name string
children map[string]*cmdTree
workflowID workflow.Identifier
}
func newNode(name string) *cmdTree {
return &cmdTree{
name: name,
children: map[string]*cmdTree{},
}
}
func (n *cmdTree) add(parts []string, workflowID workflow.Identifier) {
if len(parts) < 1 {
n.workflowID = workflowID
return
}
head := parts[0]
child, ok := n.children[head]
if !ok {
child = newNode(head)
n.children[head] = child
}
child.add(parts[1:], workflowID)
}
func (n *cmdTree) cmd(engine workflow.Engine) *cobra.Command {
var cmd *cobra.Command
if n.workflowID == nil {
cmd = newEmptyCommand(n.name)
} else {
cmd = newWorkflowCommand(n.name, engine, n.workflowID)
}
for _, child := range n.children {
cmd.AddCommand(child.cmd(engine))
}
return cmd
}
func runEmpty(cmd *cobra.Command, args []string) error {
return fmt.Errorf("no workflow for command")
}
func newEmptyCommand(name string) *cobra.Command {
return &cobra.Command{
Use: name,
Hidden: true,
RunE: runEmpty,
DisableFlagParsing: true,
}
}
func newWorkflowCommand(name string, engine workflow.Engine, id workflow.Identifier) *cobra.Command {
w, _ := engine.GetWorkflow(id)
cmd := &cobra.Command{
Use: name,
Hidden: !w.IsVisible(),
RunE: func(cmd *cobra.Command, args []string) error {
config := engine.GetConfiguration()
if err := config.AddFlagSet(cmd.Flags()); err != nil {
return err
}
data, err := engine.Invoke(id)
if err != nil {
return err
}
_, err = engine.InvokeWithInput(localworkflows.WORKFLOWID_OUTPUT_WORKFLOW, data)
return err
},
}
options := w.GetConfigurationOptions()
flagset := workflow.FlagsetFromConfigurationOptions(options)
if flagset != nil {
cmd.Flags().AddFlagSet(flagset)
}
return cmd
}