-
Notifications
You must be signed in to change notification settings - Fork 0
/
lgroute.go
154 lines (125 loc) · 2.52 KB
/
lgroute.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"os"
"bufio"
"strings"
"errors"
"log"
"bytes"
"io"
"fmt"
"flag"
)
var stderr *log.Logger
func init() {
stderr = log.New(os.Stderr, "", 0)
}
type Router struct {
Key []byte
File *os.File
flag int
}
func (r *Router) Write(data []byte) (n int, err error) {
data = append(data, []byte("\r\n")...)
return r.File.Write(data)
}
func (r *Router) Contains(s []byte) bool {
return bytes.Contains(s, r.Key)
}
func (r *Router) String() string {
var s string
if r.flag == os.O_APPEND {
s = ">>"
} else {
s = ">"
}
return "router: " + string(r.Key) + s + r.File.Name()
}
func NewRouter(arg string) (router *Router, err error) {
var params []string
var flag int
if strings.Contains(arg, ">>") {
params = strings.Split(arg, ">>")
flag = os.O_APPEND
} else if strings.Contains(arg, ">") {
params = strings.Split(arg, ">")
flag = os.O_TRUNC
} else {
return nil, errors.New(`invalid rediraction symbol in argument "` + arg + `"`)
}
if len(params) < 2 {
return nil, errors.New(`invalid argument "` + arg + `"`)
}
file, err := os.OpenFile(params[1], os.O_WRONLY | os.O_CREATE | flag, 0644)
if err != nil {
return nil, err
}
return &Router{[]byte(params[0]), file, flag}, nil
}
type Routers struct {
Routers []*Router
InParallel bool
}
func (r *Routers) handle(line []byte) {
contained := false
for _, router := range r.Routers {
if router.Contains(line) {
if _, err := router.Write(line); err != nil {
stderr.Println(err.Error())
} else {
contained = true
}
}
}
if contained != true {
fmt.Println(string(line))
}
}
func (r *Routers) Handle(line []byte) {
if r.InParallel {
go r.handle(line)
} else {
r.handle(line)
}
}
func NewRouters(args []string) *Routers {
var routers []*Router
for _, arg := range args {
router, err := NewRouter(arg)
if err != nil {
stderr.Println(err.Error())
continue
}
routers = append(routers, router)
}
return &Routers{
Routers: routers,
}
}
func CanonicalArgs(args []string) []string {
var result []string
for _, arg := range args {
if string(arg[0]) != "-" {
result = append(result, arg)
}
}
return result
}
func main() {
inparallel := flag.Bool("p", false, "Run in parallel")
flag.Parse()
args := CanonicalArgs(os.Args[1:len(os.Args)])
routers := NewRouters(args)
routers.InParallel = *inparallel
bf := bufio.NewReader(os.Stdin)
for {
line, _, err := bf.ReadLine()
if err != nil {
if err != io.EOF {
stderr.Println(err.Error())
}
break
}
routers.Handle(line)
}
}