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

86 lines
2.3 KiB
Go
Raw Normal View History

package gui
import (
"strings"
"github.com/jesseduffield/gocui"
2020-10-04 02:00:48 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
)
type credentials chan string
// promptUserForCredential wait for a username, password or passphrase input from the credentials popup
2020-08-11 12:29:05 +02:00
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")
2020-11-16 11:38:26 +02:00
switch passOrUname {
case "username":
2020-10-04 02:00:48 +02:00
credentialsView.Title = gui.Tr.CredentialsUsername
credentialsView.Mask = 0
2020-11-16 11:38:26 +02:00
case "password":
2020-10-04 02:00:48 +02:00
credentialsView.Title = gui.Tr.CredentialsPassword
credentialsView.Mask = '*'
2020-11-16 11:38:26 +02:00
default:
credentialsView.Title = gui.Tr.CredentialsPassphrase
credentialsView.Mask = '*'
}
2020-08-16 05:58:29 +02:00
if err := gui.pushContext(gui.Contexts.Credentials.Context); err != nil {
return err
}
2020-08-16 05:58:29 +02:00
gui.RenderCommitLength()
return nil
})
// wait for username/passwords/passphrase 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 {
2020-10-03 06:54:55 +02:00
keybindingConfig := gui.Config.GetUserConfig().Keybinding
2020-10-04 02:00:48 +02:00
message := utils.ResolvePlaceholderString(
gui.Tr.CloseConfirm,
map[string]string{
2020-10-03 06:54:55 +02:00
"keyBindClose": gui.getKeyDisplay(keybindingConfig.Universal.Return),
"keyBindConfirm": gui.getKeyDisplay(keybindingConfig.Universal.Confirm),
},
)
2020-10-04 02:00:48 +02:00
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, password or passphrase") {
2020-10-04 02:00:48 +02:00
errMessage = gui.Tr.PassUnameWrong
}
// we are not logging this error because it may contain a password or a passphrase
2020-11-16 11:38:26 +02:00
_ = gui.createErrorPanel(errMessage)
2020-08-11 13:29:18 +02:00
} else {
2020-08-23 05:16:44 +02:00
_ = gui.closeConfirmationPrompt(false)
}
}