-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
99 lines (90 loc) · 2.32 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
/*
* Ferret
* Copyright (c) 2016 Yieldbot, Inc.
* For the full copyright and license information, please view the LICENSE.txt file.
*/
// Ferret is a search engine
package main
import (
"flag"
"fmt"
"os"
"github.com/yieldbot/ferret/api"
"github.com/yieldbot/ferret/assets"
conf "github.com/yieldbot/ferret/config"
"github.com/yieldbot/ferret/search"
"github.com/yieldbot/gocli"
)
func init() {
// Init flags
flag.BoolVar(&usageFlag, "h", false, "Display usage")
flag.BoolVar(&usageFlag, "help", false, "Display usage")
flag.BoolVar(&versionFlag, "v", false, "Display version information")
flag.BoolVar(&versionExtFlag, "vv", false, "Display extended version information")
flag.StringVar(&configFlag, "config", "", "Config file")
}
var (
version = "latest"
commit = ""
cli gocli.Cli
usageFlag bool
versionFlag bool
versionExtFlag bool
configFlag string
config conf.Config
)
func main() {
// Init cli
cli = gocli.Cli{
Name: "Ferret",
Version: version,
Description: "Ferret is a search engine",
Commands: map[string]string{
"listen": "Listen for the UI and REST API requests (Usage: ferret listen)",
"search": "Search by the given provider (Usage: ferret search PROVIDER KEYWORD)",
},
}
cli.Init()
// Config
config = conf.Config{}
if configFlag != "" {
config.File = configFlag
} else if os.Getenv("FERRET_CONFIG") != "" {
config.File = os.Getenv("FERRET_CONFIG")
}
if config.File != "" {
if err := config.Load(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if versionFlag || versionExtFlag {
// Version
cli.PrintVersion(versionExtFlag)
} else if cli.SubCommand == "search" {
// Search
search.Init(config)
q := search.Query{
Page: search.ParsePage(cli.SubCommandArgsMap["page"]),
Goto: search.ParseGoto(cli.SubCommandArgsMap["goto"]),
Timeout: search.ParseTimeout(cli.SubCommandArgsMap["timeout"]),
Limit: search.ParseLimit(cli.SubCommandArgsMap["limit"]),
}
if len(cli.SubCommandArgs) > 0 {
q.Provider = cli.SubCommandArgs[0]
if len(cli.SubCommandArgs) > 1 {
q.Keyword = cli.SubCommandArgs[1]
}
}
q.DoPrint(q.Do())
} else if cli.SubCommand == "listen" {
// Listen
search.Init(config)
api.Init(config)
assets.Init(config)
api.Listen()
} else {
// Default
cli.PrintUsage()
}
}