mirror of
https://framagit.org/ppom/reaction
synced 2026-03-14 12:45:47 +01:00
parent
13395a37f5
commit
d9303c6249
2 changed files with 54 additions and 22 deletions
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
21
app/main.go
21
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 <ip> 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])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue