-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.go
More file actions
175 lines (153 loc) · 3.75 KB
/
utils.go
File metadata and controls
175 lines (153 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package utils
import (
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
"syscall"
"testing"
"github.com/satori/go.uuid"
)
// NewUuid returns a new V4-style unique identifier.
func NewUuid() string {
u1 := uuid.NewV4()
s1 := fmt.Sprintf("%s", u1)
return strings.Split(s1, "-")[0]
}
// GetHostOs returns either "darwin" or "ubuntu".
func GetHostOs() string {
cmd := exec.Command("uname")
out, _ := cmd.Output()
if strings.Contains(string(out), "Darwin") {
return "darwin"
}
return "ubuntu"
}
func GetHostIPAddress() string {
IP := os.Getenv("HOST_IPADDR")
if IP == "" {
IP = "172.17.8.100"
}
return IP
}
func Append(slice []string, data string) []string {
m := len(slice)
n := m + 1
if n > cap(slice) { // if necessary, reallocate
// allocate double what's needed, for future growth.
newSlice := make([]string, (n + 1))
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:n]
slice[n-1] = data
return slice
}
func GetRandomPort() string {
l, _ := net.Listen("tcp", "127.0.0.1:0") // listen on localhost
port := l.Addr()
return strings.Split(port.String(), ":")[1]
}
func getExitCode(err error) (int, error) {
exitCode := 0
if exiterr, ok := err.(*exec.ExitError); ok {
if procExit := exiterr.Sys().(syscall.WaitStatus); ok {
return procExit.ExitStatus(), nil
}
}
return exitCode, fmt.Errorf("failed to get exit code")
}
func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) {
exitCode = 0
out, err := cmd.CombinedOutput()
if err != nil {
var exiterr error
if exitCode, exiterr = getExitCode(err); exiterr != nil {
// TODO: Fix this so we check the error's text.
// we've failed to retrieve exit code, so we set it to 127
exitCode = 127
}
}
output = string(out)
return
}
func runCommandWithStdoutStderr(cmd *exec.Cmd) (exitCode int, err error) {
exitCode = 0
// var stderrBuffer, stdoutBuffer bytes.Buffer
stderrPipe, err := cmd.StderrPipe()
stdoutpipe, err := cmd.StdoutPipe()
if err != nil {
return -1, err
}
err = cmd.Start()
if err != nil {
var exiterr error
if exitCode, exiterr = getExitCode(err); exiterr != nil {
// TODO: Fix this so we check the error's text.
// we've failed to retrieve exit code, so we set it to 127
exitCode = 127
}
}
go io.Copy(os.Stdout, stdoutpipe)
go io.Copy(os.Stderr, stderrPipe)
err = cmd.Wait()
if err != nil {
var exiterr error
if exitCode, exiterr = getExitCode(err); exiterr != nil {
// TODO: Fix this so we check the error's text.
// we've failed to retrieve exit code, so we set it to 127
exitCode = 127
}
}
return
}
func runCommand(cmd *exec.Cmd) (exitCode int, err error) {
exitCode = 0
err = cmd.Run()
if err != nil {
var exiterr error
if exitCode, exiterr = getExitCode(err); exiterr != nil {
// TODO: Fix this so we check the error's text.
// we've failed to retrieve exit code, so we set it to 127
exitCode = 127
}
}
return
}
func startCommand(cmd *exec.Cmd) (exitCode int, err error) {
exitCode = 0
err = cmd.Start()
if err != nil {
var exiterr error
if exitCode, exiterr = getExitCode(err); exiterr != nil {
// TODO: Fix this so we check the error's text.
// we've failed to retrieve exit code, so we set it to 127
exitCode = 127
}
}
return
}
func logDone(message string) {
fmt.Printf("[PASSED]: %s\n", message)
}
func stripTrailingCharacters(target string) string {
target = strings.Trim(target, "\n")
target = strings.Trim(target, " ")
return target
}
func errorOut(err error, t *testing.T, message string) {
if err != nil {
t.Fatal(message)
}
}
func errorOutOnNonNilError(err error, t *testing.T, message string) {
if err == nil {
t.Fatalf(message)
}
}
func nLines(s string) int {
return strings.Count(s, "\n")
}
//func deis(bash string , arg string , cmd string )