-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_client.go
More file actions
161 lines (143 loc) · 4.08 KB
/
interactive_client.go
File metadata and controls
161 lines (143 loc) · 4.08 KB
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
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
"github.com/ayushgala/tinkerdb/pkg/client"
)
func main() {
// Connect to TinkerDB
cfg := &client.Config{
Address: "localhost:50051",
TenantID: "interactive",
}
c, err := client.NewClient(cfg)
if err != nil {
log.Fatalf("Failed to connect to TinkerDB: %v", err)
}
defer c.Close()
scanner := bufio.NewScanner(os.Stdin)
ctx := context.Background()
fmt.Println("╔════════════════════════════════════════════════════════╗")
fmt.Println("║ TinkerDB Interactive Client v1.0 ║")
fmt.Println("╚════════════════════════════════════════════════════════╝")
fmt.Printf("\nConnected to: %s\n", cfg.Address)
fmt.Printf("Current tenant: %s\n", c.GetTenant())
fmt.Println("\nCommands:")
fmt.Println(" set <key> <value> - Set a key-value pair")
fmt.Println(" get <key> - Get value for a key")
fmt.Println(" delete <key> - Delete a key")
fmt.Println(" exists <key> - Check if key exists")
fmt.Println(" keys - List all keys")
fmt.Println(" tenant <id> - Switch tenant (or show current)")
fmt.Println(" help - Show this help")
fmt.Println(" quit - Exit")
fmt.Println()
for {
fmt.Printf("[%s]> ", c.GetTenant())
if !scanner.Scan() {
break
}
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
parts := strings.Fields(line)
cmd := parts[0]
switch cmd {
case "set":
if len(parts) < 3 {
fmt.Println("❌ Usage: set <key> <value>")
continue
}
key := parts[1]
value := strings.Join(parts[2:], " ")
err := c.SetString(ctx, key, value)
if err != nil {
fmt.Printf("❌ Error: %v\n", err)
} else {
fmt.Printf("✓ Set '%s' = '%s'\n", key, value)
}
case "get":
if len(parts) < 2 {
fmt.Println("❌ Usage: get <key>")
continue
}
key := parts[1]
value, err := c.GetString(ctx, key)
if err != nil {
fmt.Printf("❌ Error: %v\n", err)
} else {
fmt.Printf("✓ %s = '%s'\n", key, value)
}
case "delete":
if len(parts) < 2 {
fmt.Println("❌ Usage: delete <key>")
continue
}
key := parts[1]
err := c.Delete(ctx, key)
if err != nil {
fmt.Printf("❌ Error: %v\n", err)
} else {
fmt.Printf("✓ Deleted '%s'\n", key)
}
case "exists":
if len(parts) < 2 {
fmt.Println("❌ Usage: exists <key>")
continue
}
key := parts[1]
exists, err := c.Exists(ctx, key)
if err != nil {
fmt.Printf("❌ Error: %v\n", err)
} else {
if exists {
fmt.Printf("✓ Key '%s' exists\n", key)
} else {
fmt.Printf("✗ Key '%s' does not exist\n", key)
}
}
case "keys":
keys, err := c.Keys(ctx)
if err != nil {
fmt.Printf("❌ Error: %v\n", err)
} else {
if len(keys) == 0 {
fmt.Println("No keys found")
} else {
fmt.Printf("✓ Found %d key(s):\n", len(keys))
for i, k := range keys {
fmt.Printf(" %d. %s\n", i+1, k)
}
}
}
case "tenant":
if len(parts) < 2 {
fmt.Printf("Current tenant: %s\n", c.GetTenant())
} else {
c.SetTenant(parts[1])
fmt.Printf("✓ Switched to tenant '%s'\n", parts[1])
}
case "help":
fmt.Println("\nCommands:")
fmt.Println(" set <key> <value> - Set a key-value pair")
fmt.Println(" get <key> - Get value for a key")
fmt.Println(" delete <key> - Delete a key")
fmt.Println(" exists <key> - Check if key exists")
fmt.Println(" keys - List all keys")
fmt.Println(" tenant <id> - Switch tenant (or show current)")
fmt.Println(" help - Show this help")
fmt.Println(" quit - Exit")
fmt.Println()
case "quit", "exit":
fmt.Println("\n👋 Goodbye!")
return
default:
fmt.Printf("❌ Unknown command: %s (type 'help' for commands)\n", cmd)
}
}
}