summaryrefslogtreecommitdiff
path: root/input/stdin
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2017-01-20 21:03:08 +0100
committerSilvan Jegen <s.jegen@gmail.com>2017-01-20 21:03:08 +0100
commit5c24009f2b1fd5a7e5abd4dc33ebdec0c6eaaf25 (patch)
tree7447a384efe6a5b45106fde621e53bdf38ad608a /input/stdin
parentba15251035961020363fa5aff95ff0a361023af9 (diff)
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.
Diffstat (limited to 'input/stdin')
-rw-r--r--input/stdin/stdin.go46
1 files changed, 46 insertions, 0 deletions
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
+}