-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (44 loc) · 1.27 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
def "github.com/GeistInDerSH/define-term/definition"
)
var url = "https://api.dictionaryapi.dev/api/v2/entries/%s/%s"
var language = flag.String("l", "en", "")
var showSynonyms = flag.Bool("s", false, "")
// getJSONBytes gets the bytes that will be converted into the json data
func getJSONBytes(word string) []byte {
def := fmt.Sprintf(url, *language, word)
resonse, _ := http.Get(def)
bytes, _ := ioutil.ReadAll(resonse.Body)
return bytes
}
func main() {
flag.Usage = func() {
fmt.Println("Usage of def: def [-l language_code] word")
fmt.Println("Command Line Flags:")
fmt.Println(" -l language_code")
fmt.Printf("\tar\tArabic\n\tde\tGerman\n\ten\tEnglish\n\tes\tSpanish\n\tfr\tFrench\n\thi\tHindi\n\tit\tItalian\n\tja\tJapanese\n\tko\tKorean\n\tpt-BR\tBrazilian Portuguese\n\tru\tRussian\n\ttr\tTurkish\n\tzh-CN\tChinese (Simplified)\n")
}
flag.Parse()
args := flag.CommandLine.Args()
if len(args) < 1 {
flag.Usage()
os.Exit(-1)
}
data := getJSONBytes(args[0])
var defWord []def.DefWord
json.Unmarshal(data, &defWord)
if defWord == nil {
fmt.Printf("No defintion found for: %s\n", args[0])
os.Exit(0)
}
for _, term := range defWord {
term.Print(*showSynonyms)
}
}