forked from gobuffalo/genny
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
48 lines (42 loc) · 877 Bytes
/
helpers.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
package genny
import (
"path/filepath"
"strings"
)
func exts(f File) []string {
var exts []string
name := f.Name()
ext := filepath.Ext(name)
for ext != "" {
exts = append([]string{ext}, exts...)
name = strings.TrimSuffix(name, ext)
ext = filepath.Ext(name)
}
return exts
}
// HasExt checks if a file has ANY of the
// extensions passed in. If no extensions
// are given then `true` is returned
func HasExt(f File, ext ...string) bool {
if len(ext) == 0 || ext == nil {
return true
}
for _, xt := range ext {
xt = strings.TrimSpace(xt)
if xt == "*" || xt == "*.*" {
return true
}
for _, x := range exts(f) {
if x == xt {
return true
}
}
}
return false
}
// StripExt from a File and return a new one
func StripExt(f File, ext string) File {
name := f.Name()
name = strings.Replace(name, ext, "", -1)
return NewFile(name, f)
}