2018-12-18 22:19:32 +11:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2018-12-19 10:06:58 +01:00
|
|
|
"strings"
|
|
|
|
|
2018-12-18 22:19:32 +11:00
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type credentials chan string
|
|
|
|
|
2020-08-11 20:29:05 +10:00
|
|
|
// promptUserForCredential wait for a username or password input from the credentials popup
|
|
|
|
func (gui *Gui) promptUserForCredential(passOrUname string) string {
|
2018-12-18 22:19:32 +11:00
|
|
|
gui.credentials = make(chan string)
|
2020-08-11 20:25:36 +10:00
|
|
|
gui.g.Update(func(g *gocui.Gui) error {
|
2018-12-18 22:19:32 +11:00
|
|
|
credentialsView, _ := g.View("credentials")
|
|
|
|
if passOrUname == "username" {
|
|
|
|
credentialsView.Title = gui.Tr.SLocalize("CredentialsUsername")
|
|
|
|
credentialsView.Mask = 0
|
|
|
|
} else {
|
|
|
|
credentialsView.Title = gui.Tr.SLocalize("CredentialsPassword")
|
|
|
|
credentialsView.Mask = '*'
|
|
|
|
}
|
2020-08-16 13:58:29 +10:00
|
|
|
|
|
|
|
if err := gui.switchContext(gui.Contexts.Credentials.Context); err != nil {
|
2018-12-18 22:19:32 +11:00
|
|
|
return err
|
|
|
|
}
|
2020-08-16 13:58:29 +10:00
|
|
|
|
2018-12-18 22:19:32 +11:00
|
|
|
gui.RenderCommitLength()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// wait for username/passwords input
|
|
|
|
userInput := <-gui.credentials
|
2018-12-18 22:23:17 +11:00
|
|
|
return userInput + "\n"
|
2018-12-18 22:19:32 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleSubmitCredential(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
message := gui.trimmedContent(v)
|
|
|
|
gui.credentials <- message
|
2020-08-17 21:58:30 +10:00
|
|
|
gui.clearEditorView(v)
|
2020-08-16 13:58:29 +10:00
|
|
|
if err := gui.returnFromContext(); err != nil {
|
2018-12-18 22:19:32 +11:00
|
|
|
return err
|
|
|
|
}
|
2020-08-16 13:58:29 +10:00
|
|
|
|
2020-03-27 21:25:37 +11:00
|
|
|
return gui.refreshSidePanels(refreshOptions{})
|
2018-12-18 22:19:32 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleCloseCredentialsView(g *gocui.Gui, v *gocui.View) error {
|
2018-12-18 22:23:17 +11:00
|
|
|
gui.credentials <- ""
|
2020-08-16 13:58:29 +10:00
|
|
|
return gui.returnFromContext()
|
2018-12-18 22:19:32 +11:00
|
|
|
}
|
|
|
|
|
2020-08-15 17:01:43 +10:00
|
|
|
func (gui *Gui) handleCredentialsViewFocused() error {
|
2018-12-18 22:19:32 +11:00
|
|
|
message := gui.Tr.TemplateLocalize(
|
|
|
|
"CloseConfirm",
|
|
|
|
Teml{
|
2020-08-17 20:45:37 +10:00
|
|
|
"keyBindClose": gui.getKeyDisplay("universal.return"),
|
|
|
|
"keyBindConfirm": gui.getKeyDisplay("universal.confirm"),
|
2018-12-18 22:19:32 +11:00
|
|
|
},
|
|
|
|
)
|
2020-08-15 16:36:39 +10:00
|
|
|
gui.renderString("options", message)
|
2020-03-09 11:34:10 +11:00
|
|
|
return nil
|
2018-12-18 22:19:32 +11:00
|
|
|
}
|
|
|
|
|
2020-08-11 21:41:32 +10:00
|
|
|
// handleCredentialsPopup handles the views after executing a command that might ask for credentials
|
|
|
|
func (gui *Gui) handleCredentialsPopup(cmdErr error) {
|
2018-12-18 22:19:32 +11:00
|
|
|
if cmdErr != nil {
|
2018-12-19 20:12:35 +11:00
|
|
|
errMessage := cmdErr.Error()
|
|
|
|
if strings.Contains(errMessage, "Invalid username or password") {
|
|
|
|
errMessage = gui.Tr.SLocalize("PassUnameWrong")
|
2018-12-19 10:06:58 +01:00
|
|
|
}
|
2018-12-18 22:19:32 +11:00
|
|
|
// we are not logging this error because it may contain a password
|
2020-08-15 16:36:39 +10:00
|
|
|
gui.createErrorPanel(errMessage)
|
2020-08-11 21:29:18 +10:00
|
|
|
} else {
|
2020-08-15 17:23:16 +10:00
|
|
|
_ = gui.closeConfirmationPrompt(true)
|
2018-12-18 22:19:32 +11:00
|
|
|
}
|
|
|
|
}
|