Skip to content

Commit da729e5

Browse files
committed
cmd/geth, console: fix reviewer issues
1 parent 14ae570 commit da729e5

6 files changed

Lines changed: 68 additions & 49 deletions

File tree

‎cmd/geth/accountcmd.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ func getPassPhrase(prompt string, confirmation bool, i int, passwords []string)
216216
if prompt != "" {
217217
fmt.Println(prompt)
218218
}
219-
password, err := console.TerminalPrompter.PromptPassword("Passphrase: ")
219+
password, err := console.Stdin.PromptPassword("Passphrase: ")
220220
if err != nil {
221221
utils.Fatalf("Failed to read passphrase: %v", err)
222222
}
223223
if confirmation {
224-
confirm, err := console.TerminalPrompter.PromptPassword("Repeat passphrase: ")
224+
confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ")
225225
if err != nil {
226226
utils.Fatalf("Failed to read passphrase confirmation: %v", err)
227227
}

‎cmd/geth/chaincmd.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func exportChain(ctx *cli.Context) {
117117
}
118118

119119
func removeDB(ctx *cli.Context) {
120-
confirm, err := console.TerminalPrompter.PromptConfirm("Remove local database?")
120+
confirm, err := console.Stdin.PromptConfirm("Remove local database?")
121121
if err != nil {
122122
utils.Fatalf("%v", err)
123123
}

‎cmd/geth/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ participating.
236236
app.After = func(ctx *cli.Context) error {
237237
logger.Flush()
238238
debug.Exit()
239-
console.TerminalPrompter.Close() // Resets terminal mode.
239+
console.Stdin.Close() // Resets terminal mode.
240240
return nil
241241
}
242242
}

‎console/console.go‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type Console struct {
7474
func New(config Config) (*Console, error) {
7575
// Handle unset config values gracefully
7676
if config.Prompter == nil {
77-
config.Prompter = TerminalPrompter
77+
config.Prompter = Stdin
7878
}
7979
if config.Prompt == "" {
8080
config.Prompt = DefaultPrompt
@@ -192,9 +192,10 @@ func (c *Console) init(preload []string) error {
192192
// Configure the console's input prompter for scrollback and tab completion
193193
if c.prompter != nil {
194194
if content, err := ioutil.ReadFile(c.histPath); err != nil {
195-
c.prompter.SetScrollHistory(nil)
195+
c.prompter.SetHistory(nil)
196196
} else {
197-
c.prompter.SetScrollHistory(strings.Split(string(content), "\n"))
197+
c.history = strings.Split(string(content), "\n")
198+
c.prompter.SetHistory(c.history)
198199
}
199200
c.prompter.SetWordCompleter(c.AutoCompleteInput)
200201
}
@@ -322,7 +323,7 @@ func (c *Console) Interactive() {
322323

323324
case line, ok := <-scheduler:
324325
// User input was returned by the prompter, handle special cases
325-
if !ok || (indents <= 0 && exit.MatchString(input)) {
326+
if !ok || (indents <= 0 && exit.MatchString(line)) {
326327
return
327328
}
328329
if onlyWhitespace.MatchString(line) {
@@ -339,8 +340,13 @@ func (c *Console) Interactive() {
339340
}
340341
// If all the needed lines are present, save the command and run
341342
if indents <= 0 {
342-
if len(input) != 0 && input[0] != ' ' && !passwordRegexp.MatchString(input) {
343-
c.history = append(c.history, input[:len(input)-1])
343+
if len(input) > 0 && input[0] != ' ' && !passwordRegexp.MatchString(input) {
344+
if command := strings.TrimSpace(input); len(c.history) == 0 || command != c.history[len(c.history)-1] {
345+
c.history = append(c.history, command)
346+
if c.prompter != nil {
347+
c.prompter.AppendHistory(command)
348+
}
349+
}
344350
}
345351
c.Evaluate(input)
346352
input = ""
@@ -356,7 +362,10 @@ func (c *Console) Execute(path string) error {
356362

357363
// Stop cleans up the console and terminates the runtime envorinment.
358364
func (c *Console) Stop(graceful bool) error {
359-
if err := ioutil.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), os.ModePerm); err != nil {
365+
if err := ioutil.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), 0600); err != nil {
366+
return err
367+
}
368+
if err := os.Chmod(c.histPath, 0600); err != nil { // Force 0600, even if it was different previously
360369
return err
361370
}
362371
c.jsre.Stop(graceful)

‎console/console_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ func (p *hookedPrompter) PromptPassword(prompt string) (string, error) {
6868
func (p *hookedPrompter) PromptConfirm(prompt string) (bool, error) {
6969
return false, errors.New("not implemented")
7070
}
71-
func (p *hookedPrompter) SetScrollHistory(history []string) {}
71+
func (p *hookedPrompter) SetHistory(history []string) {}
72+
func (p *hookedPrompter) AppendHistory(command string) {}
7273
func (p *hookedPrompter) SetWordCompleter(completer WordCompleter) {}
7374

7475
// tester is a console test environment for the console tests to operate on.

‎console/prompter.go‎

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import (
2323
"github.com/peterh/liner"
2424
)
2525

26-
// TerminalPrompter holds the stdin line reader (also using stdout for printing
27-
// prompts). Only this reader may be used for input because it keeps an internal
28-
// buffer.
29-
var TerminalPrompter = newTerminalPrompter()
26+
// Stdin holds the stdin line reader (also using stdout for printing prompts).
27+
// Only this reader may be used for input because it keeps an internal buffer.
28+
var Stdin = newTerminalPrompter()
3029

3130
// UserPrompter defines the methods needed by the console to promt the user for
3231
// various types of inputs.
@@ -44,9 +43,13 @@ type UserPrompter interface {
4443
// choice to be made, returning that choice.
4544
PromptConfirm(prompt string) (bool, error)
4645

47-
// SetScrollHistory sets the the input scrollback history that the prompter will
48-
// allow the user to scoll back to.
49-
SetScrollHistory(history []string)
46+
// SetHistory sets the the input scrollback history that the prompter will allow
47+
// the user to scoll back to.
48+
SetHistory(history []string)
49+
50+
// AppendHistory appends an entry to the scrollback history. It should be called
51+
// if and only if the prompt to append was a valid command.
52+
AppendHistory(command string)
5053

5154
// SetWordCompleter sets the completion function that the prompter will call to
5255
// fetch completion candidates when the user presses tab.
@@ -74,34 +77,34 @@ type terminalPrompter struct {
7477
// newTerminalPrompter creates a liner based user input prompter working off the
7578
// standard input and output streams.
7679
func newTerminalPrompter() *terminalPrompter {
77-
r := new(terminalPrompter)
80+
p := new(terminalPrompter)
7881
// Get the original mode before calling NewLiner.
7982
// This is usually regular "cooked" mode where characters echo.
8083
normalMode, _ := liner.TerminalMode()
8184
// Turn on liner. It switches to raw mode.
82-
r.State = liner.NewLiner()
85+
p.State = liner.NewLiner()
8386
rawMode, err := liner.TerminalMode()
8487
if err != nil || !liner.TerminalSupported() {
85-
r.supported = false
88+
p.supported = false
8689
} else {
87-
r.supported = true
88-
r.normalMode = normalMode
89-
r.rawMode = rawMode
90+
p.supported = true
91+
p.normalMode = normalMode
92+
p.rawMode = rawMode
9093
// Switch back to normal mode while we're not prompting.
9194
normalMode.ApplyMode()
9295
}
93-
r.SetCtrlCAborts(true)
94-
r.SetTabCompletionStyle(liner.TabPrints)
96+
p.SetCtrlCAborts(true)
97+
p.SetTabCompletionStyle(liner.TabPrints)
9598

96-
return r
99+
return p
97100
}
98101

99102
// PromptInput displays the given prompt to the user and requests some textual
100103
// data to be entered, returning the input of the user.
101-
func (r *terminalPrompter) PromptInput(prompt string) (string, error) {
102-
if r.supported {
103-
r.rawMode.ApplyMode()
104-
defer r.normalMode.ApplyMode()
104+
func (p *terminalPrompter) PromptInput(prompt string) (string, error) {
105+
if p.supported {
106+
p.rawMode.ApplyMode()
107+
defer p.normalMode.ApplyMode()
105108
} else {
106109
// liner tries to be smart about printing the prompt
107110
// and doesn't print anything if input is redirected.
@@ -110,47 +113,53 @@ func (r *terminalPrompter) PromptInput(prompt string) (string, error) {
110113
prompt = ""
111114
defer fmt.Println()
112115
}
113-
return r.State.Prompt(prompt)
116+
return p.State.Prompt(prompt)
114117
}
115118

116119
// PromptPassword displays the given prompt to the user and requests some textual
117120
// data to be entered, but one which must not be echoed out into the terminal.
118121
// The method returns the input provided by the user.
119-
func (r *terminalPrompter) PromptPassword(prompt string) (passwd string, err error) {
120-
if r.supported {
121-
r.rawMode.ApplyMode()
122-
defer r.normalMode.ApplyMode()
123-
return r.State.PasswordPrompt(prompt)
122+
func (p *terminalPrompter) PromptPassword(prompt string) (passwd string, err error) {
123+
if p.supported {
124+
p.rawMode.ApplyMode()
125+
defer p.normalMode.ApplyMode()
126+
return p.State.PasswordPrompt(prompt)
124127
}
125-
if !r.warned {
128+
if !p.warned {
126129
fmt.Println("!! Unsupported terminal, password will be echoed.")
127-
r.warned = true
130+
p.warned = true
128131
}
129132
// Just as in Prompt, handle printing the prompt here instead of relying on liner.
130133
fmt.Print(prompt)
131-
passwd, err = r.State.Prompt("")
134+
passwd, err = p.State.Prompt("")
132135
fmt.Println()
133136
return passwd, err
134137
}
135138

136139
// PromptConfirm displays the given prompt to the user and requests a boolean
137140
// choice to be made, returning that choice.
138-
func (r *terminalPrompter) PromptConfirm(prompt string) (bool, error) {
139-
input, err := r.Prompt(prompt + " [y/N] ")
141+
func (p *terminalPrompter) PromptConfirm(prompt string) (bool, error) {
142+
input, err := p.Prompt(prompt + " [y/N] ")
140143
if len(input) > 0 && strings.ToUpper(input[:1]) == "Y" {
141144
return true, nil
142145
}
143146
return false, err
144147
}
145148

146-
// SetScrollHistory sets the the input scrollback history that the prompter will
147-
// allow the user to scoll back to.
148-
func (r *terminalPrompter) SetScrollHistory(history []string) {
149-
r.State.ReadHistory(strings.NewReader(strings.Join(history, "\n")))
149+
// SetHistory sets the the input scrollback history that the prompter will allow
150+
// the user to scoll back to.
151+
func (p *terminalPrompter) SetHistory(history []string) {
152+
p.State.ReadHistory(strings.NewReader(strings.Join(history, "\n")))
153+
}
154+
155+
// AppendHistory appends an entry to the scrollback history. It should be called
156+
// if and only if the prompt to append was a valid command.
157+
func (p *terminalPrompter) AppendHistory(command string) {
158+
p.State.AppendHistory(command)
150159
}
151160

152161
// SetWordCompleter sets the completion function that the prompter will call to
153162
// fetch completion candidates when the user presses tab.
154-
func (r *terminalPrompter) SetWordCompleter(completer WordCompleter) {
155-
r.State.SetWordCompleter(liner.WordCompleter(completer))
163+
func (p *terminalPrompter) SetWordCompleter(completer WordCompleter) {
164+
p.State.SetWordCompleter(liner.WordCompleter(completer))
156165
}

0 commit comments

Comments
 (0)