mirror of https://github.com/hashicorp/packer
parent
608b62f699
commit
c5e6e84806
@ -1,30 +0,0 @@
|
||||
// Package iochan is a Go library for treating `io` readers and writers like
|
||||
// channels.
|
||||
package iochan
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
)
|
||||
|
||||
// LineReader takes an io.Reader and produces the contents of the reader on the
|
||||
// returned channel. Internally bufio.NewScanner is used, io.ScanLines parses
|
||||
// lines and returns them without carriage return. Scan can panic if the split
|
||||
// function returns too many empty tokens without advancing the input.
|
||||
//
|
||||
// The channel will be closed either by reaching the end of the input or an
|
||||
// error.
|
||||
func LineReader(r io.Reader) <-chan string {
|
||||
ch := make(chan string)
|
||||
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(r)
|
||||
defer close(ch)
|
||||
|
||||
for scanner.Scan() {
|
||||
ch <- scanner.Text()
|
||||
}
|
||||
}()
|
||||
|
||||
return ch
|
||||
}
|
||||
@ -1,28 +0,0 @@
|
||||
package iochan
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLineReader(t *testing.T) {
|
||||
|
||||
data := []string{"foo", "bar", "baz"}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
buf.WriteString(strings.Join(data, "\n") + "\n")
|
||||
|
||||
ch := LineReader(buf)
|
||||
|
||||
var result []string
|
||||
expected := data
|
||||
for v := range ch {
|
||||
result = append(result, v)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Fatalf("unexpected results: %#v", result)
|
||||
}
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
package json
|
||||
Loading…
Reference in new issue