Skip to content

Commit

Permalink
fix: better handle failures when parsing group glob/regex patterns
Browse files Browse the repository at this point in the history
The `Must` methods panic on failure -- this switches to using the normal
functions and logging any returned errors so we can skip broken patterns
and continue on
  • Loading branch information
tjhop committed Apr 16, 2024
1 parent 3faeba7 commit e125133
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions internal/inventory/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,18 @@ func (g Group) MatchGlobs(hostname string) int {
matched := 0

for _, globPattern := range g.globs {
glob := glob_util.MustCompile(globPattern)
glob, err := glob_util.Compile(globPattern)
if err != nil {
slog.LogAttrs(
context.Background(),
slog.LevelWarn,
"Failed to compile glob pattern for matching",
slog.String("err", err.Error()),
slog.String("glob", globPattern),
)
continue
}

if glob.Match(hostname) {
matched++
continue
Expand All @@ -223,7 +234,18 @@ func (g Group) MatchPatterns(hostname string) int {
matched := 0

for _, pattern := range g.patterns {
validPattern := regexp.MustCompile(pattern)
validPattern, err := regexp.Compile(pattern)
if err != nil {
slog.LogAttrs(
context.Background(),
slog.LevelWarn,
"Failed to compile regex pattern for matching",
slog.String("err", err.Error()),
slog.String("regex", pattern),
)
continue
}

if validPattern.MatchString(hostname) {
matched++
continue
Expand Down

0 comments on commit e125133

Please sign in to comment.