2023-05-03 20:03:22 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2023-09-03 12:13:18 +02:00
|
|
|
"bufio"
|
2023-05-03 20:03:22 +02:00
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-05-04 01:01:22 +02:00
|
|
|
"net"
|
2023-05-03 20:03:22 +02:00
|
|
|
"os"
|
2023-09-03 12:13:18 +02:00
|
|
|
"regexp"
|
2023-05-03 20:03:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Query = 0
|
|
|
|
Flush = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
Request int
|
|
|
|
Pattern string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Err error
|
|
|
|
Actions ReadableMap
|
2023-09-03 12:13:18 +02:00
|
|
|
Number int
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
|
2023-05-04 01:01:22 +02:00
|
|
|
func SendAndRetrieve(data Request) Response {
|
2023-09-03 12:13:18 +02:00
|
|
|
conn, err := net.Dial("unix", *SocketPath)
|
2023-05-03 20:03:22 +02:00
|
|
|
if err != nil {
|
2023-05-04 01:01:22 +02:00
|
|
|
log.Fatalln("Error opening connection top daemon:", err)
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-05-04 01:01:22 +02:00
|
|
|
|
|
|
|
err = gob.NewEncoder(conn).Encode(data)
|
2023-05-03 20:03:22 +02:00
|
|
|
if err != nil {
|
2023-05-04 01:01:22 +02:00
|
|
|
log.Fatalln("Can't send message:", err)
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
|
2023-05-04 01:01:22 +02:00
|
|
|
var response Response
|
|
|
|
err = gob.NewDecoder(conn).Decode(&response)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Invalid answer from daemon:", err)
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-05-04 01:01:22 +02:00
|
|
|
return response
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func usage(err string) {
|
|
|
|
fmt.Println("Usage: reactionc")
|
|
|
|
fmt.Println("Usage: reactionc flush <PATTERN>")
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2023-09-03 12:13:18 +02:00
|
|
|
func ClientQuery(streamfilter string) {
|
|
|
|
response := SendAndRetrieve(Request{Query, streamfilter})
|
|
|
|
if response.Err != nil {
|
|
|
|
log.Fatalln("Received error from daemon:", response.Err)
|
|
|
|
os.Exit(1)
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-09-03 12:13:18 +02:00
|
|
|
fmt.Println(response.Actions.ToString())
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClientFlush(pattern, streamfilter string) {
|
|
|
|
response := SendAndRetrieve(Request{Flush, pattern})
|
|
|
|
if response.Err != nil {
|
|
|
|
log.Fatalln("Received error from daemon:", response.Err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("flushed pattern %v times\n", response.Number)
|
|
|
|
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 MatchStdin(reg *regexp.Regexp) {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
Match(reg, scanner.Text())
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
}
|