Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
e753ffe5
Commit
e753ffe5
authored
Jun 01, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: Refactor on StdoutChan so it'll work for stderr easily
parent
810d17c0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
29 deletions
+34
-29
packer/communicator.go
packer/communicator.go
+34
-29
No files found.
packer/communicator.go
View file @
e753ffe5
...
...
@@ -55,35 +55,7 @@ func (r *RemoteCommand) StdoutChan() <-chan string {
// and start the goroutine to read and send to them.
if
r
.
outChans
==
nil
{
r
.
outChans
=
make
([]
chan
<-
string
,
0
,
5
)
go
func
()
{
buf
:=
bufio
.
NewReader
(
r
.
Stdout
)
var
err
error
for
err
!=
io
.
EOF
{
var
data
[]
byte
data
,
err
=
buf
.
ReadSlice
(
'\n'
)
if
len
(
data
)
>
0
{
for
_
,
ch
:=
range
r
.
outChans
{
// Note: this blocks if the channel is full (they
// are buffered by default). What to do?
ch
<-
string
(
data
)
}
}
}
// Clean up the channels by closing them and setting the
// list to nil.
r
.
outChanLock
.
Lock
()
defer
r
.
outChanLock
.
Unlock
()
for
_
,
ch
:=
range
r
.
outChans
{
close
(
ch
)
}
r
.
outChans
=
nil
}()
go
r
.
channelReader
(
r
.
Stdout
,
&
r
.
outChans
,
&
r
.
outChanLock
)
}
// Create the channel, append it to the channels we care about
...
...
@@ -141,3 +113,36 @@ func (r *RemoteCommand) Wait() {
time
.
Sleep
(
10
*
time
.
Millisecond
)
}
}
// Takes an io.Reader and then writes its data to a slice of channels.
// The channel slice is expected to be protected by the given mutex, and
// after the io.Reader is over, all channels will be closed and the slice
// set to nil.
func
(
*
RemoteCommand
)
channelReader
(
r
io
.
Reader
,
chans
*
[]
chan
<-
string
,
chanLock
*
sync
.
Mutex
)
{
buf
:=
bufio
.
NewReader
(
r
)
var
err
error
for
err
!=
io
.
EOF
{
var
data
[]
byte
data
,
err
=
buf
.
ReadSlice
(
'\n'
)
if
len
(
data
)
>
0
{
for
_
,
ch
:=
range
*
chans
{
// Note: this blocks if the channel is full (they
// are buffered by default). What to do?
ch
<-
string
(
data
)
}
}
}
// Clean up the channels by closing them and setting the
// list to nil.
chanLock
.
Lock
()
defer
chanLock
.
Unlock
()
for
_
,
ch
:=
range
*
chans
{
close
(
ch
)
}
*
chans
=
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment