diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2019-07-10 16:50:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-10 16:50:45 +0200 |
commit | de2ecfe59c07cdd303b2242892f325c8c7ec611d (patch) | |
tree | 1231b5de06f6a6368926edb225a8aaf91775dcdb /pkg/channelwriter/channelwriter.go | |
parent | 81e722d08617ee19235bf57de6d86124e6b4574a (diff) | |
parent | 780b05610ecf9ea107ec69c913dd074d69c1dc88 (diff) | |
download | podman-de2ecfe59c07cdd303b2242892f325c8c7ec611d.tar.gz podman-de2ecfe59c07cdd303b2242892f325c8c7ec611d.tar.bz2 podman-de2ecfe59c07cdd303b2242892f325c8c7ec611d.zip |
Merge pull request #3367 from baude/varlinkmore
account for varlink calls that dont use more
Diffstat (limited to 'pkg/channelwriter/channelwriter.go')
-rw-r--r-- | pkg/channelwriter/channelwriter.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/pkg/channelwriter/channelwriter.go b/pkg/channelwriter/channelwriter.go new file mode 100644 index 000000000..d51400eb3 --- /dev/null +++ b/pkg/channelwriter/channelwriter.go @@ -0,0 +1,34 @@ +package channelwriter + +import "github.com/pkg/errors" + +// Writer is an io.writer-like object that "writes" to a channel +// instead of a buffer or file, etc. It is handy for varlink endpoints when +// needing to handle endpoints that do logging "real-time" +type Writer struct { + ByteChannel chan []byte +} + +// NewChannelWriter creates a new channel writer and adds a +// byte slice channel into it. +func NewChannelWriter() *Writer { + byteChannel := make(chan []byte) + return &Writer{ + ByteChannel: byteChannel, + } +} + +// Write method for Writer +func (c *Writer) Write(w []byte) (int, error) { + if c.ByteChannel == nil { + return 0, errors.New("channel writer channel cannot be nil") + } + c.ByteChannel <- w + return len(w), nil +} + +// Close method for Writer +func (c *Writer) Close() error { + close(c.ByteChannel) + return nil +} |