Files

122 lines
2.9 KiB
Go
Raw Permalink Normal View History

2017-06-26 21:52:06 -07:00
/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2017-04-28 08:02:37 -07:00
package utils
import (
"bufio"
"bytes"
"fmt"
"os/exec"
"strings"
2017-06-26 21:52:06 -07:00
2017-07-20 23:30:32 -07:00
"github.com/coreos/pkg/capnslog"
2020-03-17 18:23:16 -06:00
utilexec "github.com/rook/rook/pkg/util/exec"
2017-04-28 08:02:37 -07:00
)
2017-07-20 23:30:32 -07:00
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "testutil")
2022-06-21 19:59:31 -04:00
// CommandArgs is a wrapper for cmd args
2017-07-21 12:07:15 -07:00
type CommandArgs struct {
Command string
CmdArgs []string
PipeToStdIn string
EnvironmentVariable []string
}
// CommandOut is a wrapper for cmd out returned after executing command args
2017-07-21 12:07:15 -07:00
type CommandOut struct {
StdOut string
StdErr string
ExitCode int
Err error
}
// ExecuteCommand executes an OS command with stdin and returns output
2017-07-21 12:07:15 -07:00
func ExecuteCommand(cmdStruct CommandArgs) CommandOut {
2017-07-20 23:30:32 -07:00
logger.Infof("Running %s %v", cmdStruct.Command, cmdStruct.CmdArgs)
2017-04-28 08:02:37 -07:00
var outBuffer, errBuffer bytes.Buffer
2020-10-05 13:27:44 +05:30
cmd := exec.Command(cmdStruct.Command, cmdStruct.CmdArgs...) //nolint:gosec // We safely suppress gosec in tests file
2017-04-28 08:02:37 -07:00
cmd.Env = append(cmd.Env, cmdStruct.EnvironmentVariable...)
stdOut, err := cmd.StdoutPipe()
if err != nil {
2017-07-21 12:07:15 -07:00
return CommandOut{StdErr: errBuffer.String(), StdOut: outBuffer.String(), Err: err}
2017-04-28 08:02:37 -07:00
}
stdin, err := cmd.StdinPipe()
if err != nil {
2017-07-21 12:07:15 -07:00
return CommandOut{Err: err}
2017-04-28 08:02:37 -07:00
}
defer stdOut.Close()
scanner := bufio.NewScanner(stdOut)
go func() {
for scanner.Scan() {
outBuffer.WriteString(scanner.Text())
fmt.Printf("%s\n", scanner.Text())
}
}()
stdErr, err := cmd.StderrPipe()
if err != nil {
2017-07-21 12:07:15 -07:00
return CommandOut{StdErr: errBuffer.String(), StdOut: outBuffer.String(), Err: err}
2017-04-28 08:02:37 -07:00
}
defer stdErr.Close()
stdErrScanner := bufio.NewScanner(stdErr)
go func() {
for stdErrScanner.Scan() {
txt := stdErrScanner.Text()
if !strings.Contains(txt, "no buildable Go source files in") {
errBuffer.WriteString(txt)
fmt.Printf("%s\n", txt)
}
}
}()
err = cmd.Start()
if err != nil {
2017-07-21 12:07:15 -07:00
return CommandOut{StdErr: errBuffer.String(), StdOut: outBuffer.String(), Err: err}
2017-04-28 08:02:37 -07:00
}
if cmdStruct.PipeToStdIn != "" {
_, err = stdin.Write([]byte(cmdStruct.PipeToStdIn))
if err != nil {
return CommandOut{StdErr: errBuffer.String(), StdOut: outBuffer.String(), Err: err}
}
2017-04-28 08:02:37 -07:00
stdin.Close()
}
2020-03-17 18:23:16 -06:00
err = cmd.Wait()
out := CommandOut{StdErr: errBuffer.String(), StdOut: outBuffer.String()}
if err != nil {
out.Err = err
if code, ok := utilexec.ExitStatus(err); ok {
out.ExitCode = code
2017-04-28 08:02:37 -07:00
}
}
2020-03-17 18:23:16 -06:00
return out
2017-04-28 08:02:37 -07:00
}