-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (66 loc) · 1.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Package main this is the package content
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"sync"
log "github.com/sirupsen/logrus"
)
func main() {
opts = parseOpts()
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "15:04:05.9999"
customFormatter.FullTimestamp = true
log.SetFormatter(customFormatter)
if server {
var wg sync.WaitGroup
defer wg.Done()
log.Printf("Server requested. Starting server")
startServer()
wg.Wait()
os.Exit(0)
}
if _, err := os.Stat(namedPipe); errors.Is(err, os.ErrNotExist) {
log.Fatalf("Error finding named pipe: %v (Server not started?)", namedPipe)
}
f, err := os.OpenFile(namedPipe, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
if err != nil {
log.Fatalf("Error opening named pipe: %v", err)
}
// TODO: Take CLI args or stdin
makeRequest := func(args []string) {
b, e := json.Marshal(args)
if e != nil {
log.Errorf("Error marshalling arguments to JSON")
}
log.Printf("Send job: %s", string(b))
s, e := f.WriteString(string(b) + "\n")
if e != nil {
log.Warn(fmt.Sprintf("Server failed to acknowledge request: %v (%v)", e, s))
os.Exit(-1)
}
}
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
makeRequest(opts.Args())
} else {
r, _ := regexp.Compile(*inputPattern)
log.Printf("Stdin is a pipe! Matching stdin against: %v", r)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
in := scanner.Text()
if r.MatchString(in) {
log.Printf("%v matches %v", in, r)
makeRequest([]string{in})
}
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error reading in from stdin: %v", err)
}
}
os.Exit(0)
}