forked from xyproto/wallutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
83 lines (72 loc) · 1.45 KB
/
utils.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
80
81
82
83
package monitor
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// has checks if a string slice has the given element
func has(sl []string, e string) bool {
for _, s := range sl {
if s == e {
return true
}
}
return false
}
// unique removes all repeated elements from a slice of strings
func unique(sl []string) []string {
var nl []string
for _, s := range sl {
if !has(nl, s) {
nl = append(nl, s)
}
}
return nl
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
// firstname finds the part of a filename before the extension
func firstname(filename string) string {
ext := filepath.Ext(filename)
return filename[:len(filename)-len(ext)]
}
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func which(executable string) string {
p, err := exec.LookPath(executable)
if err != nil {
return ""
}
return p
}
func run(shellCommand string) error {
fmt.Println(shellCommand)
cmd := exec.Command("sh", "-c", shellCommand)
if _, err := cmd.CombinedOutput(); err != nil {
return err
}
return nil
}
func output(shellCommand string) string {
fmt.Println(shellCommand)
cmd := exec.Command("sh", "-c", shellCommand)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
return ""
}
return string(stdoutStderr)
}
func containsE(envVar, subString string) bool {
return strings.Contains(os.Getenv(envVar), subString)
}
func hasE(envVar string) bool {
return os.Getenv(envVar) != ""
}