forked from weberhong/goose
/
Goose.go
172 lines (141 loc) · 3.5 KB
/
Goose.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package goose
import (
"fmt"
"github.com/fatih/color"
"github.com/getwe/figlet4go"
log "github.com/getwe/goose/log"
flags "github.com/jessevdk/go-flags"
"os"
"time"
)
// goose的入口程序.
type Goose struct {
// 建库策略
indexSty IndexStrategy
// 检索策略
searchSty SearchStrategy
// 配置文件
confPath string
// 日志配置
logConfPath string
// 建库模式数据文件
dataPath string
}
func (this *Goose) SetIndexStrategy(sty IndexStrategy) {
this.indexSty = sty
}
func (this *Goose) SetSearchStrategy(sty SearchStrategy) {
this.searchSty = sty
}
// 程序入口,解析程序参数,启动[建库|检索]模式
func (this *Goose) Run() {
defer func() {
if r := recover(); r != nil {
os.Exit(1)
}
}()
// 解析命令行参数
var opts struct {
// build mode
BuildMode bool `short:"b" long:"build" description:"run in build mode"`
// configure file
Configure string `short:"c" long:"conf" description:"congfigure file" default:"conf/goose.toml"`
// log configure file
LogConf string `short:"l" long:"logconf" description:"log congfigure file" default:"conf/log.toml"`
// build mode data file
DataFile string `short:"d" long:"datafile" description:"build mode data file"`
}
parser := flags.NewParser(&opts, flags.HelpFlag)
_, err := parser.ParseArgs(os.Args)
if err != nil {
fmt.Println(this.showLogo())
fmt.Println(err)
os.Exit(1)
}
if opts.BuildMode && len(opts.DataFile) == 0 {
fmt.Println(this.showLogo())
parser.WriteHelp(os.Stderr)
os.Exit(1)
}
this.confPath = opts.Configure
this.dataPath = opts.DataFile
this.logConfPath = opts.LogConf
// init log
err = log.LoadConfiguration(this.logConfPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
log.Debug("Load log conf finish")
// run
if opts.BuildMode {
this.buildModeRun()
} else {
this.searchModeRun()
}
// BUG(log4go) log4go need time to sync ...(wtf)
// see http://stackoverflow.com/questions/14252766/abnormal-behavior-of-log4go
time.Sleep(100 * time.Millisecond)
}
func (this *Goose) showLogo() string {
str := "goose"
ascii := figlet4go.NewAsciiRender()
// change the font color
options := figlet4go.NewRenderOptions()
options.FontColor = make([]color.Attribute, len(str))
options.FontColor[0] = color.FgMagenta
options.FontColor[1] = color.FgYellow
options.FontColor[2] = color.FgBlue
options.FontColor[3] = color.FgCyan
options.FontColor[4] = color.FgRed
renderStr, _ := ascii.RenderOpts(str, options)
return renderStr
}
// 建库模式运行
func (this *Goose) buildModeRun() {
if this.indexSty == nil {
log.Error("Please set index strategy,see Goose.SetIndexStrategy()")
return
}
gooseBuild := NewGooseBuild()
err := gooseBuild.Init(this.confPath, this.indexSty, this.dataPath)
if err != nil {
fmt.Println(err)
log.Error(err)
return
}
err = gooseBuild.Run()
if err != nil {
log.Error(err)
return
}
}
// 检索模式运行
func (this *Goose) searchModeRun() {
log.Debug("run in search mode")
if this.searchSty == nil {
log.Error("Please set search strategy,see Goose.SetSearchStrategy()")
return
}
if this.indexSty == nil {
log.Warn("can't build index real time witout Index Strategy")
}
gooseSearch := NewGooseSearch()
err := gooseSearch.Init(this.confPath, this.indexSty, this.searchSty)
if err != nil {
log.Error(err)
return
}
log.Debug("goose search init succ")
err = gooseSearch.Run()
if err != nil {
log.Error(err)
return
}
}
func NewGoose() *Goose {
g := Goose{}
g.indexSty = nil
g.searchSty = nil
return &g
}