summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--manager.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/manager.go b/manager.go
new file mode 100644
index 0000000..0c933ec
--- /dev/null
+++ b/manager.go
@@ -0,0 +1,113 @@
+package main
+
+import (
+ "bufio"
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "os"
+)
+
+type Manager struct {
+ Input Input
+ Filter Filter
+ Output Output
+}
+
+type Input interface {
+ Start() chan *Work
+}
+
+type StdinInput struct {
+ retchan chan *Work
+}
+
+type Filter interface {
+ Filter(chan *Work) chan *Work
+}
+
+type StringFilter struct {
+ retchan chan *Work
+}
+
+type Output interface {
+ Output(chan *Work)
+}
+
+type StdoutOutput struct {
+}
+
+type Work struct {
+ data []byte
+ err error
+}
+
+func (i *StdinInput) Start() chan *Work {
+ i.retchan = make(chan *Work, 100)
+ r := bufio.NewReader(os.Stdin)
+
+ go func() {
+ for {
+ bs, err := r.ReadBytes(byte('\n'))
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ fmt.Printf("Error when reading input from Stdin: %q", err)
+ os.Exit(1)
+ }
+ i.retchan <- &Work{data: bs}
+ }
+ }()
+
+ return i.retchan
+}
+
+func (f *StringFilter) Filter(ic chan *Work) chan *Work {
+ f.retchan = make(chan *Work, 100)
+
+ go func() {
+ for w := range ic {
+ dec := json.NewDecoder(bytes.NewReader(w.data))
+
+ for {
+ tok, err := dec.Token()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ fmt.Printf("Error when reading JSON: %q\n", err)
+ os.Exit(1)
+ }
+ fmt.Printf("Token: %T %v\n", tok, tok)
+ }
+
+ f.retchan <- w
+ }
+ }()
+
+ return f.retchan
+}
+
+func (o *StdoutOutput) Output(wc chan *Work) {
+ for w := range wc {
+ fmt.Printf("%s\n", w.data)
+ }
+}
+
+func (m *Manager) Go() {
+ ic := m.Input.Start()
+ fc := m.Filter.Filter(ic)
+ m.Output.Output(fc)
+}
+
+func main() {
+ m := Manager{
+ Input: &StdinInput{},
+ Filter: &StringFilter{},
+ Output: &StdoutOutput{},
+ }
+
+ m.Go()
+}