Merge pull request #73 from dpaz/issue72

Cli to analyze a single file
This commit is contained in:
Alfredo Beaumont
2017-07-20 13:40:56 +02:00
committed by GitHub
9 changed files with 557 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import (
"log"
"os"
"path/filepath"
"strings"
"gopkg.in/src-d/enry.v1"
"gopkg.in/src-d/enry.v1/data"
@ -48,6 +49,7 @@ func main() {
}
if relativePath == "." {
fmt.Print(printFileAnalysis(root))
return nil
}
@ -154,6 +156,44 @@ func printPercents(out map[string][]string, buff *bytes.Buffer) {
}
}
func printFileAnalysis(file string) string {
content, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println(err)
}
totalLines, nonBlank := getLines(file, string(content))
fileType := getFileType(file, content)
language := enry.GetLanguage(file, content)
mimeType := enry.GetMimeType(file, language)
return fmt.Sprintf(
`%s: %d lines (%d sloc)
type: %s
mime_type: %s
language: %s
`,
filepath.Base(file), totalLines, nonBlank, fileType, mimeType, language,
)
}
func getLines(file string, content string) (int, int) {
totalLines := strings.Count(content, "\n")
nonBlank := totalLines - strings.Count(content, "\n\n")
return totalLines, nonBlank
}
func getFileType(file string, content []byte) string {
switch {
case enry.IsImage(file):
return "Image"
case enry.IsBinary(content):
return "Binary"
default:
return "Text"
}
}
func writeStringLn(s string, buff *bytes.Buffer) {
buff.WriteString(s)
buff.WriteByte('\n')