2018-10-27 14:35:07 +02:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-12-18 12:25:49 +02:00
|
|
|
"bytes"
|
2018-10-29 09:23:56 +02:00
|
|
|
"strings"
|
2018-11-06 21:24:10 +02:00
|
|
|
"unicode/utf8"
|
2018-10-27 14:35:07 +02:00
|
|
|
|
2019-02-16 01:18:38 +02:00
|
|
|
"github.com/go-errors/errors"
|
|
|
|
|
2020-03-25 11:37:10 +02:00
|
|
|
"github.com/creack/pty"
|
2018-10-27 14:35:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// RunCommandWithOutputLiveWrapper runs a command and return every word that gets written in stdout
|
|
|
|
// Output is a function that executes by every word that gets read by bufio
|
|
|
|
// As return of output you need to give a string that will be written to stdin
|
|
|
|
// NOTE: If the return data is empty it won't written anything to stdin
|
2018-12-12 23:10:33 +02:00
|
|
|
func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) error {
|
2020-09-26 02:45:13 +02:00
|
|
|
c.Log.WithField("command", command).Info("RunCommand")
|
2019-06-06 12:33:25 +02:00
|
|
|
cmd := c.ExecutableFromString(command)
|
2018-12-19 11:06:58 +02:00
|
|
|
cmd.Env = append(cmd.Env, "LANG=en_US.UTF-8", "LC_ALL=en_US.UTF-8")
|
2018-10-27 14:35:07 +02:00
|
|
|
|
2018-12-18 12:25:49 +02:00
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
|
|
|
|
ptmx, err := pty.Start(cmd)
|
2018-10-27 14:35:07 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2018-12-12 23:10:33 +02:00
|
|
|
return err
|
2018-10-27 14:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2018-12-18 12:25:49 +02:00
|
|
|
scanner := bufio.NewScanner(ptmx)
|
2018-11-06 21:24:10 +02:00
|
|
|
scanner.Split(scanWordsWithNewLines)
|
2018-10-27 14:35:07 +02:00
|
|
|
for scanner.Scan() {
|
2018-12-12 13:34:20 +02:00
|
|
|
toOutput := strings.Trim(scanner.Text(), " ")
|
2018-12-18 13:23:17 +02:00
|
|
|
_, _ = ptmx.WriteString(output(toOutput))
|
2018-10-27 14:35:07 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-11-10 18:02:39 +02:00
|
|
|
err = cmd.Wait()
|
2018-12-18 12:25:49 +02:00
|
|
|
ptmx.Close()
|
2018-11-10 18:02:39 +02:00
|
|
|
if err != nil {
|
2018-12-18 12:25:49 +02:00
|
|
|
return errors.New(stderr.String())
|
2018-10-27 14:35:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-12 23:10:33 +02:00
|
|
|
return nil
|
2018-10-27 14:35:07 +02:00
|
|
|
}
|
2018-11-06 21:24:10 +02:00
|
|
|
|
|
|
|
// scanWordsWithNewLines is a copy of bufio.ScanWords but this also captures new lines
|
2018-11-06 21:37:59 +02:00
|
|
|
// For specific comments about this function take a look at: bufio.ScanWords
|
2018-11-06 21:24:10 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-06 21:37:59 +02:00
|
|
|
// 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
|
2018-11-06 21:24:10 +02:00
|
|
|
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
|
|
|
|
}
|