/
main.go
122 lines (92 loc) · 2.56 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"flag"
"fmt"
"log"
"os"
"sync"
// "time"
)
// TODO(#11): flags to specify display type. json, text,?
func displayDef(definition []string, traverses int, depth int) {
defer recovery("invalid")
if traverses == (len(definition)-1) || traverses == (depth-1) {
fmt.Printf("%d - %v\n", (traverses + 1), definition[traverses])
return
}
fmt.Printf("%d - %v\n", (traverses + 1), definition[traverses])
displayDef(definition, traverses+1, depth)
}
func parseChan(tmpDef chan []string, size int) []string {
if len(tmpDef) > 0 {
tmpDict := make([]string, 0)
for i := 0; i < size; i++ {
tmp := <-tmpDef
for j := 0; j < len(tmp); j++ {
tmpDict = append(tmpDict, tmp[j])
}
}
return tmpDict
}
return []string(nil)
}
func checkWeb(word string, apiConf *config, tmpDef chan<- []string) {
var wg sync.WaitGroup
for idx := range apiConf.Website {
wg.Add(1)
website, link, apiKey := parseConfig(apiConf, idx)
requestLink, err := parseRequest(word, website, link, apiKey)
if err != nil {
log.Fatalln(err)
} else {
go func() {
tmpDef <- callAPI(website, requestLink)
wg.Done()
}()
}
}
wg.Wait()
}
func locateDef(word string, apiConf *config, dictFile string, dictionary map[string][]string) []string {
definition, found := checkDict(word, dictionary)
if !found {
tmpDef := make(chan []string, len(apiConf.Website))
checkWeb(word, apiConf, tmpDef)
definition = parseChan(tmpDef, len(apiConf.Website))
return definition
}
return definition
}
func parseFlags() (bool, int, []string) {
verbose := flag.Bool("verbose", false, "Display word with definitions")
depth := flag.Int("depth", 0, "Amount of definitions displayed")
flag.Parse()
return *verbose, *depth, flag.Args()
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("invalid number of arguments\n")
return
}
apiConf, defPath := getConfig()
dictFile := getDictConf(apiConf)
// TODO(#23): Should we update the dictionary to reflect definitions from multiple sources
// this may lead to over the top defintions, or repeats.
dictionary := getDict(defPath + "/" + dictFile)
verbose, depth, words := parseFlags()
for index := 0; index < len(words); index++ {
word := words[index]
definition := locateDef(words[index], apiConf, dictFile, dictionary)
failed := verifyDef(definition)
if failed {
fmt.Printf("\"%v\" - Not found \n", word)
continue
}
updateDict(dictionary, word, definition)
storeJSON(defPath+"/"+dictFile, dictionary)
if verbose {
fmt.Printf("\n%v:\n", word)
}
displayDef(definition, 0, depth)
}
}