-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
209 lines (188 loc) · 4.52 KB
/
Copy pathconfig.go
File metadata and controls
209 lines (188 loc) · 4.52 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"gopkg.in/yaml.v3"
)
const defaultPriority = 100
// Entry is one script item: either a plain string or {script, priority}.
type Entry struct {
Script string
Priority int
}
func (e *Entry) UnmarshalYAML(value *yaml.Node) error {
switch value.Tag {
case "!!str":
e.Script = value.Value
e.Priority = defaultPriority
return nil
case "!!map":
type raw struct {
Script string `yaml:"script"`
Priority *int `yaml:"priority"`
}
var r raw
if err := value.Decode(&r); err != nil {
return err
}
e.Script = r.Script
if r.Priority != nil {
e.Priority = *r.Priority
} else {
e.Priority = defaultPriority
}
return nil
default:
return fmt.Errorf("unexpected YAML node tag %q for entry", value.Tag)
}
}
// CommandConfig: command → container → []Entry
type CommandConfig map[string]map[string][]Entry
// Module is a discovered module with its parsed config.
type Module struct {
Name string
Path string
Config CommandConfig
}
// effectivePriority is the lowest priority value across all entries for a command.
func (m *Module) effectivePriority(command string) int {
containers, ok := m.Config[command]
if !ok {
return defaultPriority
}
min := defaultPriority
for _, entries := range containers {
for _, e := range entries {
if e.Priority < min {
min = e.Priority
}
}
}
return min
}
// firstContainer returns the first container key found across all commands.
func (m *Module) firstContainer() string {
for _, containers := range m.Config {
for k := range containers {
return k
}
}
return ""
}
// discoverModules finds all .devops/commands.yaml files up to 3 levels deep.
func discoverModules() ([]*Module, error) {
var modules []*Module
if _, err := os.Stat(".devops/commands.yaml"); err == nil {
m, err := loadModule("root", ".devops/commands.yaml")
if err != nil {
return nil, fmt.Errorf("root: %w", err)
}
modules = append(modules, m)
}
var candidates []string
for _, pattern := range []string{
"*/.devops/commands.yaml",
"*/*/.devops/commands.yaml",
"*/*/*/.devops/commands.yaml",
} {
matches, err := filepath.Glob(pattern)
if err != nil {
continue
}
candidates = append(candidates, matches...)
}
for _, path := range candidates {
name := moduleNameFromPath(path)
m, err := loadModule(name, path)
if err != nil {
return nil, fmt.Errorf("%s: %w", name, err)
}
modules = append(modules, m)
}
return modules, nil
}
func moduleNameFromPath(path string) string {
path = filepath.ToSlash(path)
parts := strings.Split(path, "/")
for _, p := range parts {
if p != "." && p != ".devops" && p != "commands.yaml" && p != "" {
return p
}
}
return path
}
func loadModule(name, path string) (*Module, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cfg CommandConfig
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
return &Module{Name: name, Path: path, Config: cfg}, nil
}
// findModulesForCommand returns modules that define the given command, sorted
// by their effective priority (ascending).
func findModulesForCommand(modules []*Module, command string) []*Module {
var found []*Module
for _, m := range modules {
if _, ok := m.Config[command]; ok {
found = append(found, m)
}
}
sort.SliceStable(found, func(i, j int) bool {
return found[i].effectivePriority(command) < found[j].effectivePriority(command)
})
return found
}
// groupByPriority buckets the (already priority-sorted) modules into groups
// that share the same effective priority for the given command.
func groupByPriority(modules []*Module, command string) [][]*Module {
if len(modules) == 0 {
return nil
}
var groups [][]*Module
var cur []*Module
curPrio := -1
for _, m := range modules {
p := m.effectivePriority(command)
if len(cur) > 0 && p != curPrio {
groups = append(groups, cur)
cur = nil
}
curPrio = p
cur = append(cur, m)
}
if len(cur) > 0 {
groups = append(groups, cur)
}
return groups
}
func findModule(modules []*Module, name string) *Module {
for _, m := range modules {
if m.Name == name {
return m
}
}
return nil
}
// allCommands returns a sorted, deduplicated list of all commands defined
// across all modules.
func allCommands(modules []*Module) []string {
seen := map[string]bool{}
for _, m := range modules {
for cmd := range m.Config {
seen[cmd] = true
}
}
cmds := make([]string, 0, len(seen))
for c := range seen {
cmds = append(cmds, c)
}
sort.Strings(cmds)
return cmds
}