2018-12-18 13:19:32 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2018-12-19 11:06:58 +02:00
|
|
|
"strings"
|
|
|
|
|
2018-12-18 13:19:32 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type credentials chan string
|
|
|
|
|
2020-08-11 12:29:05 +02:00
|
|
|
// promptUserForCredential wait for a username or password input from the credentials popup
|
|
|
|
func (gui *Gui) promptUserForCredential(passOrUname string) string {
|
2018-12-18 13:19:32 +02:00
|
|
|
gui.credentials = make(chan string)
|
2020-08-11 12:25:36 +02:00
|
|
|
gui.g.Update(func(g *gocui.Gui) error {
|
2018-12-18 13:19:32 +02: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-15 08:36:39 +02:00
|
|
|
err := gui.switchFocus(gui.g.CurrentView(), credentialsView)
|
2018-12-18 13:19:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gui.RenderCommitLength()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// wait for username/passwords input
|
|
|
|
userInput := <-gui.credentials
|
2018-12-18 13:23:17 +02:00
|
|
|
return userInput + "\n"
|
2018-12-18 13:19:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleSubmitCredential(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
message := gui.trimmedContent(v)
|
|
|
|
gui.credentials <- message
|
|
|
|
v.Clear()
|
2020-03-28 00:57:36 +02:00
|
|
|
_ = v.SetCursor(0, 0)
|
|
|
|
_, _ = g.SetViewOnBottom("credentials")
|
2018-12-18 13:19:32 +02:00
|
|
|
nextView, err := gui.g.View("confirmation")
|
|
|
|
if err != nil {
|
2019-02-11 12:07:12 +02:00
|
|
|
nextView = gui.getFilesView()
|
2018-12-18 13:19:32 +02:00
|
|
|
}
|
2020-08-15 08:36:39 +02:00
|
|
|
err = gui.switchFocus(nil, nextView)
|
2018-12-18 13:19:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-27 12:25:37 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{})
|
2018-12-18 13:19:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleCloseCredentialsView(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
_, err := g.SetViewOnBottom("credentials")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:23:17 +02:00
|
|
|
gui.credentials <- ""
|
2020-08-15 08:36:39 +02:00
|
|
|
return gui.switchFocus(nil, gui.getFilesView())
|
2018-12-18 13:19:32 +02:00
|
|
|
}
|
|
|
|
|
2020-08-15 09:01:43 +02:00
|
|
|
func (gui *Gui) handleCredentialsViewFocused() error {
|
|
|
|
if _, err := gui.g.SetViewOnTop("credentials"); err != nil {
|
2018-12-18 13:19:32 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
message := gui.Tr.TemplateLocalize(
|
|
|
|
"CloseConfirm",
|
|
|
|
Teml{
|
|
|
|
"keyBindClose": "esc",
|
|
|
|
"keyBindConfirm": "enter",
|
|
|
|
},
|
|
|
|
)
|
2020-08-15 08:36:39 +02:00
|
|
|
gui.renderString("options", message)
|
2020-03-09 02:34:10 +02:00
|
|
|
return nil
|
2018-12-18 13:19:32 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 13:41:32 +02:00
|
|
|
// handleCredentialsPopup handles the views after executing a command that might ask for credentials
|
|
|
|
func (gui *Gui) handleCredentialsPopup(cmdErr error) {
|
2020-08-11 12:18:50 +02:00
|
|
|
_, _ = gui.g.SetViewOnBottom("credentials")
|
2018-12-18 13:19:32 +02:00
|
|
|
if cmdErr != nil {
|
2018-12-19 11:12:35 +02:00
|
|
|
errMessage := cmdErr.Error()
|
|
|
|
if strings.Contains(errMessage, "Invalid username or password") {
|
|
|
|
errMessage = gui.Tr.SLocalize("PassUnameWrong")
|
2018-12-19 11:06:58 +02:00
|
|
|
}
|
2018-12-18 13:19:32 +02:00
|
|
|
// we are not logging this error because it may contain a password
|
2020-08-15 08:36:39 +02:00
|
|
|
gui.createErrorPanel(errMessage)
|
2020-08-11 13:29:18 +02:00
|
|
|
} else {
|
2020-08-15 09:23:16 +02:00
|
|
|
_ = gui.closeConfirmationPrompt(true)
|
2018-12-18 13:19:32 +02:00
|
|
|
}
|
|
|
|
}
|