-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.go
75 lines (63 loc) · 2.07 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
_ "github.com/milvus-io/birdwatcher/asap"
"github.com/milvus-io/birdwatcher/bapps"
"github.com/milvus-io/birdwatcher/common"
"github.com/milvus-io/birdwatcher/configs"
"github.com/milvus-io/birdwatcher/states"
)
var (
oneLineCommand = flag.String("olc", "", "one line command execution mode")
simple = flag.Bool("simple", false, "use simple ui without suggestion and history")
restServer = flag.Bool("rest", false, "rest server address")
webPort = flag.Int("port", 8002, "listening port for web server")
printVersion = flag.Bool("version", false, "print version")
)
func main() {
flag.Parse()
var appFactory func(config *configs.Config) bapps.BApp
switch {
//Print current birdwatcher version
case *printVersion:
fmt.Println("Birdwatcher Version", common.Version)
return
case *simple:
appFactory = func(*configs.Config) bapps.BApp { return bapps.NewSimpleApp() }
case len(*oneLineCommand) > 0:
appFactory = func(*configs.Config) bapps.BApp { return bapps.NewOlcApp(*oneLineCommand) }
case *restServer:
appFactory = func(config *configs.Config) bapps.BApp { return bapps.NewWebServerApp(*webPort, config) }
default:
defer handleExit()
// open file and create if non-existent
file, err := os.OpenFile("bw_debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
logger := log.New(file, "Custom Log", log.LstdFlags)
appFactory = func(config *configs.Config) bapps.BApp {
return bapps.NewPromptApp(config, bapps.WithLogger(logger))
}
}
config, err := configs.NewConfig(".bw_config")
if err != nil {
// run by default, just printing warning.
fmt.Println("[WARN] load config file failed, running in default setting", err.Error())
}
start := states.Start(config)
app := appFactory(config)
app.Run(start)
}
// handleExit is the fix for go-prompt output hi-jack fix.
func handleExit() {
rawModeOff := exec.Command("/bin/stty", "-raw", "echo")
rawModeOff.Stdin = os.Stdin
_ = rawModeOff.Run()
rawModeOff.Wait()
}