reaction/app/client.go

141 lines
3.1 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"
"encoding/json"
2023-05-03 20:03:22 +02:00
"fmt"
"net"
2023-05-03 20:03:22 +02:00
"os"
"regexp"
2023-10-12 12:00:00 +02:00
"framagit.org/ppom/reaction/logger"
"sigs.k8s.io/yaml"
2023-05-03 20:03:22 +02:00
)
const (
Show = 0
2023-05-03 20:03:22 +02:00
Flush = 1
)
type Request struct {
Request int
Pattern string
}
type Response struct {
2023-10-04 12:00:00 +02:00
Err error
ClientStatus ClientStatus
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 {
2023-10-12 12:00:00 +02:00
logger.Fatalln("Error opening connection top daemon:", err)
2023-05-03 20:03:22 +02:00
}
defer conn.Close()
err = gob.NewEncoder(conn).Encode(data)
2023-05-03 20:03:22 +02:00
if err != nil {
2023-10-12 12:00:00 +02:00
logger.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 {
2023-10-12 12:00:00 +02:00
logger.Fatalln("Invalid answer from daemon:", err)
2023-05-03 20:03:22 +02:00
}
return response
2023-05-03 20:03:22 +02:00
}
type PatternStatus struct {
Matches int `json:"matches,omitempty"`
Actions map[string][]string `json:"actions,omitempty"`
}
type MapPatternStatus map[string]*PatternStatus
2023-10-04 12:00:00 +02:00
type MapPatternStatusFlush MapPatternStatus
type ClientStatus map[string]map[string]MapPatternStatus
2023-10-04 12:00:00 +02:00
type ClientStatusFlush ClientStatus
func (mps MapPatternStatusFlush) MarshalJSON() ([]byte, error) {
2023-10-04 12:00:00 +02:00
for _, v := range mps {
return json.Marshal(v)
2023-10-04 12:00:00 +02:00
}
return []byte(""), nil
2023-10-04 12:00:00 +02:00
}
func (csf ClientStatusFlush) MarshalJSON() ([]byte, error) {
2023-10-04 12:00:00 +02:00
ret := make(map[string]map[string]MapPatternStatusFlush)
for k, v := range csf {
ret[k] = make(map[string]MapPatternStatusFlush)
for kk, vv := range v {
ret[k][kk] = MapPatternStatusFlush(vv)
}
}
return json.Marshal(ret)
2023-10-04 12:00:00 +02:00
}
// end block
2023-05-03 20:03:22 +02:00
func usage(err string) {
fmt.Println("Usage: reactionc")
fmt.Println("Usage: reactionc flush <PATTERN>")
2023-10-12 12:00:00 +02:00
logger.Fatalln(err)
2023-05-03 20:03:22 +02:00
}
func ClientShow(streamfilter, format string) {
response := SendAndRetrieve(Request{Show, streamfilter})
if response.Err != nil {
2023-10-12 12:00:00 +02:00
logger.Fatalln("Received error from daemon:", response.Err)
os.Exit(1)
2023-05-03 20:03:22 +02:00
}
var text []byte
var err error
if format == "json" {
text, err = json.MarshalIndent(response.ClientStatus, "", " ")
} else {
text, err = yaml.Marshal(response.ClientStatus)
}
if err != nil {
2023-10-12 12:00:00 +02:00
logger.Fatalln("Failed to convert daemon binary response to text format:", err)
}
fmt.Println(string(text))
os.Exit(0)
}
func ClientFlush(pattern, streamfilter, format string) {
response := SendAndRetrieve(Request{Flush, pattern})
if response.Err != nil {
2023-10-12 12:00:00 +02:00
logger.Fatalln("Received error from daemon:", response.Err)
os.Exit(1)
}
var text []byte
var err error
if format == "json" {
text, err = json.MarshalIndent(ClientStatusFlush(response.ClientStatus), "", " ")
} else {
text, err = yaml.Marshal(ClientStatusFlush(response.ClientStatus))
}
2023-10-01 12:00:00 +02:00
if err != nil {
2023-10-12 12:00:00 +02:00
logger.Fatalln("Failed to convert daemon binary response to text format:", err)
2023-10-01 12:00:00 +02:00
}
fmt.Println(string(text))
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
}
}