adding ability to write a log file with all output from collector. Executing commands will now log be logged (and when debug is enabled, their output's are also logged).
This commit is contained in:
@@ -3,17 +3,19 @@ package common
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExecCmd(cmdName string, cmdArgs []string, workingDir string, environ []string) (string, error) {
|
||||
func ExecCmd(logger *logrus.Entry, cmdName string, cmdArgs []string, workingDir string, environ []string) (string, error) {
|
||||
logger.Infof("Executing command: %s %s", cmdName, strings.Join(cmdArgs, " "))
|
||||
|
||||
cmd := exec.Command(cmdName, cmdArgs...)
|
||||
var stdBuffer bytes.Buffer
|
||||
mw := io.MultiWriter(os.Stdout, &stdBuffer)
|
||||
mw := io.MultiWriter(logger.Logger.Out, &stdBuffer)
|
||||
|
||||
cmd.Stdout = mw
|
||||
cmd.Stderr = mw
|
||||
|
||||
@@ -2,6 +2,7 @@ package common_test
|
||||
|
||||
import (
|
||||
"github.com/analogj/scrutiny/collector/pkg/common"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os/exec"
|
||||
"testing"
|
||||
@@ -13,7 +14,7 @@ func TestExecCmd(t *testing.T) {
|
||||
//setup
|
||||
|
||||
//test
|
||||
result, err := common.ExecCmd("echo", []string{"hello world"}, "", nil)
|
||||
result, err := common.ExecCmd(logrus.WithField("exec", "test"), "echo", []string{"hello world"}, "", nil)
|
||||
|
||||
//assert
|
||||
require.NoError(t, err)
|
||||
@@ -26,7 +27,7 @@ func TestExecCmd_Date(t *testing.T) {
|
||||
//setup
|
||||
|
||||
//test
|
||||
_, err := common.ExecCmd("date", []string{}, "", nil)
|
||||
_, err := common.ExecCmd(logrus.WithField("exec", "test"), "date", []string{}, "", nil)
|
||||
|
||||
//assert
|
||||
require.NoError(t, err)
|
||||
@@ -56,7 +57,7 @@ func TestExecCmd_InvalidCommand(t *testing.T) {
|
||||
//setup
|
||||
|
||||
//test
|
||||
_, err := common.ExecCmd("invalid_binary", []string{}, "", nil)
|
||||
_, err := common.ExecCmd(logrus.WithField("exec", "test"), "invalid_binary", []string{}, "", nil)
|
||||
|
||||
//assert
|
||||
_, castOk := err.(*exec.ExitError)
|
||||
|
||||
Reference in New Issue
Block a user