11package analyzer
22
33import (
4- "bytes"
54 "fmt"
65 "path/filepath"
76 "regexp"
87 "strconv"
98 "strings"
10- "text/template"
119
1210 "github.com/pkg/errors"
11+ "github.com/replicatedhq/troubleshoot/internal/util"
1312 troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1413)
1514
@@ -210,7 +209,7 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
210209
211210 if isMatch {
212211 result .IsFail = true
213- tplMessage , err := templateRegExGroup (outcome .Fail .Message , foundMatches )
212+ tplMessage , err := util . RenderTemplate (outcome .Fail .Message , foundMatches )
214213 if err != nil {
215214 return result , errors .Wrap (err , "failed to template message in outcome.Fail block" )
216215 }
@@ -227,7 +226,7 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
227226
228227 if isMatch {
229228 result .IsWarn = true
230- tplMessage , err := templateRegExGroup (outcome .Warn .Message , foundMatches )
229+ tplMessage , err := util . RenderTemplate (outcome .Warn .Message , foundMatches )
231230 if err != nil {
232231 return result , errors .Wrap (err , "failed to template message in outcome.Warn block" )
233232 }
@@ -244,7 +243,7 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
244243
245244 if isMatch {
246245 result .IsPass = true
247- tplMessage , err := templateRegExGroup (outcome .Pass .Message , foundMatches )
246+ tplMessage , err := util . RenderTemplate (outcome .Pass .Message , foundMatches )
248247 if err != nil {
249248 return result , errors .Wrap (err , "failed to template message in outcome.Pass block" )
250249 }
@@ -259,20 +258,6 @@ func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troublesho
259258 return result , nil
260259}
261260
262- // templateRegExGroup takes a tpl and replaces the variables using matches.
263- func templateRegExGroup (tpl string , matches map [string ]string ) (string , error ) {
264- t , err := template .New ("" ).Parse (tpl )
265- if err != nil {
266- return "" , err
267- }
268- var msg bytes.Buffer
269- err = t .Execute (& msg , matches )
270- if err != nil {
271- return "" , err
272- }
273- return msg .String (), nil
274- }
275-
276261func compareRegex (conditional string , foundMatches map [string ]string ) (bool , error ) {
277262 if conditional == "" {
278263 return true , nil
@@ -287,6 +272,11 @@ func compareRegex(conditional string, foundMatches map[string]string) (bool, err
287272 operator := parts [1 ]
288273 lookForValue := parts [2 ]
289274
275+ // handle empty strings
276+ if lookForValue == "''" || lookForValue == `""` {
277+ lookForValue = ""
278+ }
279+
290280 foundValue , ok := foundMatches [lookForMatchName ]
291281 if ! ok {
292282 // not an error, just wasn't matched
0 commit comments