From d9303c62494895184688757044b2a32436aefefe Mon Sep 17 00:00:00 2001 From: ppom Date: Fri, 5 Apr 2024 12:00:00 +0200 Subject: [PATCH] test-regex: read conf file & use patterns fix #72 --- app/client.go | 57 +++++++++++++++++++++++++++++++++++++++++---------- app/main.go | 21 ++++++++----------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/app/client.go b/app/client.go index 8be7bdb..5d0d7a5 100644 --- a/app/client.go +++ b/app/client.go @@ -8,6 +8,7 @@ import ( "net" "os" "regexp" + "strings" "framagit.org/ppom/reaction/logger" "sigs.k8s.io/yaml" @@ -186,17 +187,51 @@ func ClientFlush(pattern, streamfilter, format string) { os.Exit(0) } -func Match(reg *regexp.Regexp, line string) { - if reg.MatchString(line) { - fmt.Printf("\033[32mmatching\033[0m: %v\n", line) - } else { - fmt.Printf("\033[31mno match\033[0m: %v\n", line) - } -} +func Match(confFilename, regex, line string) { + conf := parseConf(confFilename) -func MatchStdin(reg *regexp.Regexp) { - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { - Match(reg, scanner.Text()) + // Code close to app/startup.go + var usedPattern *Pattern + for patternName, pattern := range conf.Patterns { + if strings.Contains(regex, pattern.nameWithBraces) { + if usedPattern != nil { + logger.Fatalf("Bad regex: Can't mix different patterns (%s, %s) in same line", usedPattern.name, patternName) + } + usedPattern = pattern + regex = strings.Replace(regex, pattern.nameWithBraces, pattern.Regex, 1) + } + } + reg, err := regexp.Compile(regex) + if err != nil { + logger.Fatalln("ERROR the specified regex is invalid: %v", err) + os.Exit(1) + } + + match := func(line string) { + if matches := reg.FindStringSubmatch(line); matches != nil { + if usedPattern != nil { + match := matches[reg.SubexpIndex(usedPattern.name)] + + if usedPattern.notAnIgnore(&match) { + fmt.Printf("\033[32mmatching\033[0m [%v]: %v\n", match, line) + } else { + fmt.Printf("\033[33mignore matching\033[0m [%v]: %v\n", match, line) + } + } else { + fmt.Printf("\033[32mmatching\033[0m [%v]:\n", line) + } + } else { + fmt.Printf("\033[31mno match\033[0m: %v\n", line) + } + } + + if line != "" { + match(line) + } else { + logger.Println(logger.INFO, "no second argument: reading from stdin") + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + match(scanner.Text()) + } } } diff --git a/app/main.go b/app/main.go index 6c262f9..e1a8f5e 100644 --- a/app/main.go +++ b/app/main.go @@ -111,6 +111,10 @@ func basicUsage() { ` + bold + `reaction test-regex` + reset + ` REGEX LINE # test REGEX against LINE cat FILE | ` + bold + `reaction test-regex` + reset + ` REGEX # test REGEX against each line of FILE + # options: + -c/--config CONFIG_FILE # configuration file in json, jsonnet or yaml format + # optional: permits to use patterns like in regex + ` + bold + `reaction version` + reset + ` # print version information @@ -213,24 +217,17 @@ func Main(version, commit string) { case "test-regex": // socket not needed, no interaction with the daemon + confFilename := addConfFlag(f) subCommandParse(f, 2) + if *confFilename == "" { + logger.Println(logger.WARN, "no configuration file provided. Can't make use of registered patterns.") + } if f.Arg(0) == "" { logger.Fatalln("subcommand test-regex takes at least one REGEX argument") basicUsage() os.Exit(1) } - regex, err := regexp.Compile(f.Arg(0)) - if err != nil { - logger.Fatalln("ERROR the specified regex is invalid: %v", err) - os.Exit(1) - } - if f.Arg(1) == "" { - logger.Println(logger.INFO, "no second argument: reading from stdin") - - MatchStdin(regex) - } else { - Match(regex, f.Arg(1)) - } + Match(*confFilename, f.Arg(0), f.Arg(1)) default: logger.Fatalf("subcommand %v not recognized. Try `reaction help`", os.Args[1])