-
Notifications
You must be signed in to change notification settings - Fork 1
/
gloot.go
123 lines (115 loc) · 2.88 KB
/
gloot.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
package gloot
import (
"io"
"io/ioutil"
"os"
"strings"
"github.com/alexmullins/zip"
)
// Keyz global string array to track files
var Keyz []string
var ignoreNames = []string{}
var ignoreContent = []string{}
var includeNames = []string{}
var includeContent = []string{}
// Searcher is a public function designed to be called as a package
func Searcher(pathToDir string, igNames, igContent, inNames, inContent []string) []string {
ignoreNames = igNames
ignoreContent = igContent
includeNames = inNames
includeContent = inContent
// Start recursive search
searchForFiles(pathToDir)
if Keyz != nil {
return Keyz
}
return nil
}
// searchForFiles is a private function that recurses through directories, running our searchFileForCriteria function on every file
func searchForFiles(pathToDir string) {
files, err := ioutil.ReadDir(pathToDir)
if err != nil {
return
}
for _, file := range files {
if stringLooper(file.Name(), ignoreNames) {
} else {
if file.IsDir() {
dirName := file.Name() + "/"
fullPath := strings.Join([]string{pathToDir, dirName}, "")
searchForFiles(fullPath)
} else {
if searchFileForCriteria(pathToDir, file.Name()) {
fullPath := strings.Join([]string{pathToDir, file.Name()}, "")
Keyz = append(Keyz, fullPath)
}
}
}
}
}
func searchFileForCriteria(pathToDir, fileName string) bool {
fullPath := strings.Join([]string{pathToDir, fileName}, "")
if stringLooper(fullPath, includeNames) {
return true
}
fileData, _ := ioutil.ReadFile(fullPath)
fileLines := strings.Split(string(fileData), "\n")
for _, line := range fileLines {
if stringLooper(line, ignoreContent) {
return false
}
if stringLooper(line, includeContent) {
return true
}
}
return false
}
// A function to loop over our string slices and match any of our globally defined content
func stringLooper(target string, list []string) bool {
for _, loot := range list {
if strings.Contains(target, loot) {
return true
}
}
return false
}
// ZipFiles compresses one or many files into a single zip archive file
func ZipFiles(filename string, files []string, encryptPassword string) error {
newfile, err := os.Create(filename)
if err != nil {
return err
}
defer newfile.Close()
zipWriter := zip.NewWriter(newfile)
defer zipWriter.Close()
encryptedWriter, err := zipWriter.Encrypt("", encryptPassword)
if err != nil {
return err
}
encryptedZipWriter := zip.NewWriter(encryptedWriter)
for _, file := range files {
zipfile, err := os.Open(file)
if err != nil {
return err
}
defer zipfile.Close()
info, err := zipfile.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Method = zip.Deflate
writer, err := encryptedZipWriter.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, zipfile)
if err != nil {
return err
}
}
return nil
}