/
detect_language.go
49 lines (43 loc) · 993 Bytes
/
detect_language.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
package build
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/go-enry/go-enry/v2"
)
type Language int
const (
GO Language = iota
Python Language = 1
Unknown
)
// detectLanguage provides detecting of programming language
func detectLanguage(path string) (Language, error) {
d, err := readFile(path)
if err != nil {
return Unknown, fmt.Errorf("unable to read file: %v", err)
}
langs := enry.GetLanguagesByExtension(filepath.Base(path), d, nil)
if len(langs) > 1 {
return Unknown, fmt.Errorf("unable to detect one language: %v", langs)
}
switch langs[0] {
case "Go":
return GO, nil
case "Python":
return Python, nil
}
return Unknown, fmt.Errorf("unknown language")
}
func readFile(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("unable to open file: %v", err)
}
d, err := ioutil.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("unable to read data from file: %v", err)
}
return d, nil
}