1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/credentials_panel.go

78 lines
2.0 KiB
Go
Raw Normal View History

package gui
import (
"strings"
"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 {
gui.credentials = make(chan string)
2020-08-11 12:25:36 +02:00
gui.g.Update(func(g *gocui.Gui) error {
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 05:58:29 +02:00
if err := gui.switchContext(gui.Contexts.Credentials.Context); err != nil {
return err
}
2020-08-16 05:58:29 +02:00
gui.RenderCommitLength()
return nil
})
// wait for username/passwords input
userInput := <-gui.credentials
return userInput + "\n"
}
func (gui *Gui) handleSubmitCredential(g *gocui.Gui, v *gocui.View) error {
message := gui.trimmedContent(v)
gui.credentials <- message
2020-08-17 13:58:30 +02:00
gui.clearEditorView(v)
2020-08-16 05:58:29 +02:00
if err := gui.returnFromContext(); err != nil {
return err
}
2020-08-16 05:58:29 +02:00
return gui.refreshSidePanels(refreshOptions{})
}
func (gui *Gui) handleCloseCredentialsView(g *gocui.Gui, v *gocui.View) error {
gui.credentials <- ""
2020-08-16 05:58:29 +02:00
return gui.returnFromContext()
}
2020-08-15 09:01:43 +02:00
func (gui *Gui) handleCredentialsViewFocused() error {
message := gui.Tr.TemplateLocalize(
"CloseConfirm",
Teml{
2020-08-17 12:45:37 +02:00
"keyBindClose": gui.getKeyDisplay("universal.return"),
"keyBindConfirm": gui.getKeyDisplay("universal.confirm"),
},
)
2020-08-15 08:36:39 +02:00
gui.renderString("options", message)
2020-03-09 02:34:10 +02:00
return nil
}
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) {
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")
}
// 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)
}
}