Display multi pattern separated by a space, specify them as multiple arguments to flush command

This commit is contained in:
yo 2024-04-08 19:54:07 +02:00
parent 463c5b709f
commit 61bf6f92b9
2 changed files with 29 additions and 13 deletions

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"os" "os"
"regexp" "regexp"
"strings"
"framagit.org/ppom/reaction/logger" "framagit.org/ppom/reaction/logger"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
@ -20,7 +21,7 @@ const (
type Request struct { type Request struct {
Request int Request int
Pattern []string Pattern string
} }
type Response struct { type Response struct {
@ -85,7 +86,7 @@ func usage(err string) {
} }
func ClientShow(format, stream, filter string, regex *regexp.Regexp) { func ClientShow(format, stream, filter string, regex *regexp.Regexp) {
response := SendAndRetrieve(Request{Show, []string{""}}) response := SendAndRetrieve(Request{Show, ""})
if response.Err != nil { if response.Err != nil {
logger.Fatalln("Received error from daemon:", response.Err) logger.Fatalln("Received error from daemon:", response.Err)
} }
@ -137,9 +138,15 @@ func ClientShow(format, stream, filter string, regex *regexp.Regexp) {
if regex != nil { if regex != nil {
for streamName := range response.ClientStatus { for streamName := range response.ClientStatus {
for filterName := range response.ClientStatus[streamName] { for filterName := range response.ClientStatus[streamName] {
for patternName := range response.ClientStatus[streamName][filterName] { for patterns := range response.ClientStatus[streamName][filterName] {
if !regex.MatchString(patternName) { pmatch := false
delete(response.ClientStatus[streamName][filterName], patternName) 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 { if len(response.ClientStatus[streamName][filterName]) == 0 {
@ -162,12 +169,22 @@ func ClientShow(format, stream, filter string, regex *regexp.Regexp) {
if err != nil { if err != nil {
logger.Fatalln("Failed to convert daemon binary response to text format:", err) 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)) fmt.Println(string(text))
os.Exit(0) os.Exit(0)
} }
func ClientFlush(pattern []string, streamfilter, format string) { func ClientFlush(patterns []string, streamfilter, format string) {
response := SendAndRetrieve(Request{Flush, pattern}) response := SendAndRetrieve(Request{Flush, strings.Join(patterns, "\x00")})
if response.Err != nil { if response.Err != nil {
logger.Fatalln("Received error from daemon:", response.Err) logger.Fatalln("Received error from daemon:", response.Err)
os.Exit(1) os.Exit(1)

View File

@ -60,7 +60,8 @@ func subCommandParse(f *flag.FlagSet, maxRemainingArgs int) {
basicUsage() basicUsage()
os.Exit(0) 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:]) fmt.Printf("ERROR unrecognized argument(s): %v\n", f.Args()[maxRemainingArgs:])
basicUsage() basicUsage()
os.Exit(1) os.Exit(1)
@ -102,9 +103,7 @@ func basicUsage() {
` + bold + `reaction flush` + reset + ` TARGET ` + bold + `reaction flush` + reset + ` TARGET
# remove currently active matches and run currently pending actions for the specified TARGET # remove currently active matches and run currently pending actions for the specified TARGET
# (then show flushed matches and actions) # (then show flushed matches and actions)
# e.g. reaction flush 192.168.1.1 # e.g. reaction flush 192.168.1.1 root
# Concatenate patterns with " / " if several patterns in TARGET
# e.g. reaction flush "192.168.1.1 / root"
# options: # options:
-s/--socket SOCKET # path to the client-daemon communication socket -s/--socket SOCKET # path to the client-daemon communication socket
@ -196,7 +195,7 @@ func Main(version, commit string) {
SocketPath = addSocketFlag(f) SocketPath = addSocketFlag(f)
queryFormat := addFormatFlag(f) queryFormat := addFormatFlag(f)
limit := addLimitFlag(f) limit := addLimitFlag(f)
subCommandParse(f, 1) subCommandParse(f, -1)
if *queryFormat != "yaml" && *queryFormat != "json" { if *queryFormat != "yaml" && *queryFormat != "json" {
logger.Fatalln("only yaml and json formats are supported") logger.Fatalln("only yaml and json formats are supported")
f.PrintDefaults() f.PrintDefaults()
@ -211,7 +210,7 @@ func Main(version, commit string) {
logger.Fatalln("for now, -l/--limit is not supported") logger.Fatalln("for now, -l/--limit is not supported")
os.Exit(1) os.Exit(1)
} }
ClientFlush(strings.Split(f.Arg(0), " / "), *limit, *queryFormat) ClientFlush(f.Args(), *limit, *queryFormat)
case "test-regex": case "test-regex":
// socket not needed, no interaction with the daemon // socket not needed, no interaction with the daemon