-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscripts.go
More file actions
297 lines (251 loc) · 6.38 KB
/
scripts.go
File metadata and controls
297 lines (251 loc) · 6.38 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"
"os"
"sort"
"strings"
)
// Script holds all the data from a script
type Script struct {
// Path holds the path to the script
Path string
// Name holds the name of the script
Name string
// Desc holds the description for the script
Desc string
// Tags holds the tags for the script, such as OS, Desktop Enviroment, etc.
Tags string
// User saves the creator's name/nick
User string
//Hash holds a md5 of the last known state of the script
Hash string
}
// NewScript returns a new object of type Script
func NewScript() *Script { return &Script{} }
// SearchLocal will search on the path for valid scripts and load them onto the already loaded scripts
func SearchLocal(path string, scripts *[]Script) (count uint, err error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return
}
for _, f := range files {
isNew := true
for _, script := range *scripts {
if script.Path == path+"/"+f.Name() {
isNew = false
break
}
}
if isNew {
h, err := HashFile(path + "/" + f.Name())
if err != nil {
return count, err
}
count++
*scripts = append(*scripts, Script{
Path: path + "/" + f.Name(),
Hash: h})
}
}
return
}
// CheckLocal will check the local database of scripts.
// Will create a new one if it doesn't exists.
// Returns all the loaded data, plus info about the changes made
func CheckLocal(path string, scriptsPath string) (scripts []Script, totalValid uint, deleted uint, modified uint, newOnes uint, err error) {
// Read / create database
f, err := ioutil.ReadFile(path)
if err != nil {
if !os.IsNotExist(err) {
return
}
// database does not exist, create
f2, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
return scripts, totalValid, deleted, modified, newOnes, err
}
f2.WriteString("[{}]")
f2.Close()
f, err = ioutil.ReadFile(path)
if err != nil {
return scripts, totalValid, deleted, modified, newOnes, err
}
}
if err = json.Unmarshal(f, &scripts); err != nil {
return
}
// Check database integrity
for i, script := range scripts {
fE, hE, hash := script.CheckIntegrity()
if fE {
deleted++
scripts = append(scripts[:i], scripts[i+1:]...)
} else if !hE {
scripts[i].Hash = hash
modified++
totalValid++
} else {
totalValid++
}
}
// Look for new files
newOnes, err = SearchLocal(scriptsPath, &scripts)
if err != nil {
return
}
totalValid += newOnes
b, err := json.Marshal(scripts)
if err != nil {
return
}
f2, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
return
}
_, err = f2.Write(b)
if err != nil {
return
}
return
}
// HashFile will hash a file and return the hexsum
func HashFile(path string) (h string, err error) {
var result []byte
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
return
}
h = hex.EncodeToString(hash.Sum(result))
return
}
// GetName will return the name for the script, or it's path if it does not
// have a name assigned
func (s *Script) GetName() string {
if s.Name != "" {
return s.Name
}
return s.Path
}
// CheckIntegrity will load the file from the path and check it against known data.
func (s *Script) CheckIntegrity() (fileErr bool, hashEq bool, h string) {
h, err := HashFile(s.Path)
if err == nil {
fileErr = false
if h != s.Hash {
hashEq = false
} else {
hashEq = true
}
} else {
fileErr = true
hashEq = false
}
return
}
// Equals will check if the scripts are the same object
func (s *Script) Equals(s2 Script) bool { return s.Hash == s2.Hash }
// Save will save the data to the json database
func Save(path string, scripts []Script) {
b, err := json.Marshal(scripts)
if err != nil {
l.Println(err.Error())
return
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
l.Println(err.Error())
return
}
defer f.Close()
f.Write(b)
}
/*** SORT FUNCTIONS ***/
// Scripts is a dummy type for qsort
type Scripts []Script
func (s Scripts) Len() int {
return len(s)
}
func (s Scripts) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s Scripts) Less(i, j int) bool {
return s[i].GetName() < s[j].GetName()
}
/*** END SORT FUNCTIONS***/
// SortScripts sorts the slice based on name and path
func SortScripts(scripts Scripts) []Script {
sort.Sort(scripts)
return scripts
}
// ListByName will return all scripts which contains the name on it
// An empty string is interpreted as "any".
// This function IS NOT case sensitive.
func ListByName(scripts []Script, name string) (matches []Script) {
for _, script := range scripts {
if strings.Contains(strings.ToLower(script.Name), strings.ToLower(name)) {
matches = append(matches, script)
}
}
return
}
// ListByUser will return all scripts that were created by a user containing the string.
// An empty string is interpreted as "any".
// This function IS NOT case sensitive.
func ListByUser(scripts []Script, user string) (matches []Script) {
for _, script := range scripts {
if strings.Contains(strings.ToLower(script.User), strings.ToLower(user)) {
matches = append(matches, script)
}
}
return
}
// ListByTags will return all scripts that contain a tag which contains the passed keyword
// An empty string is interpreted as "any".
// This function IS NOT case sensitive.
func ListByTags(scripts []Script, inTag string) (matches []Script) {
for _, script := range scripts {
if strings.Contains(strings.ToLower(script.Tags), strings.ToLower(inTag)) {
matches = append(matches, script)
}
}
return
}
// ListByDesc will return all scripts that has a description containing a string
// An empty string is interpreted as "any".
// This function IS NOT case sensitive.
func ListByDesc(scripts []Script, desc string) (matches []Script) {
for _, script := range scripts {
if strings.Contains(strings.ToLower(script.Desc), strings.ToLower(desc)) {
matches = append(matches, script)
}
}
return
}
// TrimRepeated will return an alice formed from the passed one,
// but the result will only include unique values.
func TrimRepeated(scripts []Script) (valid []Script) {
for _, s := range scripts {
l.Println(scripts, valid)
eq := false
for _, v := range valid {
if s.Equals(v) {
eq = true
break
}
}
if !eq {
valid = append(valid, s)
}
}
return
}