io.go 776 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package streams
  2. import (
  3. "github.com/golang/protobuf/ptypes"
  4. streamsv1 "github.com/docker/api/protos/streams/v1"
  5. )
  6. // IO implements an io.ReadWriter that forwards everything to the stream
  7. type IO struct {
  8. Stream *Stream
  9. }
  10. func (io *IO) Read(p []byte) (int, error) {
  11. a, err := io.Stream.Recv()
  12. if err != nil {
  13. return 0, err
  14. }
  15. var m streamsv1.BytesMessage
  16. err = ptypes.UnmarshalAny(a, &m)
  17. if err != nil {
  18. return 0, err
  19. }
  20. return copy(p, m.Value), nil
  21. }
  22. func (io *IO) Write(p []byte) (n int, err error) {
  23. if len(p) == 0 {
  24. return 0, nil
  25. }
  26. message := streamsv1.BytesMessage{
  27. Type: streamsv1.IOStream_STDOUT,
  28. Value: p,
  29. }
  30. m, err := ptypes.MarshalAny(&message)
  31. if err != nil {
  32. return 0, err
  33. }
  34. return len(message.Value), io.Stream.SendMsg(m)
  35. }