-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
101 lines (87 loc) · 2.46 KB
/
util.go
File metadata and controls
101 lines (87 loc) · 2.46 KB
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
package main
import (
"encoding/binary"
"crypto/rand"
"io/ioutil"
"math"
"os"
"path/filepath"
"strings"
)
// Uses /dev/urandom to generate random numbers. We don't
// need to recreate generated numbers, so we don't save
// a RNG state.
func randUint() uint64 {
b := make([]byte, 8)
_, err := rand.Read(b)
for err != nil {
_, err = rand.Read(b)
}
return binary.BigEndian.Uint64(b)
}
func randFloat() float64 {
return float64(randUint()) / float64(math.MaxUint64)
}
// The range returned is inclusive.
func randRange(low, high uint) uint {
f := randFloat() * float64(high - low + 1)
return uint(math.Floor(f)) + low
}
// Returns true when under the specified chance.
func chance(f float64) bool {
return randFloat() < f
}
// Returns the directory listing as full path names. The passed path
// must be absolute.
func listDirs(path string) []string {
if !filepath.IsAbs(path) {
panic("cannot list dirs on non-absolute path")
}
dirs := []string{}
files, _ := ioutil.ReadDir(path)
for _, file := range files {
isPrivate := strings.HasPrefix(file.Name(), ".")
if file.IsDir() && !isPrivate {
dirs = append(dirs, filepath.Join(path, file.Name()))
}
}
return dirs
}
// Returns true if you can descend from this path, descending is going
// down a directory, as opposed to up (`cd ..` is up). The passed path
// must be absolute
func canDescend(path string) bool {
dirs := listDirs(path)
return len(dirs) > 0
}
// Returns a random path to desend. The passed path must be absolute.
func randDescension(path string) string {
dirs := listDirs(path)
if len(dirs) == 0 {
panic("Tried to descend when unable")
}
return dirs[randRange(0, uint(len(dirs) - 1))]
}
// Returns true if you can ascend from this path. No ascending
// below the home directory. The passed path must be absolute.
func canAscend(path string) bool {
home := os.Getenv("HOME")
return strings.HasPrefix(filepath.Dir(path), home)
}
// No need to be random. You can only ascend in one direction.
func ascend(path string) string {
return filepath.Dir(path)
}
// This is the furthest we can ascend.
func baseLocation() string {
home := os.Getenv("HOME")
return home
}
// Returns true if the path provided is an ascended location from.
func isAscension(to string, from string) bool {
return strings.HasPrefix(filepath.Dir(from), to)
}
// Returns true if the path provided is a descended location from.
func isDescension(to string, from string) bool {
return strings.HasPrefix(filepath.Dir(to), from)
}