-
Notifications
You must be signed in to change notification settings - Fork 2
/
Functions_Universal.go
151 lines (129 loc) · 3.27 KB
/
Functions_Universal.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
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
package main
import (
"os"
"math/rand"
"strings"
"github.com/mattn/go-shellwords"
)
/*
This is a function library for universally relevant functions.
Functions specific to certain commands will be in the files associated with those commands.
*/
// Check if file exists at a given path, and that the path is valid
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return true, err
}
// Does array contain given string?
func contains(haystack []string, needle string) bool {
for _, str := range haystack {
if str == needle {
return true
}
}
return false
}
// copies an array of bytes to prevent it disappearing when the db is closed
func cpyBytes(s []byte) []byte{
r := []byte{}
for i:=0;i<len(s);i++ {
r = append(r,s[i])
}
return r
}
// generate a random string of n length
func RandString(n int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
// parse a string and return a bucket path. 'relativeTo' is the bucket the path is assumed to be relative to
func stringToPath(target string, relativeTo bckt) []string{
bp := []string{}
if target=="~"{
bp = []string{"~"}
} else if strings.HasPrefix(target,"./"){
// if the path is relative
bp = currentBucket.path
target = strings.Replace(target,"./","",1)
tmp := escapedSplit(target)
for i:=0;i<len(tmp);i++{
bp = append(bp,tmp[i] )
}
} else if strings.HasPrefix(target,"/")||strings.HasPrefix(target,"~/"){
// if the path is absolute
if strings.HasPrefix(target,"/"){
target = "~"+target
}
tmp := escapedSplit(target)
for i:=0;i<len(tmp);i++{
bp = append(bp,tmp[i] )
}
} else {
if relativeTo.exists(){
// assume the path is relative
bp = relativeTo.path
tmp := escapedSplit(target)
for i:=0;i<len(tmp);i++{
bp = append(bp,tmp[i] )
}
} else {
// assume the path is absolute
tmp := escapedSplit(target)
bp = append(bp,"~")
for i:=0;i<len(tmp);i++{
bp = append(bp,tmp[i] )
}
}
}
return bp
}
// split a string for bucket path, but allow escaping backslashes
func escapedSplit(s string) []string{
if !strings.Contains(s,"\\/"){
return strings.Split(s,"/")
}
rndm := RandString(10)
for i:=0;strings.Contains(s,rndm);i++{
rndm = RandString(10)
}
s = strings.Replace(s,"\\/",rndm,-1)
tmp := strings.Split(s,"/")
for i:=0;i<len(tmp);i++{
tmp[i]=strings.Replace(tmp[i],rndm,"/",-1)
}
return tmp
}
func plurality(singular string, plural string, count int) string{
if count==1{
return singular
}
return plural
}
func valPlural(count int) string{
return plurality("value","values",count)
}
func bcktPlural(count int) string{
return plurality("bucket","buckets",count)
}
func parseArguments(cmd []string, minArgs int) (args []string, returnCode int){
hasArgs := len(cmd)>1
if !hasArgs && minArgs<=0{
return []string{},1 // No arguments
} else if !hasArgs && minArgs>0{
return []string{},2 // Not enough arguments
}
args,err := shellwords.Parse(cmd[1])
if err!=nil{
return []string{},3 // Failed to parse
}
if len(args)<minArgs {
return []string{},2 // Not enough arguments
}
return args,0
}