This repository has been archived by the owner on Feb 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
200 lines (172 loc) · 3.57 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package karigo
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/urfave/cli"
)
func checkCmd() cli.Command {
return cli.Command{
Name: "check",
Aliases: []string{},
Usage: "Displays the differences between the app schema and the store schema.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "apply",
Usage: "apply the updates",
},
cli.StringFlag{
Name: "config",
Usage: "path to config file",
Value: "config.json",
},
},
Action: func(c *cli.Context) error {
app, err := PrepareCmd(c)
if err != nil {
return err
}
// Sync store
app.Info("Syncing store...")
err = app.Store.SyncDatabase(nil, app.Registry, true, c.Bool("apply"))
if err != nil {
return err
}
app.Info("Store schema is synced.")
TerminateCmd(app)
return nil
},
}
}
func drainCmd() cli.Command {
return cli.Command{
Name: "drain",
Aliases: []string{},
Usage: "Empties the store (including the tables if necessary), but keeps the store itself.",
Flags: []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "path to config file",
Value: "config.json",
},
},
Action: func(c *cli.Context) error {
app, err := PrepareCmd(c)
if err != nil {
return err
}
// Drain store
app.Info("Draining store...")
err = app.Store.DrainDatabase(nil)
if err != nil {
return err
}
app.Info("Store is now drained.")
TerminateCmd(app)
return nil
},
}
}
func schemaCmd() cli.Command {
return cli.Command{
Name: "schema",
Aliases: []string{},
Usage: "Displays the app schema.",
Flags: []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "path to config file",
Value: "config.json",
},
},
Action: func(c *cli.Context) error {
app, err := PrepareCmd(c)
if err != nil {
return err
}
info, err := json.MarshalIndent(app, "", "\t")
if err != nil {
panic(err)
}
// Info
app.Info("\n" + string(info))
TerminateCmd(app)
return nil
},
}
}
func runCmd() cli.Command {
return cli.Command{
Name: "run",
Aliases: []string{},
Usage: "Instantiates the app and starts serving requests.",
Flags: []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "path to config file",
Value: "config.json",
},
},
Action: func(c *cli.Context) error {
app, err := PrepareCmd(c)
if err != nil {
return err
}
app.Info("Now listening on %d...\n\n", app.Config.Port)
err = app.Run()
if err != nil {
if err != http.ErrServerClosed {
return err
}
}
TerminateCmd(app)
return nil
},
}
}
// PrepareCmd ...
func PrepareCmd(c *cli.Context) (*App, error) {
app := c.App.Metadata["app"].(*App)
// Configuration
data, err := ioutil.ReadFile(c.String("config"))
if err != nil {
return app, err
}
err = app.ReadConfig(data)
if err != nil {
return app, err
}
if app.Config.Debug {
app.Info("Debug is ON.")
} else {
app.Info("Debug is OFF.")
}
// Check app
errs := app.Check()
if len(errs) > 0 {
return app, errs[0]
}
// Connect to database
app.Info("Connecting to database...")
err = app.Store.Open(
app.Config.Store.Driver,
app.Config.Store.Host,
app.Config.Store.Database,
app.Config.Store.User,
app.Config.Store.Password,
app.Config.Store.Options,
)
app.Info("URL: %s", app.Store.URL())
if err != nil {
return app, err
}
app.Info("Connection to database established.")
return app, nil
}
// TerminateCmd ...
func TerminateCmd(app *App) error {
app.Info("Closing database connection...")
app.Store.Close()
app.Info("Connection closed.")
return nil
}