forked from purpleworks/fleet-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
79 lines (64 loc) · 1.41 KB
/
util.go
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
package main
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
// GetMachineIP parses the unitMachine in format "uuid/ip" and returns only the IP part.
// Can be used with the {UnitStatus.Machine} field.
// Returns an empty string, if no ip was found.
func GetMachineIP(unitMachine string) string {
fields := strings.Split(unitMachine, "/")
if len(fields) < 2 {
return ""
}
return fields[1]
}
func execCmd(cmd *exec.Cmd) (string, error) {
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if result := stdout.String(); result != "" {
return result, nil
}
if err != nil {
return "", err
}
if err := stderr.String(); err != "" {
return "", fmt.Errorf(err)
}
return "", nil
}
// filterEmpty returns an array containing all non-empty strings of the input array.
// Non-empty as in `strings.TrimSpace(v) != ""`.
func filterEmpty(values []string) []string {
result := make([]string, 0)
for _, v := range values {
if strings.TrimSpace(v) != "" {
result = append(result, v)
}
}
return result
}
// error handling
const (
ERROR_TYPE_NOT_FOUND = 10000 + iota
)
type FleetClientError struct {
StatusCode int
StatusText string
}
func (this FleetClientError) Error() string {
return this.StatusText
}
func NewFleetClientError(code int, text string) FleetClientError {
return FleetClientError{
StatusCode: code,
StatusText: text,
}
}