github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/cmd/serve/restic/stdio_conn.go (about)

     1  // +build go1.9
     2  
     3  package restic
     4  
     5  import (
     6  	"net"
     7  	"os"
     8  )
     9  
    10  // Addr implements net.Addr for stdin/stdout.
    11  type Addr struct{}
    12  
    13  // Network returns the network type as a string.
    14  func (a Addr) Network() string {
    15  	return "stdio"
    16  }
    17  
    18  func (a Addr) String() string {
    19  	return "stdio"
    20  }
    21  
    22  // StdioConn implements a net.Conn via stdin/stdout.
    23  type StdioConn struct {
    24  	stdin  *os.File
    25  	stdout *os.File
    26  }
    27  
    28  func (s *StdioConn) Read(p []byte) (int, error) {
    29  	return s.stdin.Read(p)
    30  }
    31  
    32  func (s *StdioConn) Write(p []byte) (int, error) {
    33  	return s.stdout.Write(p)
    34  }
    35  
    36  // Close closes both streams.
    37  func (s *StdioConn) Close() error {
    38  	err1 := s.stdin.Close()
    39  	err2 := s.stdout.Close()
    40  	if err1 != nil {
    41  		return err1
    42  	}
    43  	return err2
    44  }
    45  
    46  // LocalAddr returns nil.
    47  func (s *StdioConn) LocalAddr() net.Addr {
    48  	return Addr{}
    49  }
    50  
    51  // RemoteAddr returns nil.
    52  func (s *StdioConn) RemoteAddr() net.Addr {
    53  	return Addr{}
    54  }