-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
59 lines (41 loc) · 945 Bytes
/
listener.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
package main
import (
"bufio"
"fmt"
"go-redis-cli/cache"
"os"
"strings"
)
func Listen() {
reader := bufio.NewScanner(os.Stdin)
client := cache.CreateClient()
listening := true
for listening {
fmt.Print("Enter a cache operation - (g)et <key>, (s)et <key> <value>, (/q)uit: ")
for reader.Scan() {
input := strings.Split(reader.Text(), " ")
inputLength := len(input)
if inputLength == 1 && input[0] == "/q" {
listening = false
break
} else if inputLength == 2 && input[0] == "g" {
key := input[1]
client.GetKeyValue(key)
break
} else if inputLength >= 3 && input[0] == "s" {
key := input[1]
remainingLength := inputLength - 2
var s []string
for i := 0; i < remainingLength; i++ {
s = append(s, input[i+2])
}
value := strings.Join(s, " ")
client.SetKeyValue(key, value)
break
} else {
fmt.Println("Invalid input!")
break
}
}
}
}