-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
56 lines (44 loc) · 1.1 KB
/
link.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
package main
import (
"math/rand"
"strings"
"time"
)
const (
linkChars = "abcdefghijklmnopqrstuvwxyz0123456789_-"
lenLinkChars = len(linkChars)
minLinkSegment = 5
maxLinkSegment = 20
)
var absRelLinksRatio = [2]int{10, 2}
func init() {
rand.Seed(time.Now().UnixNano())
}
func GetLinks(flags Flags) []string {
links := make([]string, random(flags.MinLinks, flags.MaxLinks))
for i := range links {
links[i] = getLink(flags)
}
return links
}
func getLink(flags Flags) string {
segments := make([]string, random(flags.MinLinkDepth, flags.MaxLinkDepth))
for i := range segments {
segments[i] = getLinkSegment(minLinkSegment, maxLinkSegment)
}
// generate some relative and absolute links
if rand.Intn(absRelLinksRatio[0]) > absRelLinksRatio[1] {
return "/" + strings.Join(segments, "/")
}
return strings.Join(segments, "/")
}
func getLinkSegment(minLength, maxLength int) string {
b := make([]byte, random(minLength, maxLength))
for i := range b {
b[i] = linkChars[random(0, lenLinkChars)]
}
return string(b)
}
func random(min, max int) int {
return rand.Intn(max-min) + min
}