-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (61 loc) · 1.77 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
package main
import (
"embed"
"fmt"
"log"
"math/rand"
"strings"
"time"
)
//go:embed cookies/*
var cookies embed.FS
func main() {
rand.New(rand.NewSource(time.Now().UnixNano()))
dir, err := cookies.ReadDir("cookies")
if err != nil {
log.Fatal(err)
}
var fortunes []string
// WARN: not scalable
for _, d := range dir {
if d.Type().IsDir() {
sub, err := cookies.ReadDir("cookies/" + d.Name())
if err != nil {
log.Fatal(err)
}
for _, s := range sub {
data, err := cookies.ReadFile("cookies/" + d.Name() + "/" + s.Name())
if err != nil {
log.Fatal(err)
}
content := string(data)
phrases := strings.Split(content, "%")
for _, phrase := range phrases {
trimmedPhrase := strings.TrimSpace(phrase)
if trimmedPhrase != "" {
fortunes = append(fortunes, trimmedPhrase)
}
}
}
} else {
data, err := cookies.ReadFile("cookies/" + d.Name())
if err != nil {
log.Fatal(err)
}
content := string(data)
phrases := strings.Split(content, "%")
for _, phrase := range phrases {
trimmedPhrase := strings.TrimSpace(phrase)
if trimmedPhrase != "" {
fortunes = append(fortunes, trimmedPhrase)
}
}
}
}
if len(fortunes) > 0 {
randomIndex := rand.Intn(len(fortunes))
fmt.Println(fortunes[randomIndex])
} else {
fmt.Println("No fortunes found.")
}
}