-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (90 loc) · 2.02 KB
/
main.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
func lineCounter(r io.Reader) (int, error) {
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, err
}
}
}
func readLine(r io.Reader, lineNum int) (line string, lastLine int, err error) {
sc := bufio.NewScanner(r)
for sc.Scan() {
lastLine++
if lastLine == lineNum {
// you can return sc.Bytes() if you need output in []bytes
return sc.Text(), lastLine, sc.Err()
}
}
return line, lastLine, io.EOF
}
func getRandomWord(dictionary string) string {
randomWord := ""
var line int
dictFile := fmt.Sprintf("%s.txt", dictionary)
file, err := os.Open(dictFile)
if err != nil {
log.Fatal(err)
} else {
numWords, countError := lineCounter(file)
if countError != nil {
fmt.Println("Cannot get line count")
} else {
rand.Seed(time.Now().UnixNano())
line = rand.Intn(numWords)
rs, resetErr := file.Seek(0, 0)
if resetErr != nil {
log.Fatal(resetErr)
}
if rs >= 0 {
word, last, dictErr := readLine(file, line)
if dictErr != nil {
fmt.Printf("Cannot read line %d\n", last)
}
randomWord = word
}
}
}
return randomWord
}
func generateRandomTrailingNumber(size int) string {
rand.Seed(time.Now().UnixNano())
upper := strings.Repeat("9", size)
upperBound, err := strconv.Atoi(upper)
if err != nil {
upperBound = 9999
}
randomNumber := rand.Intn(upperBound)
trailing := fmt.Sprintf("%0*d", size, randomNumber)
return trailing
}
func getSubdomainName() string {
adjective := getRandomWord("adjectives")
noun := getRandomWord("nouns")
trailing := generateRandomTrailingNumber(4) // Defaulting to 4-digit random suffix for now
subdomain := fmt.Sprintf("%s-%s-%s\n", adjective, noun, trailing)
return subdomain
}
func main() {
subdomain := getSubdomainName()
fmt.Println(subdomain)
}