-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
64 lines (52 loc) · 1.35 KB
/
cli.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
package main
import (
"os"
"time"
"github.com/fatih/color"
"github.com/thiepwong/spin-notification/common"
"github.com/thiepwong/spin-notification/database"
)
type conf struct {
Time int64 `yaml:"time"`
}
func main() {
common.DrawLogo()
cfg, data := initCheck()
mq := database.CreateClient(cfg.Db.Mqtt, data)
// if token := mq.Client.Connect(); token.Wait() && token.Error() != nil {
// panic(token.Error())
// }
// ch, e := data.Request("23-0948221")
// if e != nil {
// fmt.Println(e.Error())
// }
// fmt.Println(ch)
mq.Connect(data)
}
func initCheck() (*common.Config, *database.Database) {
var c common.Config
//var c types.Config
color.Yellow("Config checking...")
time.Sleep(1 * time.Second)
c.GetConf()
if c.Db.Pg.Host == "" || c.Db.Pg.Port == "" || c.Db.Pg.Username == "" {
color.Red("-- PostgreSQL database config load failed!")
os.Exit(1)
}
color.Green("-- PostgreSQL database config pass!")
if c.Db.Mqtt.Host == "" || c.Db.Mqtt.Port == "" || c.Db.Mqtt.DbName == "" {
color.Red("-- MQTT broker config load failed!")
os.Exit(2)
}
color.Green("-- MQTT broker config pass!")
time.Sleep(2 * time.Second)
color.Yellow("Connecting database...")
db, e := database.NewDb(&c.Db.Pg)
if e != nil {
color.Red("Database connect failed")
color.Red(e.Error())
os.Exit(0)
}
color.Green("-- Database connected!")
return &c, db
}