From e1251332d9a6ffc64729e055bbe5d52b1debed12 Mon Sep 17 00:00:00 2001 From: TJ Hoplock Date: Tue, 16 Apr 2024 19:46:59 -0400 Subject: [PATCH] fix: better handle failures when parsing group glob/regex patterns 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 --- internal/inventory/group.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/internal/inventory/group.go b/internal/inventory/group.go index a6caa3b..2c38f37 100644 --- a/internal/inventory/group.go +++ b/internal/inventory/group.go @@ -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 @@ -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