File tree Expand file tree Collapse file tree 3 files changed +22
-4
lines changed Expand file tree Collapse file tree 3 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ Package input reads user input at the console. http://github.com/tcnksm/go-input
16
16
package input
17
17
18
18
import (
19
+ "bufio"
19
20
"errors"
20
21
"io"
21
22
"os"
@@ -54,6 +55,8 @@ type UI struct {
54
55
mask bool
55
56
maskVal string
56
57
58
+ bReader * bufio.Reader
59
+
57
60
once sync.Once
58
61
}
59
62
@@ -75,6 +78,10 @@ func (i *UI) setDefault() {
75
78
if i .Reader == nil {
76
79
i .Reader = defaultReader
77
80
}
81
+
82
+ if i .bReader == nil {
83
+ i .bReader = bufio .NewReader (i .Reader )
84
+ }
78
85
}
79
86
80
87
// ValidateFunc is function to validate the user input.
@@ -163,8 +170,7 @@ func (o *Options) readOpts() *readOptions {
163
170
}
164
171
}
165
172
166
- // maskString is used to mask string which should not be displayed
167
- // directly like auth token
173
+ // maskString is used to mask string which should not be displayed.
168
174
func maskString (s string ) string {
169
175
if len (s ) < 3 {
170
176
return "*******"
Original file line number Diff line number Diff line change 5
5
"io"
6
6
"os"
7
7
"os/signal"
8
+ "strings"
8
9
)
9
10
10
11
// readOptions is option for read func
@@ -40,10 +41,12 @@ func (i *UI) read(opts *readOptions) (string, error) {
40
41
i .mask , i .maskVal = opts .mask , opts .maskVal
41
42
resultStr , resultErr = i .rawRead (f )
42
43
} else {
43
- _ , err := fmt . Fscanln ( i . Reader , & resultStr )
44
- if err != nil && err . Error () != "unexpected newline" {
44
+ line , err := i . bReader . ReadString ( '\n' )
45
+ if err != nil && err != io . EOF {
45
46
resultErr = fmt .Errorf ("failed to read the input: %s" , err )
46
47
}
48
+
49
+ resultStr = strings .TrimSuffix (line , "\n " )
47
50
}
48
51
}()
49
52
Original file line number Diff line number Diff line change @@ -22,6 +22,15 @@ func TestRead(t *testing.T) {
22
22
expect : "passw0rd" ,
23
23
},
24
24
25
+ {
26
+ opts : & readOptions {
27
+ mask : false ,
28
+ maskVal : "" ,
29
+ },
30
+ userInput : bytes .NewBufferString ("taichi nakashima" ),
31
+ expect : "taichi nakashima" ,
32
+ },
33
+
25
34
// No good way to test masking...
26
35
}
27
36
You can’t perform that action at this time.
0 commit comments