From 63f5bbbc739f3c53b55888d843252401f55e905a Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 11 Apr 2019 14:18:49 +0200 Subject: [PATCH] use our own copy of iochan.LineReader --- builder/docker/exec.go | 6 +- common/iochan/iochan.go | 28 +++++++++ common/iochan/iochan_test.go | 28 +++++++++ go.mod | 1 - go.sum | 2 - packer/communicator.go | 2 +- packer/communicator_test.go | 2 +- vendor/github.com/mitchellh/iochan/LICENSE.md | 21 ------- vendor/github.com/mitchellh/iochan/README.md | 13 ---- vendor/github.com/mitchellh/iochan/go.mod | 1 - vendor/github.com/mitchellh/iochan/iochan.go | 63 ------------------- vendor/modules.txt | 2 - 12 files changed, 61 insertions(+), 108 deletions(-) create mode 100644 common/iochan/iochan.go create mode 100644 common/iochan/iochan_test.go delete mode 100644 vendor/github.com/mitchellh/iochan/LICENSE.md delete mode 100644 vendor/github.com/mitchellh/iochan/README.md delete mode 100644 vendor/github.com/mitchellh/iochan/go.mod delete mode 100644 vendor/github.com/mitchellh/iochan/iochan.go diff --git a/builder/docker/exec.go b/builder/docker/exec.go index 386c5aa79..8aa2c019d 100644 --- a/builder/docker/exec.go +++ b/builder/docker/exec.go @@ -10,8 +10,8 @@ import ( "sync" "syscall" + "github.com/hashicorp/packer/common/iochan" "github.com/hashicorp/packer/packer" - "github.com/mitchellh/iochan" ) func runAndStream(cmd *exec.Cmd, ui packer.Ui) error { @@ -40,8 +40,8 @@ func runAndStream(cmd *exec.Cmd, ui packer.Ui) error { // Create the channels we'll use for data exitCh := make(chan int, 1) - stdoutCh := iochan.DelimReader(stdout_r, '\n') - stderrCh := iochan.DelimReader(stderr_r, '\n') + stdoutCh := iochan.LineReader(stdout_r) + stderrCh := iochan.LineReader(stderr_r) // Start the goroutine to watch for the exit go func() { diff --git a/common/iochan/iochan.go b/common/iochan/iochan.go new file mode 100644 index 000000000..59541ba4e --- /dev/null +++ b/common/iochan/iochan.go @@ -0,0 +1,28 @@ +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 +} diff --git a/common/iochan/iochan_test.go b/common/iochan/iochan_test.go new file mode 100644 index 000000000..a600aca0a --- /dev/null +++ b/common/iochan/iochan_test.go @@ -0,0 +1,28 @@ +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) + } +} diff --git a/go.mod b/go.mod index d1725813d..d3189ed45 100644 --- a/go.mod +++ b/go.mod @@ -71,7 +71,6 @@ require ( github.com/mitchellh/go-fs v0.0.0-20180402234041-7b48fa161ea7 github.com/mitchellh/go-homedir v1.0.0 github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed - github.com/mitchellh/iochan v1.0.1-0.20190408094311-e9f5309a3061 github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc github.com/mitchellh/panicwrap v0.0.0-20170106182340-fce601fe5557 github.com/mitchellh/prefixedio v0.0.0-20151214002211-6e6954073784 diff --git a/go.sum b/go.sum index 7bb800a2c..ab1c42279 100644 --- a/go.sum +++ b/go.sum @@ -227,8 +227,6 @@ github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed h1:FI2NIv6fpef6BQ github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed/go.mod h1:3rdaFaCv4AyBgu5ALFM0+tSuHrBh6v692nyQe3ikrq0= github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= -github.com/mitchellh/iochan v1.0.1-0.20190408094311-e9f5309a3061 h1:BSEloc+wp5WA/fu0jw5HBWOfoKLdvpqi38ZP22eNemg= -github.com/mitchellh/iochan v1.0.1-0.20190408094311-e9f5309a3061/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc h1:5T6hzGUO5OrL6MdYXYoLQtRWJDDgjdlOVBn9mIqGY1g= github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= diff --git a/packer/communicator.go b/packer/communicator.go index 3c2b9c07c..a86c115ad 100644 --- a/packer/communicator.go +++ b/packer/communicator.go @@ -8,7 +8,7 @@ import ( "golang.org/x/sync/errgroup" - "github.com/mitchellh/iochan" + "github.com/hashicorp/packer/common/iochan" ) // CmdDisconnect is a sentinel value to indicate a RemoteCmd diff --git a/packer/communicator_test.go b/packer/communicator_test.go index 8e80a5518..097ca25d0 100644 --- a/packer/communicator_test.go +++ b/packer/communicator_test.go @@ -9,7 +9,7 @@ import ( "time" "github.com/google/go-cmp/cmp" - "github.com/mitchellh/iochan" + "github.com/hashicorp/packer/common/iochan" "golang.org/x/sync/errgroup" ) diff --git a/vendor/github.com/mitchellh/iochan/LICENSE.md b/vendor/github.com/mitchellh/iochan/LICENSE.md deleted file mode 100644 index 762008c22..000000000 --- a/vendor/github.com/mitchellh/iochan/LICENSE.md +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2015 Mitchell Hashimoto - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/vendor/github.com/mitchellh/iochan/README.md b/vendor/github.com/mitchellh/iochan/README.md deleted file mode 100644 index 23771b1b9..000000000 --- a/vendor/github.com/mitchellh/iochan/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# iochan - -iochan is a Go library for treating `io` readers and writers like channels. -This is useful when sometimes you wish to use `io.Reader` and such in `select` -statements. - -## Installation - -Standard `go get`: - -``` -$ go get github.com/mitchellh/iochan -``` diff --git a/vendor/github.com/mitchellh/iochan/go.mod b/vendor/github.com/mitchellh/iochan/go.mod deleted file mode 100644 index 50ab2f14e..000000000 --- a/vendor/github.com/mitchellh/iochan/go.mod +++ /dev/null @@ -1 +0,0 @@ -module github.com/mitchellh/iochan diff --git a/vendor/github.com/mitchellh/iochan/iochan.go b/vendor/github.com/mitchellh/iochan/iochan.go deleted file mode 100644 index 29c8932f0..000000000 --- a/vendor/github.com/mitchellh/iochan/iochan.go +++ /dev/null @@ -1,63 +0,0 @@ -package iochan - -import ( - "bufio" - "io" -) - -// DelimReader takes an io.Reader and produces the contents of the reader -// on the returned channel. The contents on the channel will be returned -// on boundaries specified by the delim parameter, and will include this -// delimiter. -// -// If an error occurs while reading from the reader, the reading will end. -// -// In the case of an EOF or error, the channel will be closed. -// -// This must only be called once for any individual reader. The behavior is -// unknown and will be unexpected if this is called multiple times with the -// same reader. -func DelimReader(r io.Reader, delim byte) <-chan string { - ch := make(chan string) - - go func() { - buf := bufio.NewReader(r) - - for { - line, err := buf.ReadString(delim) - if line != "" { - ch <- line - } - - if err != nil { - break - } - } - - close(ch) - }() - - return ch -} - -// 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 -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 43c4b62c5..4e6e76bba 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -300,8 +300,6 @@ github.com/mitchellh/go-homedir github.com/mitchellh/go-testing-interface # github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed github.com/mitchellh/go-vnc -# github.com/mitchellh/iochan v1.0.1-0.20190408094311-e9f5309a3061 -github.com/mitchellh/iochan # github.com/mitchellh/mapstructure v0.0.0-20180111000720-b4575eea38cc github.com/mitchellh/mapstructure # github.com/mitchellh/panicwrap v0.0.0-20170106182340-fce601fe5557