-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_extension.go
68 lines (53 loc) · 1.45 KB
/
file_extension.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
package check
import (
"fmt"
"path/filepath"
"strings"
)
const (
FileExtensionHtmlMarkdown = `.html.markdown`
FileExtensionHtmlMd = `.html.md`
FileExtensionMarkdown = `.markdown`
FileExtensionMd = `.md`
)
var ValidLegacyFileExtensions = []string{
FileExtensionHtmlMarkdown,
FileExtensionHtmlMd,
FileExtensionMarkdown,
FileExtensionMd,
}
var ValidRegistryFileExtensions = []string{
FileExtensionMd,
}
func LegacyFileExtensionCheck(path string) error {
if !FilePathEndsWithExtensionFrom(path, ValidLegacyFileExtensions) {
return fmt.Errorf("file does not end with a valid extension, valid extensions: %v", ValidLegacyFileExtensions)
}
return nil
}
func RegistryFileExtensionCheck(path string) error {
if !FilePathEndsWithExtensionFrom(path, ValidRegistryFileExtensions) {
return fmt.Errorf("file does not end with a valid extension, valid extensions: %v", ValidRegistryFileExtensions)
}
return nil
}
func FilePathEndsWithExtensionFrom(path string, validExtensions []string) bool {
for _, validExtension := range validExtensions {
if strings.HasSuffix(path, validExtension) {
return true
}
}
return false
}
// TrimFileExtension removes file extensions including those with multiple periods.
func TrimFileExtension(path string) string {
filename := filepath.Base(path)
if filename == "." {
return ""
}
dotIndex := strings.IndexByte(filename, '.')
if dotIndex > 0 {
return filename[:dotIndex]
}
return filename
}