-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
84 lines (67 loc) · 1.66 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/genuinetools/pkg/cli"
"github.com/genuinetools/udict/api"
"github.com/genuinetools/udict/version"
"github.com/sirupsen/logrus"
)
var (
debug bool
)
func main() {
// Create a new cli program.
p := cli.NewProgram()
p.Name = "udict"
p.Description = "A command line urban dictionary"
// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
if p.FlagSet.NArg() < 1 {
return errors.New("pass a word or phrase")
}
return nil
}
// Set the main program action.
p.Action = func(ctx context.Context, args []string) error {
// On ^C, or SIGTERM handle exit.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
for sig := range c {
logrus.Infof("Received %s, exiting.", sig.String())
os.Exit(0)
}
}()
word := strings.Join(args, " ")
response, err := api.Define(word)
if err != nil {
return fmt.Errorf("decoding API response failed: %v", err)
}
defResponse := fmt.Sprintf("%d definitions returned\n", len(response.Results))
for _, def := range response.Results {
defResponse += fmt.Sprintf("\n%s\n--(%s) <%s>\n", def.Definition, def.Word, def.Link)
}
fmt.Println(defResponse)
return nil
}
// Run our program.
p.Run()
}