-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
125 lines (103 loc) · 2.12 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
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
package vcsinfo
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"sync"
"syscall"
)
func dirExists(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err = nil
}
return false, err
}
if fileInfo.IsDir() {
return true, nil
}
return false, nil
}
func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err = nil
}
return false, err
}
return true, nil
}
func commandExists(command string) bool {
exists, err := exec.LookPath(command)
return exists != "" && err == nil
}
func getExitCode(err error) int {
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus()
}
}
return -1
}
func runCommand(workingDir string, command ...string) ([]string, error) {
cmd := exec.Command(command[0], command[1:]...)
cmd.Dir = workingDir
out, err := cmd.CombinedOutput()
var lines []string
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if false {
// Leaving this in for the moment for easier debugging
if len(lines) == 0 {
fmt.Printf("[runCommand: %s]: <NO OUTPUT>\n", command)
} else {
for _, line := range lines {
fmt.Printf("[runCommand: %s]: %s\n", command, line)
}
}
if err != nil {
fmt.Printf("[runCommand: %s]! %s\n", command, err)
}
}
return lines, err
}
func waitGroup(routines ...func() error) []error {
waitGroup := sync.WaitGroup{}
waitGroup.Add(len(routines))
errors := []error{}
for idx := range routines {
routine := routines[idx]
go func() {
defer waitGroup.Done()
err := routine()
if err != nil {
errors = append(errors, err)
}
}()
}
waitGroup.Wait()
return errors
}
func findAcceptablePath(path string, isAcceptable func(string) (bool, error)) (string, error) {
for {
acceptable, err := isAcceptable(path)
if err != nil {
return "", err
}
if acceptable {
return path, nil
}
if path == "/" {
break
}
path = filepath.Join(path, "..")
}
return "", nil
}