-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
51 lines (46 loc) · 1.02 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
package fixtures
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"time"
"github.com/cenkalti/backoff/v3"
)
func GenerateString() string {
rand.Seed(time.Now().UTC().UnixNano())
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, 10) // Make some space
for i := 0; i < 10; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}
func FindPath(containing string) string {
workingDirectory, err := os.Getwd()
if err != nil {
panic(err)
}
lastDir := workingDirectory
for {
currentPath := fmt.Sprintf("%s/%s", lastDir, containing)
fi, err := os.Stat(currentPath)
if err == nil {
switch mode := fi.Mode(); {
case mode.IsDir():
return currentPath
}
}
newDir := filepath.Dir(lastDir)
if newDir == "/" || newDir == lastDir {
return ""
}
lastDir = newDir
}
}
func Retry(d time.Duration, op func() error) error {
bo := backoff.NewExponentialBackOff()
bo.MaxInterval = time.Second
bo.MaxElapsedTime = time.Duration(d)
return backoff.Retry(op, bo)
}