-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
505 lines (435 loc) · 13.7 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// cli provides a terminal interface to the flaarum server.
package main
import (
"os"
"fmt"
"github.com/bankole7782/flaarum"
"github.com/bankole7782/flaarum/flaarum_shared"
"strings"
"strconv"
"encoding/json"
"github.com/gookit/color"
"github.com/tidwall/pretty"
"os/exec"
)
func main() {
if len(os.Args) < 2 {
color.Red.Println("expected a command. Open help to view commands.")
os.Exit(1)
}
var keyStr string
inProd := flaarum_shared.GetSetting("in_production")
if inProd == "" {
color.Red.Println("unexpected error. Have you installed and launched flaarum?")
os.Exit(1)
}
if inProd == "true"{
keyStrPath := flaarum_shared.GetKeyStrPath()
raw, err := os.ReadFile(keyStrPath)
if err != nil {
color.Red.Println(err)
os.Exit(1)
}
keyStr = string(raw)
} else {
keyStr = "not-yet-set"
}
cl := flaarum.NewClient("127.0.0.1", keyStr, "first_proj")
err := cl.Ping()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch os.Args[1] {
case "--help", "help", "h":
fmt.Println(`flaarum cli provides some utilites for a flaarum installation.
Please Run this program from the same server that powers your flaarum.
Please don't expose your flaarum database to the internet for security sake.
Directory Commands:
pwd Print working directory. This is the directory where the files needed by any command
in this cli program must reside.
Project(s) Commands:
lp List Projects
cp Create Project: Expects the name(s) of projects after the command.
rp Rename Project. Expects the name of the project to rename followed by the new name of the project.
dp Delete Project. Expects the name(s) of projects after the command.
Table(s) Commands:
lt List Tables: Expects a project name after the command.
ct Create Table: Expects a project name and the path to a file containing the table structure
uts Update Table Structure: Expects a project name and the path to a file containing the table structure
ctvn Current Table Version Number: Expects a project and table combo eg. 'first_proj/users'
ts Table Structure Statement: Expects a project and table combo eg. 'first_proj/users' and a valid number.
dt Delete Table: Expects one or more project and table combo eg. 'first_proj/users'.
et Empty Table: Expects one or more project and table combo eg. 'first_proj/users'.
rt Rename Table: Expects a project, old table name and the new table name.
Table Data Commands:
ir Insert a row: Expects a project and table combo eg. 'first_proj/users' and a path containing a
json representation of the data to be inserted into the mentioned table.
ur Update Row: Expects a project, table and id combo eg. 'first_proj/users/31' and a path containing a
json representation of the data to be inserted into the mentioned table.
dr Delete Row: Expects one or more project, table and id combo eg. 'first_proj/users/31'
vr View Row: Expects a project, table and id combo eg. 'first_proj/users/31'
Table Search Commands:
st Search Table: Expects a project and a file containing the search statement.
arc All Rows Count: Expects a project and table combo eg. 'first_proj/users'
rc Count of rows found in a search. Expects a project and a file containing a search statement.
`)
case "pwd":
p, err := flaarum_shared.GetFlaarumPath("")
if err != nil {
color.Red.Println(err)
os.Exit(1)
}
fmt.Println(p)
case "lp":
projects, err := cl.ListProjects()
if err != nil {
color.Red.Println(err)
os.Exit(1)
}
fmt.Println("Projects List:\n")
for _, proj := range projects {
fmt.Printf(" %s\n", proj)
}
fmt.Println()
case "cp":
if len(os.Args) < 3 {
color.Red.Println("'cp' command expects project(s)")
os.Exit(1)
}
for _, arg := range os.Args[2:] {
err = cl.CreateProject(arg)
if err != nil {
color.Red.Printf("Error creating project '%s':\nError: %s\n", arg, err)
os.Exit(1)
}
}
case "rp":
if len(os.Args) != 4 {
color.Red.Println("'rp' command expects an old project arg and a new project arg.")
os.Exit(1)
}
err = cl.RenameProject(os.Args[2], os.Args[3])
if err != nil {
color.Red.Printf("Error renaming project from '%s' to '%s'.\nError: %s\n", os.Args[2], os.Args[3], err)
os.Exit(1)
}
case "dp":
if len(os.Args) < 3 {
color.Red.Println("'dp' command expects project(s)")
os.Exit(1)
}
for _, arg := range os.Args[2:] {
err = cl.DeleteProject(arg)
if err != nil {
color.Red.Printf("Error deleting project '%s':\nError: %s\n", arg, err)
os.Exit(1)
}
}
case "lt":
if len(os.Args) != 3 {
color.Red.Println("'lt' command expects a project name.")
os.Exit(1)
}
cl.ProjName = os.Args[2]
tables, err := cl.ListTables()
if err != nil {
color.Red.Printf("Error listing tables of project '%s'.\nError: %s\n", os.Args[2], err)
os.Exit(1)
}
fmt.Printf("Tables List of Project '%s' List:\n\n", os.Args[2])
for _, tbl := range tables {
fmt.Printf(" %s\n", tbl)
}
fmt.Println()
case "rt":
if len(os.Args) != 5 {
color.Red.Println("'rt' command expects a project name, old table name and a new table name.")
os.Exit(1)
}
cl.ProjName = os.Args[2]
err := cl.RenameTable(os.Args[3], os.Args[4])
if err != nil {
color.Red.Println("Error renamining table.\nError: %s\n", err)
os.Exit(1)
}
case "arc":
if len(os.Args) != 3 {
color.Red.Println("'arc' command expects a project and table combo eg. 'first_proj/users'.")
os.Exit(1)
}
parts := strings.Split(os.Args[2], "/")
cl.ProjName = parts[0]
count, err := cl.AllRowsCount(parts[1])
if err != nil {
color.Red.Printf("Error reading table '%s' of project '%s' row count.\nError: %s\n", parts[1], parts[0], err)
os.Exit(1)
}
fmt.Println(count)
case "ctvn":
if len(os.Args) != 3 {
color.Red.Println("'ctvn' command expects a project and table combo eg. 'first_proj/users'.")
os.Exit(1)
}
parts := strings.Split(os.Args[2], "/")
cl.ProjName = parts[0]
vnum, err := cl.GetCurrentTableVersionNum(parts[1])
if err != nil {
color.Red.Printf("Error reading current table version number of table '%s' of Project '%s'.\nError: %s\n",
parts[1], parts[0], err)
os.Exit(1)
}
fmt.Println(vnum)
case "ts":
if len(os.Args) != 4 {
color.Red.Println("'ts' command expects a project table combo eg. 'first_proj/users' and a valid version number.")
os.Exit(1)
}
parts := strings.Split(os.Args[2], "/")
cl.ProjName = parts[0]
vnumStr := os.Args[3]
vnum, err := strconv.ParseInt(vnumStr, 10, 64)
if err != nil {
color.Red.Printf("Number supplied '%s' is not a number.\n", vnumStr)
os.Exit(1)
}
tableStructStmt, err := cl.GetTableStructure(parts[1], vnum)
if err != nil {
color.Red.Printf("Error reading table structure number '%s' of table '%s' of Project '%s'.\nError: %s\n",
vnumStr, parts[1], parts[0], err)
os.Exit(1)
}
fmt.Println(tableStructStmt)
case "dt":
if len(os.Args) < 3 {
color.Red.Println("'dt' command expects combo(s) of project and table eg. 'first_proj/users'.")
os.Exit(1)
}
for _, arg := range os.Args[2:] {
parts := strings.Split(arg, "/")
cl.ProjName = parts[0]
err = cl.DeleteTable(parts[1])
if err != nil {
color.Red.Printf("Error deleting table '%s'. \nError: %s\n", arg, err)
os.Exit(1)
}
}
case "et":
if len(os.Args) < 3 {
color.Red.Println("'et' command expects combo(s) of project and table eg. 'first_proj/users'.")
os.Exit(1)
}
for _, arg := range os.Args[2:] {
parts := strings.Split(arg, "/")
cl.ProjName = parts[0]
err = cl.EmptyTable(parts[1])
if err != nil {
color.Red.Printf("Error emptying table '%s'. \nError: %s\n", arg, err)
os.Exit(1)
}
}
case "ct":
if len(os.Args) != 4 {
color.Red.Println("'ct' command expects the project name and a file containing table structure.")
os.Exit(1)
}
inputPath, err := flaarum_shared.GetFlaarumPath(os.Args[3])
if err != nil {
color.Red.Println("The supplied path '%s' does not exits.\n", inputPath)
os.Exit(1)
}
raw, err := os.ReadFile(inputPath)
if err != nil {
color.Red.Printf("The supplied path '%s' does not exists.\n", inputPath)
os.Exit(1)
}
cl.ProjName = os.Args[2]
err = cl.CreateTable(string(raw))
if err != nil {
color.Red.Printf("Error creating table.\nError: %s\n", err)
os.Exit(1)
}
case "uts":
if len(os.Args) != 4 {
color.Red.Println("'uts' command expects the project name and a file containing table structure.")
os.Exit(1)
}
inputPath, err := flaarum_shared.GetFlaarumPath(os.Args[3])
if err != nil {
color.Red.Println("The supplied path '%s' does not exits.\n", inputPath)
os.Exit(1)
}
raw, err := os.ReadFile(inputPath)
if err != nil {
color.Red.Printf("The supplied path '%s' does not exists.\n", inputPath)
os.Exit(1)
}
cl.ProjName = os.Args[2]
err = cl.UpdateTableStructure(string(raw))
if err != nil {
color.Red.Printf("Error updating table.\nError: %s\n", err)
os.Exit(1)
}
case "ir":
if len(os.Args) != 4 {
color.Red.Println(`'ir' command expects a project and table combo eg. 'first_proj/users' and a path containing a
json representation of the data to be inserted into the mentioned table.`)
os.Exit(1)
}
inputPath, err := flaarum_shared.GetFlaarumPath(os.Args[3])
if err != nil {
color.Red.Println("The supplied path '%s' does not exits.\n", inputPath)
os.Exit(1)
}
raw, err := os.ReadFile(inputPath)
if err != nil {
color.Red.Printf("The supplied path '%s' does not exists.\n", inputPath)
os.Exit(1)
}
rowData := make(map[string]string)
err = json.Unmarshal(raw, &rowData)
if err != nil {
color.Red.Printf("The json file is not valid.\nError: %s\n", err)
os.Exit(1)
}
parts := strings.Split(os.Args[2], "/")
cl.ProjName = parts[0]
retId, err := cl.InsertRowStr(parts[1], rowData)
if err != nil {
color.Red.Printf("Error inserting a new row.\nError: %s\n", err)
os.Exit(1)
}
fmt.Println(retId)
case "ur":
if len(os.Args) != 4 {
color.Red.Println(`'ur' command expects a project, table and id combo eg. 'first_proj/users/31' and a path containing a
json representation of the data to be inserted into the mentioned table.`)
os.Exit(1)
}
inputPath, err := flaarum_shared.GetFlaarumPath(os.Args[3])
if err != nil {
color.Red.Println("The supplied path '%s' does not exits.\n", inputPath)
os.Exit(1)
}
raw, err := os.ReadFile(inputPath)
if err != nil {
color.Red.Printf("The supplied path '%s' does not exists.\n", inputPath)
os.Exit(1)
}
rowData := make(map[string]string)
err = json.Unmarshal(raw, &rowData)
if err != nil {
color.Red.Printf("The json file is not valid.\nError: %s\n", err)
os.Exit(1)
}
parts := strings.Split(os.Args[2], "/")
cl.ProjName = parts[0]
err = cl.UpdateRowsStr(fmt.Sprintf(`
table: %s
where:
id = %s
`, parts[1], parts[2]), rowData)
if err != nil {
color.Red.Printf("Error updating row.\nError: %s\n", err)
os.Exit(1)
}
case "dr":
if len(os.Args) < 3 {
color.Red.Println("'dr' command expects one or more project, table and id combo eg. 'first_proj/users/31'")
os.Exit(1)
}
for _, arg := range os.Args[2:] {
parts := strings.Split(os.Args[2], "/")
cl.ProjName = parts[0]
err = cl.DeleteRows(fmt.Sprintf(`
table: %s
where:
id = %s
`, parts[1], parts[2]))
if err != nil {
color.Red.Printf("Error deleting '%s'.\nError: %s\n", arg, err)
os.Exit(1)
}
}
case "vr":
if len(os.Args) != 3 {
color.Red.Println("'vr' command expects a project, table and id combo eg. 'first_proj/users/31'")
os.Exit(1)
}
parts := strings.Split(os.Args[2], "/")
cl.ProjName = parts[0]
arow, err := cl.SearchForOne(fmt.Sprintf(`
table: %s expand
where:
id = %s
`, parts[1], parts[2]))
if err != nil {
color.Red.Printf("Error viewing row '%s'.\nError: %s\n", os.Args[2], err)
os.Exit(1)
}
for k, v := range *arow {
fmt.Printf("%s: %v\n", k, v)
}
fmt.Println()
case "st":
if len(os.Args) != 4 {
color.Red.Println("'st' expects a project and a file containing the search statment.")
os.Exit(1)
}
inputPath, err := flaarum_shared.GetFlaarumPath(os.Args[3])
if err != nil {
color.Red.Println("The supplied path '%s' does not exits.\n", inputPath)
os.Exit(1)
}
raw, err := os.ReadFile(inputPath)
if err != nil {
color.Red.Printf("The supplied path '%s' does not exists.\n", inputPath)
os.Exit(1)
}
cl.ProjName = os.Args[2]
rows, err := cl.Search(string(raw))
if err != nil {
color.Red.Printf("Error running search '%s'.\nError: %s\n", os.Args[3], err)
os.Exit(1)
}
jsonBytes, err := json.Marshal(*rows)
if err != nil {
color.Red.Printf("Error ocurred.\nError: %s\n", err)
os.Exit(1)
}
prettyJson := pretty.Pretty(jsonBytes)
cmd := exec.Command("less")
cmd.Stdin = strings.NewReader(string(prettyJson))
cmd.Stdout = os.Stdout
err = cmd.Run()
if err != nil {
color.Red.Println("Error occured.\nError: %s\n", err)
os.Exit(1)
}
case "rc":
if len(os.Args) != 4 {
color.Red.Println("'rc' expects a project and a file containing the search statment.")
os.Exit(1)
}
inputPath, err := flaarum_shared.GetFlaarumPath(os.Args[3])
if err != nil {
color.Red.Println("The supplied path '%s' does not exits.\n", inputPath)
os.Exit(1)
}
raw, err := os.ReadFile(inputPath)
if err != nil {
color.Red.Printf("The supplied path '%s' does not exists.\n", inputPath)
os.Exit(1)
}
cl.ProjName = os.Args[2]
count, err := cl.CountRows(string(raw))
if err != nil {
color.Red.Printf("Error running search '%s'.\nError: %s\n", os.Args[3], err)
os.Exit(1)
}
fmt.Println(count)
default:
color.Red.Println("Unexpected command. Run the cli with --help to find out the supported commands.")
os.Exit(1)
}
}