mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-22 05:29:44 +02:00
use a string builder for credential checking
This commit is contained in:
parent
267ecbe694
commit
4d80c87736
@ -6,7 +6,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
@ -73,12 +72,10 @@ func (self *cmdObjRunner) processOutput(reader io.Reader, writer io.Writer, prom
|
|||||||
checkForCredentialRequest := self.getCheckForCredentialRequestFunc()
|
checkForCredentialRequest := self.getCheckForCredentialRequestFunc()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(reader)
|
scanner := bufio.NewScanner(reader)
|
||||||
scanner.Split(scanWordsWithNewLines)
|
scanner.Split(bufio.ScanBytes)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
text := scanner.Text()
|
newBytes := scanner.Bytes()
|
||||||
self.log.Info(text)
|
askFor, ok := checkForCredentialRequest(newBytes)
|
||||||
output := strings.Trim(text, " ")
|
|
||||||
askFor, ok := checkForCredentialRequest(output)
|
|
||||||
if ok {
|
if ok {
|
||||||
toInput := promptUserForCredential(askFor)
|
toInput := promptUserForCredential(askFor)
|
||||||
// If the return data is empty we don't write anything to stdin
|
// If the return data is empty we don't write anything to stdin
|
||||||
@ -90,13 +87,17 @@ func (self *cmdObjRunner) processOutput(reader io.Reader, writer io.Writer, prom
|
|||||||
}
|
}
|
||||||
|
|
||||||
// having a function that returns a function because we need to maintain some state inbetween calls hence the closure
|
// having a function that returns a function because we need to maintain some state inbetween calls hence the closure
|
||||||
func (self *cmdObjRunner) getCheckForCredentialRequestFunc() func(string) (CredentialType, bool) {
|
func (self *cmdObjRunner) getCheckForCredentialRequestFunc() func([]byte) (CredentialType, bool) {
|
||||||
ttyText := ""
|
var ttyText strings.Builder
|
||||||
// this function takes each word of output from the command and builds up a string to see if we're being asked for a password
|
// this function takes each word of output from the command and builds up a string to see if we're being asked for a password
|
||||||
return func(word string) (CredentialType, bool) {
|
return func(newBytes []byte) (CredentialType, bool) {
|
||||||
ttyText = ttyText + " " + word
|
_, err := ttyText.Write(newBytes)
|
||||||
|
if err != nil {
|
||||||
|
self.log.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
prompts := map[string]CredentialType{
|
prompts := map[string]CredentialType{
|
||||||
|
`Password:`: Password,
|
||||||
`.+'s password:`: Password,
|
`.+'s password:`: Password,
|
||||||
`Password\s*for\s*'.+':`: Password,
|
`Password\s*for\s*'.+':`: Password,
|
||||||
`Username\s*for\s*'.+':`: Username,
|
`Username\s*for\s*'.+':`: Username,
|
||||||
@ -104,8 +105,8 @@ func (self *cmdObjRunner) getCheckForCredentialRequestFunc() func(string) (Crede
|
|||||||
}
|
}
|
||||||
|
|
||||||
for pattern, askFor := range prompts {
|
for pattern, askFor := range prompts {
|
||||||
if match, _ := regexp.MatchString(pattern, ttyText); match {
|
if match, _ := regexp.MatchString(pattern, ttyText.String()); match {
|
||||||
ttyText = ""
|
ttyText.Reset()
|
||||||
return askFor, true
|
return askFor, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -113,49 +114,3 @@ func (self *cmdObjRunner) getCheckForCredentialRequestFunc() func(string) (Crede
|
|||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// scanWordsWithNewLines is a copy of bufio.ScanWords but this also captures new lines
|
|
||||||
// For specific comments about this function take a look at: bufio.ScanWords
|
|
||||||
func scanWordsWithNewLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
||||||
start := 0
|
|
||||||
for width := 0; start < len(data); start += width {
|
|
||||||
var r rune
|
|
||||||
r, width = utf8.DecodeRune(data[start:])
|
|
||||||
if !isSpace(r) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for width, i := 0, start; i < len(data); i += width {
|
|
||||||
var r rune
|
|
||||||
r, width = utf8.DecodeRune(data[i:])
|
|
||||||
if isSpace(r) {
|
|
||||||
return i + width, data[start:i], nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if atEOF && len(data) > start {
|
|
||||||
return len(data), data[start:], nil
|
|
||||||
}
|
|
||||||
return start, nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// isSpace is also copied from the bufio package and has been modified to also captures new lines
|
|
||||||
// For specific comments about this function take a look at: bufio.isSpace
|
|
||||||
func isSpace(r rune) bool {
|
|
||||||
if r <= '\u00FF' {
|
|
||||||
switch r {
|
|
||||||
case ' ', '\t', '\v', '\f':
|
|
||||||
return true
|
|
||||||
case '\u0085', '\u00A0':
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if '\u2000' <= r && r <= '\u200a' {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
switch r {
|
|
||||||
case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user