-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
112 lines (100 loc) · 1.99 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
package main
import (
"fmt"
"github.com/peterh/liner"
"github.com/skua/hashtable"
)
func main() {
line := liner.NewLiner()
defer line.Close()
line.SetCtrlCAborts(true)
fmt.Println("Skua v0.0.1 - Key-Value Store cli")
fmt.Printf("Max Key length: 256 bytes\nMax Value length: 1MB\nUsage:\n\t add <key> <value>\n")
fmt.Println("\t update <key> <value>")
fmt.Println("\t del <key>")
fmt.Println("\t get <key>")
fmt.Printf("Type 'exit' to quit\n\n")
ht := hashtable.NewHashTable(16)
for {
cmd, err := line.Prompt("Skua> ")
if err != nil {
fmt.Println("Error reading input:", err)
break
}
switch cmd {
case "exit":
fmt.Println("Goodbye!")
return
default:
processCommand(cmd, ht)
}
}
}
func processCommand(cmd string, ht *hashtable.HashTable) {
args := splitCommand(cmd)
if len(args) == 0 {
return
}
switch args[0] {
case "add":
if len(args) == 3 {
key := args[1]
value := args[2]
ht.Insert(key, value)
} else {
fmt.Println("Skua> Usage: add key value")
}
case "update":
if len(args) == 3 {
key := args[1]
value := args[2]
ht.Update(key, value)
} else {
fmt.Println("Skua> Usage: update key value")
}
case "get":
if len(args) == 2 {
key := args[1]
v, e := ht.Get(key)
if e {
fmt.Printf("Skua> %s not found\n", key)
}
fmt.Println(v)
} else {
fmt.Println("Skua> Usage: get key")
}
case "del":
if len(args) == 2 {
key := args[1]
ht.Delete(key)
} else {
fmt.Println("Skua> Usage: del key")
}
default:
fmt.Printf("Skua> Unrecognized command: %s\n", args[0])
}
}
func splitCommand(cmd string) []string {
args := make([]string, 0)
inQuotes := false
currentArg := ""
for _, char := range cmd {
switch char {
case ' ':
if !inQuotes {
args = append(args, currentArg)
currentArg = ""
} else {
currentArg += string(char)
}
case '"':
inQuotes = !inQuotes
default:
currentArg += string(char)
}
}
if currentArg != "" {
args = append(args, currentArg)
}
return args
}