From 5147048d4a67c59f799203e1cc5f79b183208cab Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Wed, 15 Feb 2017 12:55:58 +0100 Subject: Put the Manager in its own package --- conf/parser.go | 8 +++----- main.go | 5 +++-- manager.go | 46 ---------------------------------------------- manager/manager.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 53 deletions(-) delete mode 100644 manager.go create mode 100644 manager/manager.go diff --git a/conf/parser.go b/conf/parser.go index 0e19338..29f8968 100644 --- a/conf/parser.go +++ b/conf/parser.go @@ -21,14 +21,12 @@ import ( "io" "os" + "github.com/Shugyousha/stasher/manager" "github.com/Shugyousha/stasher/registry" ) // Having a Config to Manager function could be nice? Or we could just // return a Manager from here which may be even better... -type Config struct { -} - type parser struct { s scanner last token @@ -48,11 +46,11 @@ func newParser(s *scanner) *parser { return p } -func NewConfig(r io.Reader) *Config { +func NewManagerFromConfig(r io.Reader) *manager.Manager { p := newParser(newScanner(r)) p.startparsing() - return &Config{} + return &manager.Manager{} } func (p *parser) startparsing() { diff --git a/main.go b/main.go index 69677d5..8b2248a 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "github.com/Shugyousha/stasher/conf" "github.com/Shugyousha/stasher/filter/str" "github.com/Shugyousha/stasher/input/stdin" + "github.com/Shugyousha/stasher/manager" "github.com/Shugyousha/stasher/output/stdout" ) @@ -19,13 +20,13 @@ func main() { os.Exit(1) } - c := conf.NewConfig(bufio.NewReader(f)) + c := conf.NewManagerFromConfig(bufio.NewReader(f)) fmt.Fprintf(os.Stderr, "config: %#v\n", c) ffmap := make(map[string]func(string) string, 10) ffmap["F"] = func(s string) string { return strings.ToUpper(s) } - m := Manager{ + m := manager.Manager{ Input: stdin.New(nil), Filter: str.New(ffmap), Output: stdout.New(nil), diff --git a/manager.go b/manager.go deleted file mode 100644 index d3d38fd..0000000 --- a/manager.go +++ /dev/null @@ -1,46 +0,0 @@ -package main - -import ( - "fmt" - "sync" - - "github.com/Shugyousha/stasher/filter" - "github.com/Shugyousha/stasher/input" - "github.com/Shugyousha/stasher/output" - "github.com/Shugyousha/stasher/work" -) - -type Manager struct { - Input input.Input - Filter filter.Filter - Output output.Output -} - -func (m *Manager) Run() { - var wg sync.WaitGroup - - ic := m.Input.Start() - for w := range ic { - if w.Err != nil { - fmt.Printf("Got an error when getting Work input: %q\n", w.Err) - continue - } - wg.Add(1) - - go func(w *work.Work) { - nw := m.Filter.Filter(w) - err := nw.Error() - if err != nil { - fmt.Printf("Got an error when filtering Work: %q\n", err) - } - err = m.Output.Output(nw) - if err != nil { - fmt.Printf("Got an error when outputting Work: %q\n", err) - } - - wg.Done() - }(w) - } - - wg.Wait() -} diff --git a/manager/manager.go b/manager/manager.go new file mode 100644 index 0000000..5bbd49f --- /dev/null +++ b/manager/manager.go @@ -0,0 +1,46 @@ +package manager + +import ( + "fmt" + "sync" + + "github.com/Shugyousha/stasher/filter" + "github.com/Shugyousha/stasher/input" + "github.com/Shugyousha/stasher/output" + "github.com/Shugyousha/stasher/work" +) + +type Manager struct { + Input input.Input + Filter filter.Filter + Output output.Output +} + +func (m *Manager) Run() { + var wg sync.WaitGroup + + ic := m.Input.Start() + for w := range ic { + if w.Err != nil { + fmt.Printf("Got an error when getting Work input: %q\n", w.Err) + continue + } + wg.Add(1) + + go func(w *work.Work) { + nw := m.Filter.Filter(w) + err := nw.Error() + if err != nil { + fmt.Printf("Got an error when filtering Work: %q\n", err) + } + err = m.Output.Output(nw) + if err != nil { + fmt.Printf("Got an error when outputting Work: %q\n", err) + } + + wg.Done() + }(w) + } + + wg.Wait() +} -- cgit v1.2.3