reaction/app/client.go

88 lines
1.6 KiB
Go
Raw Normal View History

2023-05-03 20:03:22 +02:00
package app
import (
"bufio"
2023-05-03 20:03:22 +02:00
"encoding/gob"
"fmt"
"log"
"net"
2023-05-03 20:03:22 +02:00
"os"
"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
Number int
2023-05-03 20:03:22 +02:00
}
func SendAndRetrieve(data Request) Response {
conn, err := net.Dial("unix", *SocketPath)
2023-05-03 20:03:22 +02:00
if err != nil {
log.Fatalln("Error opening connection top daemon:", err)
2023-05-03 20:03:22 +02:00
}
err = gob.NewEncoder(conn).Encode(data)
2023-05-03 20:03:22 +02:00
if err != nil {
log.Fatalln("Can't send message:", err)
2023-05-03 20:03: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
}
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)
}
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
}
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
}
}