|
| 1 | +package oscommands |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "io" |
| 7 | + "os/exec" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + "unicode/utf8" |
| 11 | + |
| 12 | + "github.com/go-errors/errors" |
| 13 | + "github.com/jesseduffield/lazygit/pkg/utils" |
| 14 | +) |
| 15 | + |
| 16 | +// DetectUnamePass detect a username / password / passphrase question in a command |
| 17 | +// promptUserForCredential is a function that gets executed when this function detect you need to fillin a password or passphrase |
| 18 | +// The promptUserForCredential argument will be "username", "password" or "passphrase" and expects the user's password/passphrase or username back |
| 19 | +func (c *OSCommand) DetectUnamePass(cmdObj ICmdObj, writer io.Writer, promptUserForCredential func(string) string) error { |
| 20 | + ttyText := "" |
| 21 | + errMessage := c.RunCommandWithOutputLive(cmdObj, writer, func(word string) string { |
| 22 | + ttyText = ttyText + " " + word |
| 23 | + |
| 24 | + prompts := map[string]string{ |
| 25 | + `.+'s password:`: "password", |
| 26 | + `Password\s*for\s*'.+':`: "password", |
| 27 | + `Username\s*for\s*'.+':`: "username", |
| 28 | + `Enter\s*passphrase\s*for\s*key\s*'.+':`: "passphrase", |
| 29 | + } |
| 30 | + |
| 31 | + for pattern, askFor := range prompts { |
| 32 | + if match, _ := regexp.MatchString(pattern, ttyText); match { |
| 33 | + ttyText = "" |
| 34 | + return promptUserForCredential(askFor) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return "" |
| 39 | + }) |
| 40 | + return errMessage |
| 41 | +} |
| 42 | + |
| 43 | +// Due to a lack of pty support on windows we have RunCommandWithOutputLiveWrapper being defined |
| 44 | +// separate for windows and other OS's |
| 45 | +func (c *OSCommand) RunCommandWithOutputLive(cmdObj ICmdObj, writer io.Writer, handleOutput func(string) string) error { |
| 46 | + return RunCommandWithOutputLiveWrapper(c, cmdObj, writer, handleOutput) |
| 47 | +} |
| 48 | + |
| 49 | +type cmdHandler struct { |
| 50 | + stdoutPipe io.Reader |
| 51 | + stdinPipe io.Writer |
| 52 | + close func() error |
| 53 | +} |
| 54 | + |
| 55 | +// RunCommandWithOutputLiveAux runs a command and return every word that gets written in stdout |
| 56 | +// Output is a function that executes by every word that gets read by bufio |
| 57 | +// As return of output you need to give a string that will be written to stdin |
| 58 | +// NOTE: If the return data is empty it won't write anything to stdin |
| 59 | +func RunCommandWithOutputLiveAux( |
| 60 | + c *OSCommand, |
| 61 | + cmdObj ICmdObj, |
| 62 | + writer io.Writer, |
| 63 | + // handleOutput takes a word from stdout and returns a string to be written to stdin. |
| 64 | + // See DetectUnamePass above for how this is used to check for a username/password request |
| 65 | + handleOutput func(string) string, |
| 66 | + startCmd func(cmd *exec.Cmd) (*cmdHandler, error), |
| 67 | +) error { |
| 68 | + c.Log.WithField("command", cmdObj.ToString()).Info("RunCommand") |
| 69 | + c.LogCommand(cmdObj.ToString(), true) |
| 70 | + cmd := cmdObj.AddEnvVars("LANG=en_US.UTF-8", "LC_ALL=en_US.UTF-8").GetCmd() |
| 71 | + |
| 72 | + var stderr bytes.Buffer |
| 73 | + cmd.Stderr = io.MultiWriter(writer, &stderr) |
| 74 | + |
| 75 | + handler, err := startCmd(cmd) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + defer func() { |
| 81 | + if closeErr := handler.close(); closeErr != nil { |
| 82 | + c.Log.Error(closeErr) |
| 83 | + } |
| 84 | + }() |
| 85 | + |
| 86 | + tr := io.TeeReader(handler.stdoutPipe, writer) |
| 87 | + |
| 88 | + go utils.Safe(func() { |
| 89 | + scanner := bufio.NewScanner(tr) |
| 90 | + scanner.Split(scanWordsWithNewLines) |
| 91 | + for scanner.Scan() { |
| 92 | + text := scanner.Text() |
| 93 | + output := strings.Trim(text, " ") |
| 94 | + toInput := handleOutput(output) |
| 95 | + if toInput != "" { |
| 96 | + _, _ = handler.stdinPipe.Write([]byte(toInput)) |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + err = cmd.Wait() |
| 102 | + if err != nil { |
| 103 | + return errors.New(stderr.String()) |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// scanWordsWithNewLines is a copy of bufio.ScanWords but this also captures new lines |
| 110 | +// For specific comments about this function take a look at: bufio.ScanWords |
| 111 | +func scanWordsWithNewLines(data []byte, atEOF bool) (advance int, token []byte, err error) { |
| 112 | + start := 0 |
| 113 | + for width := 0; start < len(data); start += width { |
| 114 | + var r rune |
| 115 | + r, width = utf8.DecodeRune(data[start:]) |
| 116 | + if !isSpace(r) { |
| 117 | + break |
| 118 | + } |
| 119 | + } |
| 120 | + for width, i := 0, start; i < len(data); i += width { |
| 121 | + var r rune |
| 122 | + r, width = utf8.DecodeRune(data[i:]) |
| 123 | + if isSpace(r) { |
| 124 | + return i + width, data[start:i], nil |
| 125 | + } |
| 126 | + } |
| 127 | + if atEOF && len(data) > start { |
| 128 | + return len(data), data[start:], nil |
| 129 | + } |
| 130 | + return start, nil, nil |
| 131 | +} |
| 132 | + |
| 133 | +// isSpace is also copied from the bufio package and has been modified to also captures new lines |
| 134 | +// For specific comments about this function take a look at: bufio.isSpace |
| 135 | +func isSpace(r rune) bool { |
| 136 | + if r <= '\u00FF' { |
| 137 | + switch r { |
| 138 | + case ' ', '\t', '\v', '\f': |
| 139 | + return true |
| 140 | + case '\u0085', '\u00A0': |
| 141 | + return true |
| 142 | + } |
| 143 | + return false |
| 144 | + } |
| 145 | + if '\u2000' <= r && r <= '\u200a' { |
| 146 | + return true |
| 147 | + } |
| 148 | + switch r { |
| 149 | + case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000': |
| 150 | + return true |
| 151 | + } |
| 152 | + return false |
| 153 | +} |
0 commit comments