-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
K8s Test Runner - Update scripts (#865)
- Loading branch information
Showing
4 changed files
with
102 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package exec | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Cmd executes os command, logging both streams | ||
func Cmd(command string) error { | ||
return CmdWithStreamFunc(command, func(m string) { | ||
log.Info().Str("Text", m).Msg("Command output") | ||
}) | ||
} | ||
|
||
// readStdPipe continuously read a pipe from the command | ||
func readStdPipe(pipe io.ReadCloser, streamFunc func(string)) { | ||
scanner := bufio.NewScanner(pipe) | ||
scanner.Split(bufio.ScanLines) | ||
for scanner.Scan() { | ||
m := scanner.Text() | ||
if streamFunc != nil { | ||
streamFunc(m) | ||
} | ||
} | ||
} | ||
|
||
// CmdWithStreamFunc executes command with stream function | ||
func CmdWithStreamFunc(command string, outputFunction func(string)) error { | ||
c := strings.Split(command, " ") | ||
cmd := exec.Command(c[0], c[1:]...) | ||
stderr, err := cmd.StderrPipe() | ||
if err != nil { | ||
return err | ||
} | ||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return err | ||
} | ||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
go readStdPipe(stderr, outputFunction) | ||
go readStdPipe(stdout, outputFunction) | ||
return cmd.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters