This repository has been archived by the owner on Sep 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (139 loc) · 3.28 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"encoding/gob"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/allegro/bigcache"
"github.com/spf13/viper"
"github.com/urfave/cli"
)
var (
Version = "unknown"
BaseURL = "https://od-api.oxforddictionaries.com:443/api/v2"
Client *http.Client
flagRaw = false
flagCategory string
flagCache = false
flagSimple = false
flagHTML = false
)
func loadConfig() {
viper.SetConfigName(".dictrc")
viper.AddConfigPath("$HOME")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
}
func loadCache(cacheDir string, cache *bigcache.BigCache) (*bigcache.BigCache, error) {
file, err := os.Open(cacheDir + "cache.bin")
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(cache)
}
file.Close()
return nil, err
}
func saveCache(cacheDir string, cache *bigcache.BigCache) error {
file, err := os.Create(cacheDir + "cache.bin")
if err == nil {
encoder := gob.NewEncoder(file)
encoder.Encode(cache)
}
file.Close()
return err
}
func setupRequestCommand(c *cli.Context) error {
// load the Viper config
loadConfig()
// override the default http.Client timeouts
Client = &http.Client{
Timeout: time.Second * 10,
}
BaseURL = viper.GetString("api_url")
if flagCategory != "" {
flagCategory = fmt.Sprintf(`#[lexicalCategory=="%s"]`, strings.Title(flagCategory))
} else {
flagCategory = "0"
}
return nil
}
func main() {
dict := cli.NewApp()
dict.Name = "dict"
dict.Usage = "Oxford Dictionary CLI tool"
dict.Version = Version
apiRequestFlags := &[]cli.Flag{
cli.StringFlag{
Name: "category, c",
Usage: `(unimplemented) request info for a specific lexical category`,
Destination: &flagCategory,
},
cli.BoolFlag{
Name: "html",
Usage: "wrap output in an HTML document with Blackfriday",
Destination: &flagHTML,
},
// cli.BoolFlag{
// Name: "no-cache, f",
// Usage: "force an API request, updating the cache",
// Destination: &flagCache,
// },
cli.BoolFlag{
Name: "raw, r",
Usage: "return raw JSON from the request",
Destination: &flagRaw,
},
cli.BoolFlag{
Name: "simple, s",
Usage: "output only the values, separated by newlines (excludes subsenses)",
Destination: &flagSimple,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "(unimplemented) increase definition verbosity",
},
}
dict.Commands = []cli.Command{
cli.Command{
Name: "antonyms",
Aliases: []string{"a"},
Usage: "look up antonyms",
Flags: *apiRequestFlags,
Before: setupRequestCommand,
Action: func(c *cli.Context) error {
return AntonymCommand(c.Args().Get(0))
},
},
cli.Command{
Name: "define",
Aliases: []string{"d"},
Usage: "define a word",
Flags: *apiRequestFlags,
Before: setupRequestCommand,
Action: func(c *cli.Context) error {
return DefineCommand(c.Args().Get(0))
},
},
cli.Command{
Name: "synonyms",
Aliases: []string{"s"},
Usage: "look up synonyms",
Flags: *apiRequestFlags,
Before: setupRequestCommand,
Action: func(c *cli.Context) error {
return SynonymCommand(c.Args().Get(0))
},
},
}
err := dict.Run(os.Args)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
}