-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
219 lines (189 loc) · 4.94 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
// Copyright 2015 Osipov Konstantin <k.osipov.msk@gmail.com>. All rights reserved.
// license that can be found in the LICENSE file.
// This file is part of the application source code leveldb-cli
// This software provides a console interface to leveldb.
// ATTENTION! This version of the software is an experimental!
// There is no guarantee that the application will work correctly
// This code will be refactored, so do not rely on its structure
package main
import (
"fmt"
"os"
"path"
"runtime"
"strconv"
"bitbucket.org/creachadair/shell"
"github.com/chzyer/readline"
"github.com/liderman/leveldb-cli/commands"
)
// Software version number
const VERSION = "0.3.0"
var completer = readline.NewPrefixCompleter(
readline.PcItem("show",
readline.PcItem("prefix"),
readline.PcItem("range"),
),
readline.PcItem("exit"),
readline.PcItem("quit"),
readline.PcItem("help"),
readline.PcItem("get"),
readline.PcItem("export"),
readline.PcItem("put"),
readline.PcItem("set"),
readline.PcItem("delete"),
readline.PcItem("version"),
)
// Main function
func main() {
l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
HistoryFile: "/tmp/leveldb-cli.tmp",
AutoComplete: completer,
})
if err != nil {
panic(err)
}
defer l.Close()
fmt.Println("*********************************************************")
fmt.Println("** LevelDB CLI **")
fmt.Println("*********************************************************")
fmt.Println("")
fmt.Println("Run command 'help' for help.")
for {
line, err := l.Readline()
if err != nil {
break
}
args, _ := shell.Split(line)
switch {
// Command: version
case line == "version":
fmt.Printf("Version %s. %s %s %s\n", VERSION, runtime.Compiler, runtime.GOARCH, runtime.GOOS)
break
// Command: help
case line == "help":
fmt.Println("Enter one of the commands to get help: show, set, get, export, open, close, put, delete, version")
break
// Command: quit and exit
case line == "quit":
case line == "exit":
goto exit
case len(args) == 0:
break
// Command: show
case args[0] == "show":
if len(args) == 1 {
fmt.Println("Bad format. Please use 'show prefix|range|limit'")
break
}
switch args[1] {
// Sub-command: range
case "range":
if len(args) < 4 || len(args) > 5 {
fmt.Println("Bad format. Please use 'show range START LIMIT [FORMAT]'")
break
}
format := ""
if len(args) == 5 {
format = args[4]
}
fmt.Println(commands.ShowByRange(args[2], args[3], format))
break
// Sub-command: prefix
case "prefix":
if len(args) < 3 || len(args) > 4 {
fmt.Println("Bad format. Please use 'show prefix PREFIX [FORMAT]'")
break
}
format := ""
if len(args) == 4 {
format = args[3]
}
fmt.Println(commands.ShowByPrefix(args[2], format))
break
// Sub-command: limit
case "limit":
if len(args) < 3 || len(args) > 4 {
fmt.Println("Bad format. Please use 'show limit LIMIT [FORMAT]'")
break
}
limit, err := strconv.Atoi(args[2])
if err != nil {
fmt.Println("Bad LIMIT value. LIMIT must be a number")
break
}
format := ""
if len(args) == 4 {
format = args[3]
}
fmt.Println(commands.ShowLimit(limit, format))
break
default:
fmt.Println("Bad format. Please use 'show prefix|range|limit'")
}
break
// Command: put and set
case args[0] == "put":
case args[0] == "set":
if len(args) != 3 {
fmt.Printf("Bad format. Please use '%s KEY VALUE'\n", args[0])
break
}
fmt.Println(commands.Set(args[1], args[2]))
break
// Command: get
case args[0] == "get":
if len(args) < 2 || len(args) > 3 {
fmt.Println("Bad format. Please use 'get KEY FORMAT'")
break
}
format := ""
if len(args) == 3 {
format = args[2]
}
fmt.Println(commands.Get(args[1], format))
break
//Command export
case args[0] == "export":
if len(args) != 3 {
fmt.Println("Bad format. Please use 'export KEY FILENAME'")
break
}
fmt.Println(commands.Export(args[1], args[2]))
break
// Command: delete
case args[0] == "delete":
if len(args) != 2 {
fmt.Print("Bad format. Please use 'delete KEY'")
break
}
fmt.Println(commands.Delete(args[1]))
break
// Command: close
case args[0] == "close":
if len(args) != 1 {
fmt.Print("Bad format. Please use 'close'")
break
}
l.SetPrompt("\033[31m»\033[0m ")
fmt.Println(commands.Close())
break
// Command: open
case args[0] == "open":
_, err := os.Stat(args[1])
if err != nil {
fmt.Println("Database not exist! Create new database.")
}
if len(args) != 2 {
fmt.Println("Bad format. Please use 'open DATABASE_NAME'")
break
}
l.SetPrompt(fmt.Sprintf("\033[31m%s»\033[0m ", path.Base(args[1])))
fmt.Println(commands.Open(args[1]))
break
default:
fmt.Println("Unknown command: ", strconv.Quote(line))
}
}
exit:
}