You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
924 B
46 lines
924 B
3 years ago
|
package shell
|
||
4 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"errors"
|
||
4 years ago
|
"github.com/sirupsen/logrus"
|
||
4 years ago
|
"io"
|
||
|
"os/exec"
|
||
|
"path"
|
||
4 years ago
|
"strings"
|
||
4 years ago
|
)
|
||
|
|
||
3 years ago
|
type localShell struct{}
|
||
|
|
||
|
func (s *localShell) Command(logger *logrus.Entry, cmdName string, cmdArgs []string, workingDir string, environ []string) (string, error) {
|
||
4 years ago
|
logger.Infof("Executing command: %s %s", cmdName, strings.Join(cmdArgs, " "))
|
||
4 years ago
|
|
||
|
cmd := exec.Command(cmdName, cmdArgs...)
|
||
|
var stdBuffer bytes.Buffer
|
||
4 years ago
|
|
||
|
logWriters := []io.Writer{
|
||
|
&stdBuffer,
|
||
|
}
|
||
|
if logger.Logger.Level == logrus.DebugLevel {
|
||
|
logWriters = append(logWriters, logger.Logger.Out)
|
||
|
}
|
||
|
|
||
|
mw := io.MultiWriter(logWriters...)
|
||
4 years ago
|
|
||
|
cmd.Stdout = mw
|
||
|
cmd.Stderr = mw
|
||
|
|
||
|
if environ != nil {
|
||
|
cmd.Env = environ
|
||
|
}
|
||
|
if workingDir != "" && path.IsAbs(workingDir) {
|
||
|
cmd.Dir = workingDir
|
||
|
} else if workingDir != "" {
|
||
|
return "", errors.New("Working Directory must be an absolute path")
|
||
|
}
|
||
|
|
||
|
err := cmd.Run()
|
||
|
return stdBuffer.String(), err
|
||
|
|
||
|
}
|