Skip to content

Commit 09f3dce

Browse files
committed
streamline terminal input
- use ssh/terminal for everything, but in paticular my patch, which correctly cancels out on Control-C - get rid of miniline - get rid of gopass
1 parent 33bc1eb commit 09f3dce

20 files changed

Lines changed: 1272 additions & 482 deletions

File tree

‎go/minterm/minterm.go‎

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ package minterm
77
import (
88
"errors"
99
"fmt"
10+
"github.com/keybase/go-crypto/ssh/terminal"
11+
"io"
1012
"os"
1113
"strings"
12-
13-
"github.com/keybase/gopass"
14-
"github.com/keybase/miniline"
1514
)
1615

1716
// MinTerm is a minimal terminal interface.
1817
type MinTerm struct {
19-
termIn *os.File
20-
termOut *os.File
21-
width int
22-
height int
18+
termIn *os.File
19+
termOut *os.File
20+
closeTermOut bool
21+
width int
22+
height int
2323
}
2424

2525
var ErrPromptInterrupted = errors.New("prompt interrupted")
@@ -42,7 +42,7 @@ func (m *MinTerm) Shutdown() error {
4242
if m.termIn != nil {
4343
go m.termIn.Close()
4444
}
45-
if m.termOut != nil {
45+
if m.termOut != nil && m.closeTermOut {
4646
go m.termOut.Close()
4747
}
4848
return nil
@@ -62,11 +62,8 @@ func (m *MinTerm) Write(s string) error {
6262
// Prompt gets a line of input from the terminal. It displays the text in
6363
// the prompt parameter first.
6464
func (m *MinTerm) Prompt(prompt string) (string, error) {
65-
s, err := miniline.ReadLine(prompt)
66-
if err == miniline.ErrInterrupted {
67-
return "", ErrPromptInterrupted
68-
}
69-
return s, nil
65+
m.Write(prompt)
66+
return m.readLine()
7067
}
7168

7269
// PromptPassword gets a line of input from the terminal, but
@@ -76,12 +73,38 @@ func (m *MinTerm) PromptPassword(prompt string) (string, error) {
7673
if !strings.HasSuffix(prompt, ": ") {
7774
m.Write(": ")
7875
}
79-
b, err := gopass.GetPasswd(int(m.termIn.Fd()))
76+
return m.readSecret()
77+
}
78+
79+
func (m *MinTerm) fdIn() int { return int(m.termIn.Fd()) }
80+
81+
func (m *MinTerm) readLine() (string, error) {
82+
fd := int(m.fdIn())
83+
oldState, err := terminal.MakeRaw(fd)
84+
if err != nil {
85+
return "", err
86+
}
87+
defer terminal.Restore(fd, oldState)
88+
var ret string
89+
ret, err = terminal.NewTerminal(m.termIn, "").ReadLine()
90+
return ret, convertErr(err)
91+
}
92+
93+
func (m *MinTerm) readSecret() (string, error) {
94+
fd := int(m.fdIn())
95+
oldState, err := terminal.MakeRaw(fd)
8096
if err != nil {
81-
if err == gopass.ErrInterrupted {
82-
err = ErrPromptInterrupted
83-
}
8497
return "", err
8598
}
86-
return string(b), nil
99+
defer terminal.Restore(fd, oldState)
100+
var ret string
101+
ret, err = terminal.NewTerminal(m.termIn, "").ReadPassword("")
102+
return ret, convertErr(err)
103+
}
104+
105+
func convertErr(e error) error {
106+
if e == io.ErrUnexpectedEOF {
107+
e = ErrPromptInterrupted
108+
}
109+
return e
87110
}

‎go/minterm/minterm_nix.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func (m *MinTerm) open() error {
1717
}
1818
m.termIn = f
1919
m.termOut = f
20+
m.closeTermOut = false // since it's repeated
2021
fd := int(f.Fd())
2122
w, h, err := terminal.GetSize(fd)
2223
if err != nil {

‎go/minterm/minterm_windows.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (m *MinTerm) open() error {
2424

2525
m.termIn = fin
2626
m.termOut = fout
27+
m.closeTermOut = true // since it's a different file...
2728
fdout := int(fout.Fd())
2829
w, h, err := terminal.GetSize(fdout)
2930
if err != nil {

0 commit comments

Comments
 (0)