git.templeos.me/xultist/go-enry/v2@v2.0.0-20230215093429-6ef3e87f47c0/utils.go (about)

     1  package enry
     2  
     3  import (
     4  	"bytes"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/go-enry/go-enry/v2/data"
     9  	"github.com/go-enry/go-enry/v2/regex"
    10  )
    11  
    12  const binSniffLen = 8000
    13  
    14  var configurationLanguages = map[string]struct{}{
    15  	"XML":  {},
    16  	"JSON": {},
    17  	"TOML": {},
    18  	"YAML": {},
    19  	"INI":  {},
    20  	"SQL":  {},
    21  }
    22  
    23  // IsConfiguration tells if filename is in one of the configuration languages.
    24  func IsConfiguration(path string) bool {
    25  	language, _ := GetLanguageByExtension(path)
    26  	_, is := configurationLanguages[language]
    27  	return is
    28  }
    29  
    30  // IsImage tells if a given file is an image (PNG, JPEG or GIF format).
    31  func IsImage(path string) bool {
    32  	extension := filepath.Ext(path)
    33  	if extension == ".png" || extension == ".jpg" || extension == ".jpeg" || extension == ".gif" {
    34  		return true
    35  	}
    36  
    37  	return false
    38  }
    39  
    40  // GetMIMEType returns a MIME type of a given file based on its languages.
    41  func GetMIMEType(path string, language string) string {
    42  	if mime, ok := data.LanguagesMime[language]; ok {
    43  		return mime
    44  	}
    45  
    46  	if IsImage(path) {
    47  		return "image/" + filepath.Ext(path)[1:]
    48  	}
    49  
    50  	return "text/plain"
    51  }
    52  
    53  // IsDocumentation returns whether or not path is a documentation path.
    54  func IsDocumentation(path string) bool {
    55  	return matchRegexSlice(data.DocumentationMatchers, path)
    56  }
    57  
    58  // IsDotFile returns whether or not path has dot as a prefix.
    59  func IsDotFile(path string) bool {
    60  	base := filepath.Base(filepath.Clean(path))
    61  	return strings.HasPrefix(base, ".") && base != "."
    62  }
    63  
    64  // IsVendor returns whether or not path is a vendor path.
    65  func IsVendor(path string) bool {
    66  	return data.FastVendorMatcher.MatchString(path)
    67  }
    68  
    69  // IsTest returns whether or not path is a test path.
    70  func IsTest(path string) bool {
    71  	return matchRegexSlice(data.TestMatchers, path)
    72  }
    73  
    74  func matchRegexSlice(exprs []regex.EnryRegexp, str string) bool {
    75  	for _, expr := range exprs {
    76  		if expr.MatchString(str) {
    77  			return true
    78  		}
    79  	}
    80  
    81  	return false
    82  }
    83  
    84  // IsBinary detects if data is a binary value based on:
    85  // http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
    86  func IsBinary(data []byte) bool {
    87  	if len(data) > binSniffLen {
    88  		data = data[:binSniffLen]
    89  	}
    90  
    91  	if bytes.IndexByte(data, byte(0)) == -1 {
    92  		return false
    93  	}
    94  
    95  	return true
    96  }
    97  
    98  // GetColor returns a HTML color code of a given language.
    99  func GetColor(language string) string {
   100  	if color, ok := data.LanguagesColor[language]; ok {
   101  		return color
   102  	}
   103  
   104  	if color, ok := data.LanguagesColor[GetLanguageGroup(language)]; ok {
   105  		return color
   106  	}
   107  
   108  	return "#cccccc"
   109  }
   110  
   111  // IsGenerated returns whether the file with the given path and content is a
   112  // generated file.
   113  func IsGenerated(path string, content []byte) bool {
   114  	ext := strings.ToLower(filepath.Ext(path))
   115  	if _, ok := data.GeneratedCodeExtensions[ext]; ok {
   116  		return true
   117  	}
   118  
   119  	for _, m := range data.GeneratedCodeNameMatchers {
   120  		if m(path) {
   121  			return true
   122  		}
   123  	}
   124  
   125  	path = strings.ToLower(path)
   126  	for _, m := range data.GeneratedCodeMatchers {
   127  		if m(path, ext, content) {
   128  			return true
   129  		}
   130  	}
   131  
   132  	return false
   133  }