-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (69 loc) · 1.46 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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"time"
"github.com/timwmillard/slang/urbandictionary"
)
func main() {
flag.Usage = func() {
fmt.Println("Usage:")
fmt.Printf("%s [OPTIONS] <word>\n", os.Args[0])
fmt.Println()
fmt.Println("Options:")
flag.PrintDefaults()
}
all := flag.Bool("all", false, "display all definitions")
auth := flag.Bool("auth", false, "configure API Key")
example := flag.Bool("example", false, "show example")
flag.Parse()
if *auth {
promptAuth()
}
if len(flag.Args()) < 1 {
flag.Usage()
os.Exit(1)
}
apiKey, err := readAuth()
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading config:", err)
os.Exit(1)
}
if apiKey == "" {
promptAuth()
}
word := flag.Arg(0)
urbanDict := urbandictionary.NewClient(apiKey)
definitions, err := urbanDict.Define(word)
if err != nil {
fmt.Fprintln(os.Stderr, "Urban Dictionary error:", err)
os.Exit(1)
}
if len(definitions) == 0 {
fmt.Println("No definitions")
os.Exit(1)
}
if *all {
printAll(definitions)
} else {
printRand(definitions, *example)
}
}
func printRand(defs []urbandictionary.Definition, showExample bool) {
rand.Seed(time.Now().UnixNano())
index := rand.Intn(len(defs))
fmt.Println(defs[index].Definition)
if showExample {
fmt.Println()
fmt.Println("Example:")
fmt.Println(defs[index].Example)
}
}
func printAll(defs []urbandictionary.Definition) {
for _, def := range defs {
fmt.Println(def.Definition)
fmt.Println()
}
}