-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (69 loc) · 1.6 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
77
78
79
package main
import (
"fmt"
"os"
"os/signal"
"github.com/raitonoberu/riverpipe/client"
"github.com/raitonoberu/riverpipe/client/event"
"github.com/raitonoberu/riverpipe/output"
"github.com/raitonoberu/riverpipe/output/namedpipe"
"github.com/raitonoberu/riverpipe/output/stdout"
"github.com/urfave/cli/v2"
)
func main() {
var file string
var bufsize int
app := &cli.App{
Name: "riverpipe",
Usage: "print River events in JSON format",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "where to create named pipe (will be removed if exists!)",
Destination: &file,
},
&cli.IntFlag{
Name: "bufsize",
Aliases: []string{"b"},
Usage: "events buffer size",
Value: 16,
Destination: &bufsize,
},
},
Action: func(ctx *cli.Context) error {
var out output.Output
if file == "" {
out = stdout.New()
} else {
out = namedpipe.New(file)
}
client, err := client.New()
if err != nil {
return fmt.Errorf("couldn't create client: %w", err)
}
defer client.Release()
cleanup := make(chan os.Signal, 1)
signal.Notify(cleanup, os.Interrupt, os.Kill)
go func() {
<-cleanup
client.Release()
os.Exit(1)
}()
ch := make(chan event.Event, bufsize)
go func() {
err := client.Run(ch)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err.Error())
client.Release()
os.Exit(1)
}
}()
return out.Run(ch)
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, "error:", err.Error())
os.Exit(1)
}
}