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"
"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)

View File

@ -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