-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.go
More file actions
96 lines (80 loc) · 2.87 KB
/
detect.go
File metadata and controls
96 lines (80 loc) · 2.87 KB
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
89
90
91
92
93
94
95
96
package capability
import (
"sort"
"sync"
"github.com/GoCodeAlone/workflow/config"
)
var (
mappingMu sync.RWMutex
// moduleTypeToCapabilities maps module type strings to their capability categories.
// Populated by plugins during registration via RegisterModuleTypeMapping.
moduleTypeToCapabilities = make(map[string][]string)
// triggerTypeToCapabilities maps trigger types to capabilities.
triggerTypeToCapabilities = make(map[string][]string)
// workflowTypeToCapabilities maps workflow types to capabilities.
workflowTypeToCapabilities = make(map[string][]string)
)
// RegisterModuleTypeMapping records that a module type requires certain capabilities.
func RegisterModuleTypeMapping(moduleType string, capabilities ...string) {
mappingMu.Lock()
defer mappingMu.Unlock()
moduleTypeToCapabilities[moduleType] = append(moduleTypeToCapabilities[moduleType], capabilities...)
}
// RegisterTriggerTypeMapping records trigger type to capability mapping.
func RegisterTriggerTypeMapping(triggerType string, capabilities ...string) {
mappingMu.Lock()
defer mappingMu.Unlock()
triggerTypeToCapabilities[triggerType] = append(triggerTypeToCapabilities[triggerType], capabilities...)
}
// RegisterWorkflowTypeMapping records workflow type to capability mapping.
func RegisterWorkflowTypeMapping(workflowType string, capabilities ...string) {
mappingMu.Lock()
defer mappingMu.Unlock()
workflowTypeToCapabilities[workflowType] = append(workflowTypeToCapabilities[workflowType], capabilities...)
}
// ResetMappings clears all registered type-to-capability mappings. Intended for testing.
func ResetMappings() {
mappingMu.Lock()
defer mappingMu.Unlock()
moduleTypeToCapabilities = make(map[string][]string)
triggerTypeToCapabilities = make(map[string][]string)
workflowTypeToCapabilities = make(map[string][]string)
}
// DetectRequired scans a WorkflowConfig and returns the set of capabilities needed.
// It inspects module types, trigger types, and workflow types and returns a
// deduplicated, sorted list of required capabilities.
func DetectRequired(cfg *config.WorkflowConfig) []string {
seen := make(map[string]bool)
mappingMu.RLock()
defer mappingMu.RUnlock()
// Scan modules for module types.
for _, mod := range cfg.Modules {
if caps, ok := moduleTypeToCapabilities[mod.Type]; ok {
for _, c := range caps {
seen[c] = true
}
}
}
// Scan triggers keys for trigger types.
for triggerType := range cfg.Triggers {
if caps, ok := triggerTypeToCapabilities[triggerType]; ok {
for _, c := range caps {
seen[c] = true
}
}
}
// Scan workflows keys for workflow types.
for workflowType := range cfg.Workflows {
if caps, ok := workflowTypeToCapabilities[workflowType]; ok {
for _, c := range caps {
seen[c] = true
}
}
}
result := make([]string, 0, len(seen))
for c := range seen {
result = append(result, c)
}
sort.Strings(result)
return result
}