From 61bf6f92b9adbc6813dd5dc097e621aaf6119f57 Mon Sep 17 00:00:00 2001 From: yo Date: Mon, 8 Apr 2024 19:54:07 +0200 Subject: [PATCH] Display multi pattern separated by a space, specify them as multiple arguments to flush command --- app/client.go | 31 ++++++++++++++++++++++++------- app/main.go | 11 +++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/client.go b/app/client.go index 01ece26..e35cb22 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" @@ -20,7 +21,7 @@ const ( type Request struct { Request int - Pattern []string + Pattern string } type Response struct { @@ -85,7 +86,7 @@ func usage(err string) { } func ClientShow(format, stream, filter string, regex *regexp.Regexp) { - response := SendAndRetrieve(Request{Show, []string{""}}) + response := SendAndRetrieve(Request{Show, ""}) if response.Err != nil { logger.Fatalln("Received error from daemon:", response.Err) } @@ -137,9 +138,15 @@ func ClientShow(format, stream, filter string, regex *regexp.Regexp) { if regex != nil { for streamName := range response.ClientStatus { for filterName := range response.ClientStatus[streamName] { - for patternName := range response.ClientStatus[streamName][filterName] { - if !regex.MatchString(patternName) { - delete(response.ClientStatus[streamName][filterName], patternName) + for patterns := range response.ClientStatus[streamName][filterName] { + pmatch := false + for _, p := range strings.Split(patterns, "\x00") { + if regex.MatchString(p) { + pmatch = true + } + } + if !pmatch { + delete(response.ClientStatus[streamName][filterName], patterns) } } if len(response.ClientStatus[streamName][filterName]) == 0 { @@ -162,12 +169,22 @@ func ClientShow(format, stream, filter string, regex *regexp.Regexp) { if err != nil { logger.Fatalln("Failed to convert daemon binary response to text format:", err) } + + // Replace \0 joined string with space joined string ("1.2.3.4\0root" -> "1.2.3.4 root") + for streamName := range response.ClientStatus { + for filterName := range response.ClientStatus[streamName] { + for patterns := range response.ClientStatus[streamName][filterName] { + text = []byte(strings.ReplaceAll(string(text), strings.Join(strings.Split(patterns, "\x00"), "\\0"), strings.Join(strings.Split(patterns, "\x00"), " "))) + } + } + } fmt.Println(string(text)) + os.Exit(0) } -func ClientFlush(pattern []string, streamfilter, format string) { - response := SendAndRetrieve(Request{Flush, pattern}) +func ClientFlush(patterns []string, streamfilter, format string) { + response := SendAndRetrieve(Request{Flush, strings.Join(patterns, "\x00")}) if response.Err != nil { logger.Fatalln("Received error from daemon:", response.Err) os.Exit(1) diff --git a/app/main.go b/app/main.go index ae9fc4b..801856d 100644 --- a/app/main.go +++ b/app/main.go @@ -60,7 +60,8 @@ func subCommandParse(f *flag.FlagSet, maxRemainingArgs int) { basicUsage() os.Exit(0) } - if len(f.Args()) > maxRemainingArgs { + // -1 = no limit to remaining args + if maxRemainingArgs > -1 && len(f.Args()) > maxRemainingArgs { fmt.Printf("ERROR unrecognized argument(s): %v\n", f.Args()[maxRemainingArgs:]) basicUsage() os.Exit(1) @@ -102,9 +103,7 @@ func basicUsage() { ` + bold + `reaction flush` + reset + ` TARGET # remove currently active matches and run currently pending actions for the specified TARGET # (then show flushed matches and actions) - # e.g. reaction flush 192.168.1.1 - # Concatenate patterns with " / " if several patterns in TARGET - # e.g. reaction flush "192.168.1.1 / root" + # e.g. reaction flush 192.168.1.1 root # options: -s/--socket SOCKET # path to the client-daemon communication socket @@ -196,7 +195,7 @@ func Main(version, commit string) { SocketPath = addSocketFlag(f) queryFormat := addFormatFlag(f) limit := addLimitFlag(f) - subCommandParse(f, 1) + subCommandParse(f, -1) if *queryFormat != "yaml" && *queryFormat != "json" { logger.Fatalln("only yaml and json formats are supported") f.PrintDefaults() @@ -211,7 +210,7 @@ func Main(version, commit string) { logger.Fatalln("for now, -l/--limit is not supported") os.Exit(1) } - ClientFlush(strings.Split(f.Arg(0), " / "), *limit, *queryFormat) + ClientFlush(f.Args(), *limit, *queryFormat) case "test-regex": // socket not needed, no interaction with the daemon