go-enry/utils.go

134 lines
2.9 KiB
Go
Raw Normal View History

2017-06-13 13:56:07 +02:00
package enry
2016-07-13 19:05:09 +02:00
import (
"bytes"
"path/filepath"
"strings"
2020-03-19 17:31:29 +01:00
"github.com/go-enry/go-enry/v2/data"
"github.com/go-enry/go-enry/v2/regex"
2016-07-13 19:05:09 +02:00
)
const binSniffLen = 8000
2017-05-29 10:05:16 +02:00
var configurationLanguages = map[string]struct{}{
"XML": {},
"JSON": {},
"TOML": {},
"YAML": {},
"INI": {},
"SQL": {},
2016-07-18 16:20:12 +02:00
}
// IsConfiguration tells if filename is in one of the configuration languages.
2016-07-13 22:21:18 +02:00
func IsConfiguration(path string) bool {
2017-06-12 13:42:20 +02:00
language, _ := GetLanguageByExtension(path)
_, is := configurationLanguages[language]
2016-07-13 22:21:18 +02:00
return is
}
// IsImage tells if a given file is an image (PNG, JPEG or GIF format).
2017-07-11 12:27:48 +02:00
func IsImage(path string) bool {
extension := filepath.Ext(path)
2017-07-11 11:13:49 +02:00
if extension == ".png" || extension == ".jpg" || extension == ".jpeg" || extension == ".gif" {
return true
}
2017-07-11 12:27:48 +02:00
2017-07-11 11:13:49 +02:00
return false
}
// GetMIMEType returns a MIME type of a given file based on its languages.
func GetMIMEType(path string, language string) string {
if mime, ok := data.LanguagesMime[language]; ok {
return mime
}
2017-07-11 12:27:48 +02:00
if IsImage(path) {
return "image/" + filepath.Ext(path)[1:]
}
return "text/plain"
2017-07-10 12:50:52 +02:00
}
// IsDocumentation returns whether or not path is a documentation path.
func IsDocumentation(path string) bool {
return matchRegexSlice(data.DocumentationMatchers, path)
}
// IsDotFile returns whether or not path has dot as a prefix.
func IsDotFile(path string) bool {
base := filepath.Base(filepath.Clean(path))
return strings.HasPrefix(base, ".") && base != "."
}
// IsVendor returns whether or not path is a vendor path.
func IsVendor(path string) bool {
return matchRegexSlice(data.VendorMatchers, path)
}
2016-07-13 22:21:18 +02:00
2020-04-06 16:23:48 +02:00
// IsTest returns whether or not path is a test path.
func IsTest(path string) bool {
return matchRegexSlice(data.TestMatchers, path)
2020-04-06 16:23:48 +02:00
}
2017-05-29 10:05:16 +02:00
// IsBinary detects if data is a binary value based on:
// http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
2016-07-13 22:21:18 +02:00
func IsBinary(data []byte) bool {
if len(data) > binSniffLen {
data = data[:binSniffLen]
2016-07-13 22:21:18 +02:00
}
if bytes.IndexByte(data, byte(0)) == -1 {
return false
}
return true
}
// GetColor returns a HTML color code of a given language.
func GetColor(language string) string {
if color, ok := data.LanguagesColor[language]; ok {
return color
}
if color, ok := data.LanguagesColor[GetLanguageGroup(language)]; ok {
return color
}
return "#cccccc"
}
func matchRegexSlice(exprs []regex.EnryRegexp, str string) bool {
for _, expr := range exprs {
if expr.MatchString(str) {
return true
}
}
return false
}
// IsGenerated returns whether the file with the given path and content is a
// generated file.
func IsGenerated(path string, content []byte) bool {
ext := strings.ToLower(filepath.Ext(path))
if _, ok := data.GeneratedCodeExtensions[ext]; ok {
return true
}
for _, m := range data.GeneratedCodeNameMatchers {
if m(path) {
return true
}
}
path = strings.ToLower(path)
for _, m := range data.GeneratedCodeMatchers {
if m(path, ext, content) {
return true
}
}
return false
}