-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_test.go
99 lines (80 loc) · 1.68 KB
/
find_test.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
package find
import (
"context"
"fmt"
"log"
"os"
"time"
)
func ExampleFind() {
ctx, cancel := context.WithDeadline(
context.Background(),
time.Now().Add(5*time.Minute),
)
defer cancel()
where := "path/to/the/source"
// Since recursive search does not active by default,
// Find will search for files and folders only in the
// root of where.
results, err := Find(ctx, where, "*template*")
if err != nil {
log.Println(err)
return
}
for _, r := range results {
fmt.Println(r)
}
}
func ExampleFind_withOptions() {
ctx, cancel := context.WithDeadline(
context.Background(),
time.Now().Add(5*time.Minute),
)
defer cancel()
where := "path/to/the/source"
// Results will contain only folders searched recursively.
results, err := Find(
ctx,
where,
"*template*",
Only(Folder),
Recursively,
)
if err != nil {
log.Println(err)
return
}
for _, r := range results {
fmt.Println(r)
}
}
func ExampleTemplate_standalone() {
template := NewTemplate("*custom*&*template*")
// Can be any string slice, resulted from different sources.
s, err := os.ReadDir("some/folder")
if err != nil {
log.Fatalln(err)
}
for _, el := range s {
if template.Match(el.Name()) {
// Do something here...
}
}
}
func ExampleTemplates() {
ts := []string{"*this*", "*that*"}
templates := NewTemplates(ts)
// Can be any string slice, resulted from different sources.
s, err := os.ReadDir("some/folder")
if err != nil {
log.Fatalln(err)
}
for _, el := range s {
if MatchAny(templates, el.Name()) {
// Do something here if match any of the templates...
}
if MatchAll(templates, el.Name()) {
// Do something here if match all of the templates...
}
}
}