-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (80 loc) · 2.23 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
package main
import (
"fmt"
"log"
"os"
"strconv"
)
const (
// Number of attempts to get a unique joke
jokeGetRetryCount = 5
urlJokesRandom = "https://api.chucknorris.io/jokes/random"
urlJokesCategories = "https://api.chucknorris.io/jokes/categories"
urlJokesRandomCategory = "https://api.chucknorris.io/jokes/random?category="
helpMessage = `Usage of ./joker:
random: Retrieve a random chuck joke
dump: Get 5 random unique jokes for each of the existing categories and saves them to text files - one for each of the categories
dump -n int: Get n random unique jokes for each of the existing categories and saves them to text files - one for each of the categories`
)
type JokesRandom struct {
Joke string `json:"value"`
}
type JokesCategories []string
func main() {
// flag.Parse() does not allow non-flag arguments, so use os.Args with switch
args := os.Args[1:]
if len(args) == 0 {
fmt.Println(randomJoke())
return
}
switch args[0] {
case "random":
fmt.Println(randomJoke())
case "dump":
switch argsLen := len(args); {
case argsLen == 1:
dumpJokesToFiles(5)
case argsLen == 3:
n, err := strconv.Atoi(args[2])
if err != nil {
fmt.Println("-n value must be an int value")
return
}
if n > 0 {
dumpJokesToFiles(n)
} else {
fmt.Println("-n value must be greater than 0")
}
default:
fmt.Println(helpMessage)
}
case "-h":
fallthrough
case "--help":
fallthrough
default:
fmt.Println(helpMessage)
}
}
// Get jokesCount random unique jokes for each of the existing categories
// and saves them to text files - one for each of the categories
func dumpJokesToFiles(jokesCount int) {
var categories JokesCategories
decodeJsonToTarget(urlJokesCategories, &categories)
err := writeMapToFile(jokesCategoriesToMapConcurrent(jokesCount, categories))
if err != nil {
log.Fatal("writeMapToFile error occurred")
}
}
// Return random joke with provided category
func randomJokeCategory(category string) string {
var jokeCategory JokesRandom
decodeJsonToTarget(urlJokesRandomCategory+category, &jokeCategory)
return jokeCategory.Joke
}
// Return random joke string
func randomJoke() string {
var chuck JokesRandom
decodeJsonToTarget(urlJokesRandom, &chuck)
return chuck.Joke
}