/
file.go
420 lines (375 loc) · 10.2 KB
/
file.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package library
import (
"bufio"
"fmt"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/js"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
// Initiate File Function
func InitiateFileFunction(flags Flag) {
/** Minify Files in Path .js and .css */
if *flags.Minify {
minifyFiles(*flags.Path)
}
/** Count Files Containing Text */
if *flags.File && *flags.Count && *flags.Text != "" {
count := CountFilesContainingText(*flags.Path, *flags.Text, *flags.Exclude)
fmt.Println("🐙 There are", count, "files containing", *flags.Text)
}
/** removeFilesExceptExtensions */
if *flags.File && *flags.Remove && len(*flags.Ext) > 0 {
RemoveFilesExceptExtensions(*flags.Path, *flags.Ext, *flags.Except)
}
/** remove Files Older than days matching regex */
if *flags.File && *flags.Remove && *flags.OlderThan && *flags.Days > 0 {
RemoveFilesOlderThan(*flags.Path, *flags.Regex, *flags.Days, *flags.Exclude, *flags.DryRun)
}
/** Delete Directory or Files in Path Matching Filename */
if *flags.Dir && *flags.Remove && len(*flags.Dirname) > 0 {
DeleteDirectoriesorFilesinPath(*flags.Path, *flags.Dirname, *flags.Filename)
}
if *flags.File && *flags.Remove && len(*flags.Filename) > 0 {
DeleteDirectoriesorFilesinPath(*flags.Path, *flags.Dirname, *flags.Filename)
}
/** Exctract Links from Directory Path */
if *flags.ExtractUrl {
urls, err := ExtractURLsFromDirectoryPath(*flags.Path, *flags.Url)
if err != nil {
fmt.Println("❌ Error extracting links:", err)
}
for _, url := range urls {
fmt.Println(url)
}
}
/** Search and Replace */
if *flags.SearchandReplace && *flags.From != "" && *flags.To != "" {
SearchandReplace(*flags.Path, *flags.From, *flags.To, -1)
}
}
// File Exist in Path.
func FileExistsInPath(filePath, directoryPath string) (bool, error) {
// Construct the full path to the file.
fullPath := filepath.Join(directoryPath, filePath)
// Check if the file exists.
_, err := os.Stat(fullPath)
if err == nil {
// File exists.
return true, nil
} else if os.IsNotExist(err) {
// File does not exist.
return false, nil
} else {
// An error occurred while accessing the file.
return false, err
}
}
/** Extract URLs from File */
func extractURLsFromFile(filePath string, baseURL string) ([]string, error) {
fileContent, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
// Define a regular expression to match URLs
urlPattern := `https?://\S+`
var urls []string
// Find URLs in the file content
urlRegex := regexp.MustCompile(urlPattern)
urlMatches := urlRegex.FindAllString(string(fileContent), -1)
// Filter URLs based on the baseURL if provided
if baseURL != "" {
for _, url := range urlMatches {
if strings.Contains(url, baseURL) {
urls = append(urls, url)
}
}
} else {
urls = append(urls, urlMatches...)
}
return urls, nil
}
/** Minify Files in Path .js and .css */
func minifyFiles(path string) {
if path == "" {
CurrentDirectory, _ := os.Getwd()
path = CurrentDirectory
}
m := minify.New()
m.AddFunc("text/javascript", js.Minify)
m.AddFunc("text/css", css.Minify)
filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("❌", err)
}
if info.IsDir() {
return nil
}
if filepath.Ext(filePath) == ".js" || filepath.Ext(filePath) == ".css" {
// Open the file
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
// Minify the file
var contentType string
if filepath.Ext(filePath) == ".js" {
contentType = "text/javascript"
} else {
contentType = "text/css"
}
// read the file
bs, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
// minify the content
minifiedContent, err := m.String(contentType, string(bs))
if err != nil {
panic(err)
}
// write the minified content to the file
err = ioutil.WriteFile(filePath, []byte(minifiedContent), 0644)
if err != nil {
panic(err)
}
}
return nil
})
fmt.Println("✅ Successfully minify files in", path)
}
/** Count Files Containing Text */
func CountFilesContainingText(path string, text string, exclude []string) int {
if path == "" {
CurrentDirectory, _ := os.Getwd()
path = CurrentDirectory
}
var count int
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("❌ Error:", err)
return err
}
// Check if the directory should be ignored
for _, excludeDir := range exclude {
if strings.Contains(path, excludeDir) {
return nil
}
}
if !info.IsDir() {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.Contains(scanner.Text(), text) {
count++
break
}
}
}
return nil
})
if err != nil {
fmt.Println("❌ Error:", err)
return 0
}
return count
}
/** Remove Files Except Specified Extensions */
func RemoveFilesExceptExtensions(root string, allowedExtensions []string, exception []string) error {
if root == "" {
CurrentDirectory, _ := os.Getwd()
root = CurrentDirectory
}
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("❌ ", err)
}
if !info.IsDir() {
ext := filepath.Ext(info.Name())
if !SliceContainsString(allowedExtensions, ext) && !SliceContainsString(exception, info.Name()) {
err := os.Remove(path)
if err != nil {
return err
}
fmt.Println("✅ Successfully remove files except extensions", allowedExtensions, "in", info.Name())
}
}
return nil
})
}
/** Remove Files Older Than */
func RemoveFilesOlderThan(path string, pattern string, retentionDays int, exclude []string, dryrun bool) error {
if path == "" {
CurrentDirectory, _ := os.Getwd()
path = CurrentDirectory
}
currentTime := time.Now()
return filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
// Check if the file exists
if err != nil {
fmt.Println("❌ ", err)
}
// Skip directories
if info.IsDir() {
return nil
}
// Check if the directory should be ignored
for _, excludeDir := range exclude {
if strings.Contains(filePath, excludeDir) {
return nil
}
}
if !info.ModTime().Add(time.Duration(retentionDays) * 24 * time.Hour).After(currentTime) {
// Match file name if pattern is not empty
matched := true
if pattern != "" {
matched, _ = filepath.Match(pattern, info.Name())
}
// Remove file if matched
if matched {
if dryrun {
fmt.Println("✅ Dry run, will remove", filePath)
} else {
os.Remove(filePath)
fmt.Println("✅ Successfully remove files older than", retentionDays, "days in", filePath)
}
}
}
return nil
})
}
/** Delete Directory or Files in Path Matching Filename */
func DeleteDirectoriesorFilesinPath(root string, dirnames []string, filenames []string) error {
if root == "" {
CurrentDirectory, _ := os.Getwd()
root = CurrentDirectory
}
// Walk through the directory tree
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("❌ ", err)
return nil
}
// If the path is a directory and it has the correct name, delete it
if SliceContainsString(dirnames, info.Name()) || SliceContainsString(filenames, info.Name()) {
err = os.RemoveAll(path)
if err != nil {
fmt.Println("❌ ", err)
return nil
}
if info.IsDir() {
fmt.Println("✅ Successfully remove directories nested by name", info.Name(), "in", root)
} else {
fmt.Println("✅ Successfully remove files nested by filename", info.Name(), "in", root)
}
} else if info.IsDir() {
// Check if the directory is empty
f, err := os.Open(path)
if err != nil {
fmt.Println("❌ ", err)
return nil
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == io.EOF {
// Directory is empty, so delete it
os.Remove(path)
}
}
return nil
})
}
/** Exctract URLs from Directory Path */
func ExtractURLsFromDirectoryPath(path string, baseURL string) ([]string, error) {
if path == "" {
// Use the current directory path if path is not provided
dir, err := os.Getwd()
if err != nil {
return nil, err
}
path = dir
}
uniqueURLs := make(map[string]struct{}) // Map to store unique URLs
// Check if the path is a directory
fileInfo, err := os.Stat(path)
if err != nil {
return nil, err
}
if !fileInfo.IsDir() {
return nil, fmt.Errorf("Path is not a directory: %s", path)
}
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, file := range files {
if file.IsDir() {
// If it's a subdirectory, recursively extract URLs
subpath := filepath.Join(path, file.Name())
subURLs, err := ExtractURLsFromDirectoryPath(subpath, baseURL)
if err != nil {
return nil, err
}
for _, url := range subURLs {
uniqueURLs[url] = struct{}{}
}
} else {
// If it's a file, extract URLs based on the file content
filePath := filepath.Join(path, file.Name())
fileURLs, err := extractURLsFromFile(filePath, baseURL)
if err != nil {
return nil, err
}
for _, url := range fileURLs {
uniqueURLs[url] = struct{}{}
}
}
}
// Convert unique URLs from the map to a slice
var urls []string
for url := range uniqueURLs {
// Cleaned unwanted symbols from the URL
symbolPattern := `[^\w://.]`
regex := regexp.MustCompile(symbolPattern)
cleaned := regex.ReplaceAllString(url, "")
// Add the URL to the slice
urls = append(urls, cleaned)
}
return urls, nil
}
/** Search and Replace in Directory or File */
func SearchandReplace(path string, from string, to string, limit int) {
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
newData := strings.Replace(string(data), from, to, limit)
err = ioutil.WriteFile(path, []byte(newData), 0644)
if err != nil {
return err
}
}
return nil
})
if err != nil {
fmt.Println("❌ ", err)
} else {
fmt.Println("✅ Success Search and Replace", from, "to", to, "in", path)
}
}