-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadis.go
63 lines (51 loc) · 1.12 KB
/
loadis.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Please provide a file name to load\n")
os.Exit(1)
}
filename := os.Args[1]
fmt.Printf("Loading file %q\n", filename)
dirname, err := os.MkdirTemp(".", "tmpledis")
if err != nil {
fmt.Printf("Failed to create temp dir: %v\n", err)
os.Exit(1)
}
fmt.Printf("Data will be loaded into %q\n", dirname)
ledisInfo, err := getLedisInfo(dirname)
if err != nil {
fmt.Printf("Failed to connect to ledis: %v\n", err)
os.Exit(1)
}
if _, err := ledisInfo.Conn.LoadDumpFile(filename); err != nil {
fmt.Printf("Failed to load file: %v\n", err)
os.Exit(1)
}
for {
cmd, done := nextCommand()
if done {
break
}
err := cmd.Execute(ledisInfo)
if err != nil {
fmt.Printf("Command failed: %v\n", err)
}
}
fmt.Printf("k, bye\n")
}
func nextCommand() (*Command, bool) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter command: ")
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
if strings.ToLower(text) == "quit" {
return nil, true
}
return &Command{Text: text}, false
}