/
Command_Copy.go
68 lines (50 loc) · 1.16 KB
/
Command_Copy.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
package main
import (
"fmt"
)
func copy(cmd []string, del bool){
args,r := parseArguments(cmd,2)
if r==2{
fmt.Println("[Error] You must enter both a target and destination key")
return
} else if r==3{
fmt.Println("[Error] Couldn't parse arguments")
return
}
path1 := stringToPath(args[0],currentBucket)
path2 := stringToPath(args[1],currentBucket)
k1 := path1[len(path1)-1]
k2 := path2[len(path2)-1]
if len(path1)<2 || len(path2)<2{
fmt.Println("[Error] Invalid path(s) entered [length error]")
return
}
pb1 := bckt{path1[:len(path1)-1]}
pb2 := bckt{path2[:len(path2)-1]}
if !pb1.exists() {
fmt.Printf("[Error] Source bucket (%s) doesn't exist\n",path1)
return
}
if !pb2.exists() {
fmt.Printf("[Error] Destination bucket (%s) doesn't exist\n",path2)
}
_,e2 := pb2.getOne(k2)
if e2{
fmt.Println("[Error] Destination key already exists")
return
}
v1,e1 := pb1.getOne(k1)
if !e1{
fmt.Println("[Error] Source key doesn't exist")
return
}
if v1.isBucket(){
fmt.Println("[Error] Source is a bucket. Copying buckets is not yet supported")
return
} else {
pb2.insert([]byte(k2),v1.v)
}
if del{
pb1.delete(v1.k)
}
}