-
Notifications
You must be signed in to change notification settings - Fork 2
/
light-messenger.go
98 lines (78 loc) · 2.13 KB
/
light-messenger.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
package main
import (
"log"
"os"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/usb-radiology/light-messenger/src/configuration"
"github.com/usb-radiology/light-messenger/src/lmdatabase"
"github.com/usb-radiology/light-messenger/src/server"
"github.com/usb-radiology/light-messenger/src/version"
)
func main() {
log.Printf("%s %s", version.Version, version.BuildTime)
initConfig, err := configuration.LoadAndSetConfiguration("./config.json")
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
}
app := cli.NewApp()
app.Name = "light-messenger"
app.Usage = ""
app.Version = version.Version + " " + version.BuildTime
app.Commands = []cli.Command{
{
Name: "web",
Usage: "run web server (default)",
Action: func(c *cli.Context) error {
return actionWeb(initConfig)
},
},
{
Name: "db-exec",
Usage: "execute db script",
Action: func(c *cli.Context) error {
return actionDbExec(initConfig, c)
},
Flags: []cli.Flag{
cli.StringFlag{Name: "script-path"},
},
},
}
app.Action = app.Commands[0].Action
errRun := app.Run(os.Args)
if errRun != nil {
log.Fatalf("%+v", errors.WithStack(errRun))
}
}
func actionWeb(initConfig *configuration.Configuration) error {
httpServer := server.InitServer(initConfig)
server.Start(httpServer, initConfig.Server.HTTPPort)
return nil
}
func actionDbExec(initConfig *configuration.Configuration, c *cli.Context) error {
db, errDb := lmdatabase.GetDB(initConfig)
if errDb != nil {
return errDb
}
scriptPath := c.String("script-path")
statements, errReadStatements := lmdatabase.ReadStatementsFromSQL(scriptPath)
if errReadStatements != nil {
return errReadStatements
}
for _, statement := range *statements {
log.Printf("%s", statement)
execStatementResult, errExecStatement := lmdatabase.ExecStatement(db, statement)
if errExecStatement != nil {
return errExecStatement
}
if execStatementResult == nil {
continue
}
rowsAffected, errRowsAffected := execStatementResult.RowsAffected()
if errRowsAffected != nil {
return errRowsAffected
}
log.Printf("rowsAffected %d", rowsAffected)
}
return nil
}