-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.go
More file actions
75 lines (63 loc) · 1.63 KB
/
stdlib.go
File metadata and controls
75 lines (63 loc) · 1.63 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
package stdlib
import (
"fmt"
"os"
"path/filepath"
"strings"
)
const (
PS = string(os.PathSeparator)
)
// FileExtChecker A store for file extensions to exclude and included.
//
// Deprecated This was used long ago but no longer needed and should be removed.
// Originally written for tmptoapp now named tmplpress.
type FileExtChecker struct {
excludes *[]string
includes *[]string
}
// NewFileExtChecker Initialize a new FileExtChecker instance.
// Deprecated This was used long ago but no longer needed and should be removed.
// Originally written for tmptoapp now named tmplpress.
func NewFileExtChecker(el, in *[]string) (*FileExtChecker, error) {
var err error
if el == nil && in == nil {
return nil, fmt.Errorf("you did not add provide any file extensions to include or exclude")
}
if el == nil {
el = &[]string{}
}
if in == nil {
in = &[]string{}
}
fce := FileExtChecker{
excludes: el,
includes: in,
}
return &fce, err
}
// IsValid Returns true for files that match allowed or excluded extensions.
// Passing a full path only checks the basename.
// Default to include all files.
// If a file has no extension, then use its basename.
func (fec *FileExtChecker) IsValid(file string) bool {
basename := filepath.Base(file) // account for hidden directories
ext := strings.Trim(filepath.Ext(basename), ".")
// when there is no extension (unix/linux/mac) use the basename
if len(ext) == 0 && len(basename) > 1 {
ext = basename
}
if ext != "" {
for _, t := range *fec.excludes {
if t == ext {
return false
}
}
for _, t := range *fec.includes {
if t == ext {
return true
}
}
}
return true
}