-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (96 loc) · 2.18 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
var commands []string = os.Args
if len(commands) == 1 {
log.Fatal("Please, add a desired pattern and/or a flag")
}
var pattern string = os.Args[1]
home, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
}
if len(commands) == 2 {
for _, match := range basicSearch(home, pattern) {
fmt.Println(match)
}
} else if len(commands) == 3 {
switch commands[2] {
case "-r":
for _, match := range filterRegex(basicSearch(home, "*"), pattern) {
fmt.Println(match)
}
case "-c":
for _, match := range filterContains(basicSearch(home, "*"), pattern) {
fmt.Println(match)
}
}
} else {
fmt.Println("Please, add a desired pattern and/or a flag")
}
}
func basicSearch(dir string, pattern string) []string {
var result []string
// get files in current directory
matches, err := filepath.Glob(filepath.Join(dir, pattern))
if err != nil {
fmt.Println(err)
}
for _, match := range matches {
result = append(result, match)
}
// get files in sub-directories
var folders []string = listFolders(dir)
if len(folders) > 0 {
for _, folder := range folders {
for _, subfile := range basicSearch(folder, pattern) {
result = append(result, subfile)
}
}
}
return result
}
func listFolders(dir string) []string {
var dirs []string
files, err := os.ReadDir(dir)
if err != nil {
fmt.Println(err)
}
for _, file := range files {
if file.IsDir() {
dirs = append(dirs, filepath.Join(dir, file.Name()))
}
}
return dirs
}
func filterRegex(files []string, pattern string) []string {
pattern = strings.Replace(pattern, "'", "", -1)
r, _ := regexp.Compile(pattern)
var result []string
for _, file := range files {
var a []string = strings.Split(file, "/")
var chunk string = a[len(a)-1]
if r.MatchString(chunk) {
result = append(result, file)
}
}
return result
}
func filterContains(files []string, pattern string) []string {
var result []string
for _, file := range files {
var a []string = strings.Split(file, "/")
var chunk string = a[len(a)-1]
if strings.Contains(chunk, pattern) {
result = append(result, file)
}
}
return result
}