Example #1
0
// A custom writer for debugging purposes
func CustomWrite(w io.Writer) pipe.Pipe {
	return pipe.TaskFunc(func(s *pipe.State) error {
		scanner := bufio.NewScanner(s.Stdin)
		for scanner.Scan() {
			_, err := w.Write([]byte("--->" + scanner.Text() + "\n"))
			if err != nil {
				return err
			}
		}
		return nil
	})
}
Example #2
0
func Processor(f func(line []byte) []byte) pipe.Pipe {
	return pipe.TaskFunc(func(s *pipe.State) error {
		r := bufio.NewReader(s.Stdin)
		for {
			line, err := r.ReadBytes('\n')
			if len(line) > 0 {
				line := f(line)
				if len(line) > 0 {
					_, err := s.Stdout.Write(line)
					if err != nil {
						return err
					}
				}
			}
			if err != nil {
				if err == io.EOF {
					return nil
				}
				return err
			}
		}
	})
}