-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
200 lines (170 loc) · 5.38 KB
/
cmd.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 main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
db "github.com/squirlyfoxy/ronny/database"
)
func StartCMD(dt db.Database) {
//Start command parser
fmt.Println("Welcome to Ronny Database Version 1.0")
fmt.Println("Type 'help' for a list of commands")
fmt.Println("--------------------------------------")
fmt.Println("")
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("ronny> ")
//Read input, space is not a newline
var input string
input, _ = reader.ReadString('\n')
//CRLF to LF
input = strings.Replace(input, "\n", "", -1)
//Commands
//'help' - Prints the list of commands
//'exit' - Exits the program
//'take [typeName] from [startTypeName] where [startTypeKey] = [value]' - execute a simple query
//'exec [functionName] from [typeName]' - execute a function
//'reload' - reloads the database
//Check if input is 'help'
if strings.HasPrefix(input, "help") {
fmt.Println("'help' - Prints the list of commands")
fmt.Println("'exit' - Exits from Ronny")
fmt.Println("'wami' - Show the name of the database")
fmt.Println("'chme [newName]' - Change the name of the database")
fmt.Println("'tlist' - show all the tables")
fmt.Println("'shtable [tableID]' - show a table structure")
fmt.Println("'rash' - clean all scripts hashes")
fmt.Println("'save' - save the database")
fmt.Println("'serve' - start the API web server")
fmt.Println("'realod scripts' - reload the database resetting the data structures")
} else if strings.HasPrefix(input, "exit") {
fmt.Println("Bye!")
break
} else if strings.HasPrefix(input, "reload scripts") {
//Remove all the scripts
dt.Scripts = []string{}
//Remove all the tables
dt.Tables = []db.Table{}
//Get all the files in the './db/scripts' folder
files, err := ioutil.ReadDir("./db/scripts")
if err != nil {
fmt.Println(err)
continue
}
//For each file
for _, f := range files {
dt.ReadScript(string("./db/scripts/" + f.Name()))
}
fmt.Println()
//Save the database
dt.Save()
//Write "Database reloaded!" in green
fmt.Print("\033[32mDatabase reloaded!\033[0m\n")
} else if strings.HasPrefix(input, "tlist") {
if len(dt.Tables) == 0 {
fmt.Println("No tables found")
continue
}
//Print all the table names
fmt.Println("Tables:")
fmt.Println("--------------------------------------")
fmt.Println("| ID | Name |")
fmt.Println("--------------------------------------")
var i int
for _, t := range dt.Tables {
fmt.Printf("| %d | %s |\n", i, t.Name)
i++
}
fmt.Println("--------------------------------------")
fmt.Println("")
fmt.Println("Use ID in this environment to access tables fastly.")
} else if strings.HasPrefix(input, "shtable") {
//Get the table ID (shtable [tableID]). convert it to an int. Remove \r at the end
rs := strings.Replace(input, "shtable ", "", -1)
rs = strings.Replace(rs, "\r", "", -1)
tableID, err := strconv.Atoi(rs)
if err != nil {
fmt.Println("Invalid table ID, use tlist to see all the avviable tables")
continue
}
if len(dt.Tables) <= tableID {
fmt.Println("Invalid table ID, use tlist to see all the avviable tables")
continue
}
if len(dt.Tables) == 0 {
fmt.Println("No tables found")
continue
}
//Get the table
t := dt.Tables[tableID]
//Print the table structure
fmt.Println("Table: " + t.Name)
//Print the table structure
//Name and type should be alighed to the same length
fmt.Printf("%-15s\n", "--------------------------------------")
fmt.Printf("| %-15s | %-15s |\n", "Name", "Type")
fmt.Printf("%-15s\n", "--------------------------------------")
for _, c := range t.Columns {
var ts string
ts = db.ColumnTypes[c.Type]
fmt.Printf("| %-15s | %-15s |", c.Name, ts)
//If autoincrement, is the primary key ("<-")
if c.Rule == db.AUTOINCREMENT {
fmt.Printf(" <- PRIMARY_KEY")
} else if c.Rule == db.USERACCESSKEY {
fmt.Printf(" <- USER_ACCESS_KEY")
} else if c.IsArray {
fmt.Printf(" <- ARRAY")
}
fmt.Printf("\n")
}
fmt.Printf("%-15s\n", "--------------------------------------")
fmt.Println("")
fmt.Println("Functions: ", len(t.Functions))
} else if strings.HasPrefix(input, "wami") {
if dt.Name == "" {
fmt.Println("Your database has no name YeT.")
continue
}
fmt.Println("Database: " + dt.Name)
} else if strings.HasPrefix(input, "chme") {
//Get the new name
rs := strings.Replace(input, "chme ", "", -1)
rs = strings.Replace(rs, "\r", "", -1)
if rs == "" {
fmt.Println("You need to specify a new name for the database")
continue
}
//Change the name
dt.Name = rs
} else if strings.HasPrefix(input, "save") {
//Save the database
dt.Save()
fmt.Println("Database saved!")
} else if strings.HasPrefix(input, "serve") {
//Start the API web server
db.StartADS(&dt)
} else if strings.HasPrefix(input, "rash") {
//Clean "SCRIPTS_HASH" file
err := os.Remove("./db/SCRIPTS_HASH")
if err != nil {
fmt.Println(err)
}
file, err := os.Create("./db/SCRIPTS_HASH")
if err != nil {
fmt.Println(err)
}
file.Close()
} else {
//If nothing, continue
if input == "" || input == "\r" {
continue
}
//Print red: Uknown command
fmt.Print("\033[31mUnknown command\033[0m\n")
}
}
}