-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefine.go
68 lines (52 loc) · 1.17 KB
/
define.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
package main
import (
"errors"
"log"
"net/http"
"os"
"path/filepath"
"github.com/hectron/go-define/lingua"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
)
const AppName = "go-define"
// this is injected using ldflags during compile time
// @see Makefile
var Version = ""
func main() {
app := &cli.App{
Name: AppName,
Version: Version,
Usage: "Find the definition of a word",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: filepath.Join(ConfigPath, "config.yml"),
Usage: "Load configuration from `FILE`",
},
},
Action: func(c *cli.Context) error {
word := c.Args().Get(0)
if len(word) == 0 {
return errors.New("Please provide a word to define")
}
LoadConfig(c)
apiKey := viper.GetString("LINGUA_ROBOT_API_KEY")
if apiKey == "" {
return errors.New("Please provide an API key")
}
linguaClient := lingua.Lingua{HttpClient: http.DefaultClient, ApiKey: apiKey}
summary, err := linguaClient.Define(word)
if err != nil {
return err
}
summary.Print(os.Stdout)
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}