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

89 lines
2.5 KiB
Go
Raw Normal View History

package gui
import (
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
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
2022-01-08 06:46:35 +02:00
func (gui *Gui) promptUserForCredential(passOrUname oscommands.CredentialType) string {
gui.credentials = make(chan string)
2020-08-11 12:25:36 +02:00
gui.g.Update(func(g *gocui.Gui) error {
2021-04-04 16:31:52 +02:00
credentialsView := gui.Views.Credentials
2020-11-16 11:38:26 +02:00
switch passOrUname {
2022-01-08 06:46:35 +02:00
case oscommands.Username:
2020-10-04 02:00:48 +02:00
credentialsView.Title = gui.Tr.CredentialsUsername
credentialsView.Mask = 0
2022-01-08 06:46:35 +02:00
case oscommands.Password:
2020-10-04 02:00:48 +02:00
credentialsView.Title = gui.Tr.CredentialsPassword
credentialsView.Mask = '*'
2022-01-08 06:46:35 +02:00
case oscommands.Passphrase:
credentialsView.Title = gui.Tr.CredentialsPassphrase
credentialsView.Mask = '*'
}
2020-08-16 05:58:29 +02:00
2021-04-03 06:56:11 +02:00
if err := gui.pushContext(gui.State.Contexts.Credentials); 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() error {
2021-04-04 15:51:59 +02:00
credentialsView := gui.Views.Credentials
2021-10-17 04:00:44 +02:00
message := strings.TrimSpace(credentialsView.TextArea.GetContent())
gui.credentials <- message
2021-10-17 08:38:59 +02:00
credentialsView.ClearTextArea()
2020-08-16 05:58:29 +02:00
if err := gui.returnFromContext(); err != nil {
return err
}
2020-08-16 05:58:29 +02:00
2021-04-08 16:33:39 +02:00
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}
func (gui *Gui) handleCloseCredentialsView() 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 {
2021-12-29 02:50:20 +02:00
keybindingConfig := gui.UserConfig.Keybinding
2020-10-03 06:54:55 +02:00
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
2021-04-04 16:31:52 +02:00
gui.renderString(gui.Views.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
}
2021-04-08 16:33:39 +02:00
_ = gui.returnFromContext()
// 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)
}
}