-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.go
86 lines (76 loc) · 1.79 KB
/
setup.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
package cli
import (
"fmt"
"github.com/alecthomas/kong"
"github.com/hearchco/hearchco/src/gotypelimits"
"github.com/hearchco/hearchco/src/search/category"
"github.com/hearchco/hearchco/src/search/engines"
"github.com/rs/zerolog/log"
)
func Setup() Flags {
var cli Flags
ctx := kong.Parse(&cli,
kong.Name("hearchco"),
kong.Description("Fastasst metasearch engine"),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Summary: true,
Compact: true,
}),
kong.Vars{
"version": fmt.Sprintf("%v (%v@%v)", Version, GitCommit, Timestamp),
"data_folder": ".",
"query_string": "banana death",
},
)
if err := ctx.Validate(); err != nil {
log.Panic().
Caller().
Err(err).
Msg("Failed parsing cli")
// ^PANIC
}
if cli.Query == "" {
log.Fatal().
Caller().
Msg("Query cannot be empty or whitespace")
// ^FATAL
}
// TODO: make upper limit configurable
pagesMaxUpperLimit := 10
if cli.PagesMax < 1 || cli.PagesMax > pagesMaxUpperLimit {
log.Fatal().
Caller().
Int("pages", cli.PagesMax).
Int("min", 1).
Int("max", pagesMaxUpperLimit).
Msg("Pages value out of range")
// ^FATAL
}
if cli.PagesStart < 1 || cli.PagesStart > gotypelimits.MaxInt-pagesMaxUpperLimit {
log.Fatal().
Caller().
Int("start", cli.PagesStart).
Int("min", 1).
Int("max", gotypelimits.MaxInt-pagesMaxUpperLimit).
Msg("Start value out of range")
// ^FATAL
} else {
// since it's >=1, we decrement it to match the 0-based index
cli.PagesStart -= 1
}
if err := engines.ValidateLocale(cli.Locale); err != nil {
log.Fatal().
Caller().
Err(err).
Msg("Invalid locale flag")
// ^FATAL
}
if _, err := category.FromString(cli.Category); err != nil {
log.Fatal().
Caller().
Msg("Invalid category flag")
// ^FATAL
}
return cli
}