remove iochan package from sdk, switch to using its descendent, mitchellh/iochan

This commit is contained in:
Megan Marsh 2020-12-11 16:50:11 -08:00
parent 608b62f699
commit c5e6e84806
5 changed files with 6 additions and 65 deletions

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -1 +0,0 @@
package json

View File

@ -9,7 +9,7 @@ import (
"time"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/packer/packer-plugin-sdk/iochan"
"github.com/mitchellh/iochan"
"golang.org/x/sync/errgroup"
)
@ -42,8 +42,8 @@ func TestRemoteCmd_StartWithUi(t *testing.T) {
testPrintFn := func(in io.Reader, expected []string) error {
i := 0
got := []string{}
for output := range iochan.LineReader(in) {
got = append(got, output)
for output := range iochan.DelimReader(in, '\n') {
got = append(got, strings.TrimSpace(output))
i++
if i == len(expected) {
// here ideally the LineReader chan should be closed, but since

View File

@ -10,8 +10,8 @@ import (
"sync"
"syscall"
"github.com/hashicorp/packer/packer-plugin-sdk/iochan"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/mitchellh/iochan"
)
// RunAndStream allows you to run a local command and stream output to the UI.
@ -39,8 +39,8 @@ func RunAndStream(cmd *exec.Cmd, ui packersdk.Ui, sensitive []string) error {
// Create the channels we'll use for data
exitCh := make(chan int, 1)
stdoutCh := iochan.LineReader(stdout_r)
stderrCh := iochan.LineReader(stderr_r)
stdoutCh := iochan.DelimReader(stdout_r, '\n')
stderrCh := iochan.DelimReader(stderr_r, '\n')
// Start the goroutine to watch for the exit
go func() {