reaction show: implement --pattern: pattern filtering with regex

fix #30
This commit is contained in:
ppom 2023-10-22 12:00:00 +02:00
parent 4053a83629
commit ac2621911f
2 changed files with 43 additions and 17 deletions

View File

@ -84,7 +84,7 @@ func usage(err string) {
logger.Fatalln(err) logger.Fatalln(err)
} }
func ClientShow(format, stream, filter string) { func ClientShow(format, stream, filter string, regex *regexp.Regexp) {
response := SendAndRetrieve(Request{Show, ""}) 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)
@ -114,6 +114,24 @@ func ClientShow(format, stream, filter string) {
os.Exit(1) os.Exit(1)
} }
} }
// Limit to pattern
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)
}
}
if len(response.ClientStatus[streamName][filterName]) == 0 {
delete(response.ClientStatus[streamName], filterName)
}
}
if len(response.ClientStatus[streamName]) == 0 {
delete(response.ClientStatus, streamName)
}
}
}
var text []byte var text []byte
var err error var err error
if format == "json" { if format == "json" {

View File

@ -49,8 +49,8 @@ func addLevelFlag(f *flag.FlagSet) *string {
return addStringFlag([]string{"l", "loglevel"}, "INFO", f) return addStringFlag([]string{"l", "loglevel"}, "INFO", f)
} }
func addStreamFilterFlag(f *flag.FlagSet) *string { func addPatternFlag(f *flag.FlagSet) *string {
return addStringFlag([]string{"sf", "streamfilter"}, "", f) return addStringFlag([]string{"p", "pattern"}, "", f)
} }
func subCommandParse(f *flag.FlagSet, maxRemainingArgs int) { func subCommandParse(f *flag.FlagSet, maxRemainingArgs int) {
@ -67,8 +67,6 @@ func subCommandParse(f *flag.FlagSet, maxRemainingArgs int) {
} }
} }
// FIXME add this options for show & flush
// -l/--limit .STREAM[.FILTER] # limit to stream and filter
func basicUsage() { func basicUsage() {
const ( const (
bold = "\033[1m" bold = "\033[1m"
@ -96,7 +94,8 @@ func basicUsage() {
# options: # options:
-s/--socket SOCKET # path to the client-daemon communication socket -s/--socket SOCKET # path to the client-daemon communication socket
-f/--format yaml|json # (default: yaml) -f/--format yaml|json # (default: yaml)
-sf/--streamfilter STREAM[.FILTER] # only show items related to this STREAM (or STREAM.FILTER) -l/--limit STREAM[.FILTER] # only show items related to this STREAM (or STREAM.FILTER)
-p/--pattern PATTERN # only show items matching the PATTERN regex
` + bold + `reaction flush` + reset + ` TARGET ` + bold + `reaction flush` + reset + ` TARGET
# run currently active matches and pending actions for the specified TARGET # run currently active matches and pending actions for the specified TARGET
@ -155,6 +154,7 @@ func Main() {
SocketPath = addSocketFlag(f) SocketPath = addSocketFlag(f)
queryFormat := addFormatFlag(f) queryFormat := addFormatFlag(f)
limit := addLimitFlag(f) limit := addLimitFlag(f)
pattern := addPatternFlag(f)
subCommandParse(f, 0) subCommandParse(f, 0)
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")
@ -171,7 +171,15 @@ func Main() {
logger.Fatalln("-l/--limit: only one . separator is supported") logger.Fatalln("-l/--limit: only one . separator is supported")
} }
} }
ClientShow(*queryFormat, stream, filter) var regex *regexp.Regexp
var err error
if *pattern != "" {
regex, err = regexp.Compile(*pattern)
if err != nil {
logger.Fatalln("-p/--pattern: ", err)
}
}
ClientShow(*queryFormat, stream, filter, regex)
case "flush": case "flush":
SocketPath = addSocketFlag(f) SocketPath = addSocketFlag(f)