11package utils
22
33import (
4+ "bufio"
45 "fmt"
6+ "github.com/satori/go.uuid"
57 "io"
68 "net"
79 "os"
810 "os/exec"
911 "strings"
1012 "syscall"
1113 "testing"
12-
13- "github.com/satori/go.uuid"
14+ "time"
1415)
1516
1617// NewUuid returns a new V4-style unique identifier.
@@ -20,6 +21,21 @@ func NewUuid() string {
2021 return strings .Split (s1 , "-" )[0 ]
2122}
2223
24+ func GetFileBytes (filename string ) []byte {
25+ file , _ := os .Open (filename )
26+ defer file .Close ()
27+ stat , _ := file .Stat ()
28+ bs := make ([]byte , stat .Size ())
29+ _ , _ = file .Read (bs )
30+ return bs
31+ }
32+
33+ func GetUserDetails () (string , string ) {
34+ u1 := uuid .NewV4 ()
35+ s1 := fmt .Sprintf ("%s" , u1 )
36+ return strings .Split (s1 , "-" )[0 ], strings .Split (s1 , "-" )[1 ]
37+ }
38+
2339// GetHostOs returns either "darwin" or "ubuntu".
2440func GetHostOs () string {
2541 cmd := exec .Command ("uname" )
@@ -69,45 +85,40 @@ func getExitCode(err error) (int, error) {
6985 return exitCode , fmt .Errorf ("failed to get exit code" )
7086}
7187
72- func runCommandWithOutput (cmd * exec.Cmd ) (output string , exitCode int , err error ) {
73- exitCode = 0
74- out , err := cmd .CombinedOutput ()
75- if err != nil {
76- var exiterr error
77- if exitCode , exiterr = getExitCode (err ); exiterr != nil {
78- // TODO: Fix this so we check the error's text.
79- // we've failed to retrieve exit code, so we set it to 127
80- exitCode = 127
81- }
82- }
83- output = string (out )
84- return
85- }
86-
87- func runCommandWithStdoutStderr (cmd * exec.Cmd ) (exitCode int , err error ) {
88- exitCode = 0
89- // var stderrBuffer, stdoutBuffer bytes.Buffer
88+ func RunCommandWithStdoutStderr (cmd * exec.Cmd ) (stdout , stderr bytes.Buffer , err error ) {
89+ var stdout , stderr bytes.Buffer
9090 stderrPipe , err := cmd .StderrPipe ()
9191 stdoutpipe , err := cmd .StdoutPipe ()
9292
93+ cmd .Env = os .Environ ()
9394 if err != nil {
94- return - 1 , err
95+ return
9596 }
9697
9798 err = cmd .Start ()
9899 if err != nil {
99- var exiterr error
100- if exitCode , exiterr = getExitCode (err ); exiterr != nil {
101- // TODO: Fix this so we check the error's text.
102- // we've failed to retrieve exit code, so we set it to 127
103- exitCode = 127
104- }
100+ return
105101 }
106102
107- go io .Copy (os .Stdout , stdoutpipe )
108- go io .Copy (os .Stderr , stderrPipe )
109-
103+ go func () {
104+ io .Copy (& stdout , stdoutPipe )
105+ io .Copy (os .Stdout , stdoutPipe )
106+ }()
107+ go func () {
108+ io .Copy (& stderr , stderrPipe )
109+ io .Copy (os .Stderr , stderrPipe )
110+ }()
111+ time .Sleep (2000 * time .Millisecond )
110112 err = cmd .Wait ()
113+ if err != nil {
114+ return
115+ }
116+ return
117+ }
118+
119+ func runCommandWithOutput (cmd * exec.Cmd ) (output string , exitCode int , err error ) {
120+ exitCode = 0
121+ out , err := cmd .CombinedOutput ()
111122 if err != nil {
112123 var exiterr error
113124 if exitCode , exiterr = getExitCode (err ); exiterr != nil {
@@ -116,6 +127,7 @@ func runCommandWithStdoutStderr(cmd *exec.Cmd) (exitCode int, err error) {
116127 exitCode = 127
117128 }
118129 }
130+ output = string (out )
119131 return
120132}
121133
0 commit comments