mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-10 04:07:18 +02:00
26ca41a40e
I don't know if this is a hack or not: we run a git command and increment the pending action count to 1 but at some point the command requests a username or password, so we need to prompt the user to enter that. At that point we don't want to say that there is a pending action, so we decrement the action count before prompting the user and then re-increment it again afterward. Given that we panic when the counter goes below zero, it's important that it's not zero when we run the git command (should be impossible anyway). I toyed with a different approach using channels and a long-running goroutine that handles all commands that request credentials but it feels over-engineered compared to this commit's approach.
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package helpers
|
|
|
|
import (
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
)
|
|
|
|
type CredentialsHelper struct {
|
|
c *HelperCommon
|
|
}
|
|
|
|
func NewCredentialsHelper(
|
|
c *HelperCommon,
|
|
) *CredentialsHelper {
|
|
return &CredentialsHelper{
|
|
c: c,
|
|
}
|
|
}
|
|
|
|
// promptUserForCredential wait for a username, password or passphrase input from the credentials popup
|
|
// We return a channel rather than returning the string directly so that the calling function knows
|
|
// when the prompt has been created (before the user has entered anything) so that it can
|
|
// note that we're now waiting on user input and lazygit isn't processing anything.
|
|
func (self *CredentialsHelper) PromptUserForCredential(passOrUname oscommands.CredentialType) <-chan string {
|
|
ch := make(chan string)
|
|
|
|
self.c.OnUIThread(func() error {
|
|
title, mask := self.getTitleAndMask(passOrUname)
|
|
|
|
return self.c.Prompt(types.PromptOpts{
|
|
Title: title,
|
|
Mask: mask,
|
|
HandleConfirm: func(input string) error {
|
|
ch <- input + "\n"
|
|
|
|
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
|
},
|
|
HandleClose: func() error {
|
|
ch <- "\n"
|
|
|
|
return nil
|
|
},
|
|
})
|
|
})
|
|
|
|
return ch
|
|
}
|
|
|
|
func (self *CredentialsHelper) getTitleAndMask(passOrUname oscommands.CredentialType) (string, bool) {
|
|
switch passOrUname {
|
|
case oscommands.Username:
|
|
return self.c.Tr.CredentialsUsername, false
|
|
case oscommands.Password:
|
|
return self.c.Tr.CredentialsPassword, true
|
|
case oscommands.Passphrase:
|
|
return self.c.Tr.CredentialsPassphrase, true
|
|
case oscommands.PIN:
|
|
return self.c.Tr.CredentialsPIN, true
|
|
}
|
|
|
|
// should never land here
|
|
panic("unexpected credential request")
|
|
}
|