-
Notifications
You must be signed in to change notification settings - Fork 13
/
match.go
176 lines (159 loc) · 4.8 KB
/
match.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
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
package aini
import (
"fmt"
"path"
"strings"
"golang.org/x/exp/maps"
)
// MatchHostsByPatterns looks for all hosts that match the Ansible host patterns as described in https://docs.ansible.com/ansible/latest/inventory_guide/intro_patterns.html
//
// e.g. "webservers:gateways:myhost.domain:!atlanta"
func (inventory *InventoryData) MatchHostsByPatterns(patterns string) (map[string]*Host, error) {
patternList := strings.Split(patterns, ":")
matchedHosts := make(map[string]*Host)
for _, host := range inventory.Hosts {
matched, err := host.MatchPatterns(patternList)
if err != nil {
return matchedHosts, err
}
if matched {
matchedHosts[host.Name] = host
}
}
return matchedHosts, nil
}
// MatchPatterns checks whether the given host matches the list of Ansible host patterns.
//
// e.g. [webservers, gateways, myhost.domain, !atlanta]
func (host *Host) MatchPatterns(patterns []string) (bool, error) {
allNames := make([]string, 0, 1+len(host.Groups))
allNames = append(allNames, host.Name)
allNames = append(allNames, maps.Keys(host.Groups)...)
return MatchNamesByPatterns(allNames, patterns)
}
// MatchNamesByPatterns checks whether the give hostname and group names match list of Ansible host patterns.
//
// e.g. [webservers, gateways, myhost.domain, !atlanta]
func MatchNamesByPatterns(allNames []string, patterns []string) (bool, error) {
numPositiveMatch := 0
for index, pattern := range patterns {
switch {
case pattern == "":
if index == 0 {
return false, nil
}
continue
case pattern == "all" || pattern == "*":
numPositiveMatch++
case pattern[0] == '!':
if index == 0 {
return false, fmt.Errorf("exclusion pattern \"%s\" cannot be the first pattern", pattern)
}
any, err := matchAnyName(pattern[1:], allNames)
if err != nil {
return false, err
}
if any {
return false, err
}
case pattern[0] == '&':
if index == 0 {
return false, fmt.Errorf("intersection pattern \"%s\" cannot be the first pattern", pattern)
}
any, err := matchAnyName(pattern[1:], allNames)
if err != nil {
return false, err
}
if !any {
return false, err
}
default:
any, err := matchAnyName(pattern, allNames)
if err != nil {
return false, err
}
if any {
numPositiveMatch++
}
}
}
return numPositiveMatch > 0, nil
}
func matchAnyName(pattern string, allNames []string) (bool, error) {
for _, name := range allNames {
matched, err := path.Match(pattern, name)
if err != nil {
return false, err
}
if matched {
return true, nil
}
}
return false, nil
}
// MatchHosts looks for hosts whose hostnames match the pattern. Group memberships are not considered.
func (inventory *InventoryData) MatchHosts(pattern string) (map[string]*Host, error) {
return MatchHosts(inventory.Hosts, pattern)
}
// MatchHosts looks for hosts whose hostnames match the pattern. Group memberships are not considered.
func (group *Group) MatchHosts(pattern string) (map[string]*Host, error) {
return MatchHosts(group.Hosts, pattern)
}
// MatchHosts looks for hosts whose hostnames match the pattern. Group memberships are not considered.
func MatchHosts(hosts map[string]*Host, pattern string) (map[string]*Host, error) {
matchedHosts := make(map[string]*Host)
for _, host := range hosts {
m, err := path.Match(pattern, host.Name)
if err != nil {
return nil, err
}
if m {
matchedHosts[host.Name] = host
}
}
return matchedHosts, nil
}
// MatchGroups looks for groups that match the pattern
func (inventory *InventoryData) MatchGroups(pattern string) (map[string]*Group, error) {
return MatchGroups(inventory.Groups, pattern)
}
// MatchGroups looks for groups that match the pattern
func (host *Host) MatchGroups(pattern string) (map[string]*Group, error) {
return MatchGroups(host.Groups, pattern)
}
// MatchGroups looks for groups that match the pattern
func MatchGroups(groups map[string]*Group, pattern string) (map[string]*Group, error) {
matchedGroups := make(map[string]*Group)
for _, group := range groups {
m, err := path.Match(pattern, group.Name)
if err != nil {
return nil, err
}
if m {
matchedGroups[group.Name] = group
}
}
return matchedGroups, nil
}
// MatchVars looks for vars that match the pattern
func (group *Group) MatchVars(pattern string) (map[string]string, error) {
return MatchVars(group.Vars, pattern)
}
// MatchVars looks for vars that match the pattern
func (host *Host) MatchVars(pattern string) (map[string]string, error) {
return MatchVars(host.Vars, pattern)
}
// MatchVars looks for vars that match the pattern
func MatchVars(vars map[string]string, pattern string) (map[string]string, error) {
matchedVars := make(map[string]string)
for k, v := range vars {
m, err := path.Match(pattern, v)
if err != nil {
return nil, err
}
if m {
matchedVars[k] = v
}
}
return matchedVars, nil
}