-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (85 loc) · 1.92 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
package main
import (
"embed"
"fmt"
"math/rand"
"os"
"strings"
)
//go:embed assets/fortunes
var f_fortunes embed.FS
//go:embed assets/literature
var f_lit embed.FS
//go:embed assets/riddles
var f_riddles embed.FS
func err_handler(e error) {
if e != nil {
panic(e)
}
}
func random_intn(min, max int) int {
return min + rand.Intn(max-min)
}
func random_fortuner_allfortunes() []byte {
files := [3]string{"fortunes", "literature", "riddles"}
random_num := random_intn(0, 3)
choice := files[random_num]
fname := "assets/" + choice
if fname == "assets/fortunes" {
data, err := f_fortunes.ReadFile(fname)
err_handler(err)
return data
} else if fname == "assets/literature" {
data, err := f_lit.ReadFile(fname)
err_handler(err)
return data
} else {
data, err := f_riddles.ReadFile(fname)
err_handler(err)
return data
}
}
func random_fortuner_literature() []byte {
data, err := f_lit.ReadFile("assets/literature")
err_handler(err)
return data
}
func random_fortuner_riddles() []byte {
data, err := f_riddles.ReadFile("assets/riddles")
err_handler(err)
return data
}
func random_fortuner_fortunes() []byte {
data, err := f_fortunes.ReadFile("assets/fortunes")
err_handler(err)
return data
}
func random_fortune_printer(f []byte) {
b_string := string(f)
fortuner_array := strings.Split(b_string, "%")
random_fortune := random_intn(1, len(fortuner_array))
fmt.Println(fortuner_array[random_fortune])
}
func main() {
cli_args := os.Args
// fmt.Println(len(cli_args))
if len(cli_args) == 1 {
f := random_fortuner_allfortunes()
random_fortune_printer(f)
} else {
switch cli_args[1] {
case "-f":
f := random_fortuner_fortunes()
random_fortune_printer(f)
case "-l":
f := random_fortuner_literature()
random_fortune_printer(f)
case "-r":
f := random_fortuner_riddles()
random_fortune_printer(f)
default:
fmt.Println("incorrect usage, try :")
fmt.Println("fortune [-f/-r/-l]")
}
}
}