From 8853e1719526988930ced52d7900628df157c6f2 Mon Sep 17 00:00:00 2001 From: Cheney Date: Sun, 31 Jan 2016 12:18:46 +0800 Subject: [PATCH] support dial to remote readline --- remote.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 remote.go diff --git a/remote.go b/remote.go new file mode 100644 index 0000000..64517a5 --- /dev/null +++ b/remote.go @@ -0,0 +1,49 @@ +package readline + +import ( + "io" + "net" + "os" +) + +type Conn struct { + Conn net.Conn + Terminal *Terminal + runChan chan error +} + +func NewConn(conn net.Conn, t *Terminal) (*Conn, error) { + return &Conn{ + Conn: conn, + Terminal: t, + runChan: make(chan error), + }, nil +} + +func (c *Conn) Run() error { + c.Terminal.EnterRawMode() + go func() { + _, err := io.Copy(c.Conn, os.Stdin) + c.runChan <- err + }() + go func() { + _, err := io.Copy(os.Stdout, c.Conn) + c.runChan <- err + }() + err := <-c.runChan + c.Terminal.ExitRawMode() + return err +} + +func Dial(network string, address string) (*Remote, error) { + conn, err := net.Dial(network, address) + if err != nil { + return nil, err + } + var cfg Config + t, err := NewTerminal(&cfg) + if err != nil { + return nil, err + } + return NewRemote(conn, t) +}