From 05d3fbcc2a6f0474a0ded95d624ba3a5eb0c9681 Mon Sep 17 00:00:00 2001 From: Ryan Hileman Date: Tue, 22 Dec 2015 21:39:23 -0800 Subject: [PATCH] add stdin remapping support --- operation.go | 3 +-- readline.go | 14 +++++++++++++- terminal.go | 6 +++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/operation.go b/operation.go index 4ea03da..0f93282 100644 --- a/operation.go +++ b/operation.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io" - "os" "golang.org/x/crypto/ssh/terminal" ) @@ -330,7 +329,7 @@ func (o *Operation) Password(prompt string) ([]byte, error) { o.t.EnterRawMode() defer o.t.ExitRawMode() - b, err := terminal.ReadPassword(int(os.Stdin.Fd())) + b, err := terminal.ReadPassword(int(o.cfg.Stdin.Fd())) fmt.Fprint(w, "\r\n") return b, err } diff --git a/readline.go b/readline.go index 06b4211..6a66870 100644 --- a/readline.go +++ b/readline.go @@ -1,6 +1,9 @@ package readline -import "io" +import ( + "io" + "os" +) type Instance struct { Config *Config @@ -8,6 +11,11 @@ type Instance struct { Operation *Operation } +type FdReader interface { + io.Reader + Fd() uintptr +} + type Config struct { // prompt supports ANSI escape sequence, so we can color some characters even in windows Prompt string @@ -30,6 +38,7 @@ type Config struct { InterruptPrompt string EOFPrompt string + Stdin FdReader Stdout io.Writer Stderr io.Writer @@ -46,6 +55,9 @@ func (c *Config) Init() error { return nil } c.inited = true + if c.Stdin == nil { + c.Stdin = os.Stdin + } if c.Stdout == nil { c.Stdout = Stdout } diff --git a/terminal.go b/terminal.go index 24114bd..4e17227 100644 --- a/terminal.go +++ b/terminal.go @@ -36,7 +36,7 @@ func NewTerminal(cfg *Config) (*Terminal, error) { } func (t *Terminal) EnterRawMode() (err error) { - t.state, err = MakeRaw(StdinFd) + t.state, err = MakeRaw(int(t.cfg.Stdin.Fd())) return err } @@ -44,7 +44,7 @@ func (t *Terminal) ExitRawMode() (err error) { if t.state == nil { return } - err = Restore(StdinFd, t.state) + err = Restore(int(t.cfg.Stdin.Fd()), t.state) if err == nil { t.state = nil } @@ -91,7 +91,7 @@ func (t *Terminal) ioloop() { expectNextChar bool ) - buf := bufio.NewReader(Stdin) + buf := bufio.NewReader(t.cfg.Stdin) for { if !expectNextChar { atomic.StoreInt32(&t.isReading, 0)