You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.go 2.9KB

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