-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
65 lines (51 loc) · 1.11 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
package main
import (
"io/ioutil"
"math"
"math/rand"
"os"
"strings"
"time"
)
// getFiles returns an array of files names found in a directory.
// To get all files leave the ext blank.
func getFiles(dirPath string, ext string) ([]string, error) {
var fileNames []string
files, err := ioutil.ReadDir(dirPath)
if err != nil {
return fileNames, err
}
for i := 0; i < len(files); i++ {
fn := files[i].Name()
if files[i].IsDir() {
continue
}
if ext != "" {
v := strings.Split(fn, ".")
if ext[1:] != v[len(v)-1] {
continue
}
}
fileNames = append(fileNames, fn)
}
return fileNames, nil
}
// fileOrDirectoryExists checks existance of file or directory.
func fileOrDirectoryExists(path string) bool {
if path == "" {
return false
}
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return true
}
// randInt gets a random int between two numbers.
func randInt(min int, max int) int {
rand.Seed(time.Now().UTC().UnixNano())
x := max - min
// In case the max is less than min, take the absolute value.
x = int(math.Abs(float64(x)))
return min + rand.Intn(x)
}