From 5c24009f2b1fd5a7e5abd4dc33ebdec0c6eaaf25 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Fri, 20 Jan 2017 21:03:08 +0100 Subject: Introduce registry We introduce a registry that contains maps to builder functions. These builder functions return the interface implementation of the modules specified in the configuration. We also make the input module type use the registry. All other module types still have to be converted. --- input/stdin/stdin.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 input/stdin/stdin.go (limited to 'input/stdin') diff --git a/input/stdin/stdin.go b/input/stdin/stdin.go new file mode 100644 index 0000000..fa4d21f --- /dev/null +++ b/input/stdin/stdin.go @@ -0,0 +1,46 @@ +package stdin + +import ( + "bufio" + "fmt" + "io" + "os" + + "github.com/Shugyousha/stasher/input" + "github.com/Shugyousha/stasher/registry" + "github.com/Shugyousha/stasher/work" +) + +func init() { + registry.Inputregistry["input"] = New +} + +type StdinInput struct { + retchan chan *work.Work +} + +func New(map[string]string) input.Input { + return &StdinInput{} +} + +func (i *StdinInput) Start() chan *work.Work { + i.retchan = make(chan *work.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.Work{Data: bs} + } + close(i.retchan) + }() + + return i.retchan +} -- cgit v1.2.3