go-enry/cli/enry/main.go

142 lines
2.7 KiB
Go
Raw Normal View History

2016-07-13 19:05:23 +02:00
package main
import (
"bytes"
2016-07-13 19:05:23 +02:00
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
2016-07-13 19:05:23 +02:00
"os"
"path/filepath"
2017-06-08 09:27:27 +02:00
"gopkg.in/src-d/enry.v1"
2016-07-13 19:05:23 +02:00
)
func main() {
flag.Usage = usage
breakdownFlag := flag.Bool("breakdown", false, "")
jsonFlag := flag.Bool("json", false, "")
2016-07-13 19:05:23 +02:00
flag.Parse()
2016-07-14 00:38:06 +02:00
root, err := filepath.Abs(flag.Arg(0))
if err != nil {
log.Fatal(err)
2016-07-14 00:38:06 +02:00
}
2016-07-13 19:05:23 +02:00
errors := false
out := make(map[string][]string, 0)
2016-07-14 00:38:06 +02:00
err = filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
if err != nil {
errors = true
log.Println(err)
return filepath.SkipDir
2016-07-14 00:38:06 +02:00
}
relativePath, err := filepath.Rel(root, path)
if err != nil {
errors = true
log.Println(err)
return nil
}
if relativePath == "." {
return nil
}
if f.IsDir() {
relativePath = relativePath + "/"
}
2017-06-13 13:56:07 +02:00
if enry.IsVendor(relativePath) || enry.IsDotFile(relativePath) ||
enry.IsDocumentation(relativePath) || enry.IsConfiguration(relativePath) {
2016-07-13 19:05:23 +02:00
if f.IsDir() {
return filepath.SkipDir
}
return nil
}
if f.IsDir() {
return nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
errors = true
log.Println(err)
return nil
}
2016-07-13 19:05:23 +02:00
2017-06-13 13:56:07 +02:00
language := enry.GetLanguage(filepath.Base(path), content)
if language == enry.OtherLanguage {
return nil
2016-07-13 19:05:23 +02:00
}
out[language] = append(out[language], relativePath)
2016-07-13 19:05:23 +02:00
return nil
})
if err != nil {
log.Fatal(err)
}
2016-07-14 00:38:06 +02:00
var buff bytes.Buffer
switch {
case *jsonFlag && !*breakdownFlag:
printJson(out, &buff)
case *jsonFlag && *breakdownFlag:
printBreakDown(out, &buff)
case *breakdownFlag:
printPercents(out, &buff)
buff.WriteByte('\n')
printBreakDown(out, &buff)
default:
printPercents(out, &buff)
}
fmt.Print(buff.String())
2016-07-13 19:05:23 +02:00
}
2016-07-14 00:38:06 +02:00
func usage() {
fmt.Fprintf(
os.Stderr, "enry, A simple (and faster) implementation of github/linguist \nusage: %s <path>\n %s <path> [-json] [-breakdown]\n %s [-json] [-breakdown]\n",
os.Args[0], os.Args[0], os.Args[0],
2016-07-14 00:38:06 +02:00
)
}
func printBreakDown(out map[string][]string, buff *bytes.Buffer) {
for name, language := range out {
writeStringLn(name, buff)
for _, file := range language {
writeStringLn(file, buff)
}
writeStringLn("", buff)
}
}
func printJson(out map[string][]string, buff *bytes.Buffer) {
data, _ := json.Marshal(out)
buff.Write(data)
}
func printPercents(out map[string][]string, buff *bytes.Buffer) {
fileCount := make(map[string]int, len(out))
total := 0
for name, language := range out {
fileCount[name] = len(language)
total += len(language)
}
for name, count := range fileCount {
percent := float32(count) / float32(total) * 100
buff.WriteString(fmt.Sprintf("%.2f%% %s\n", percent, name))
}
}
2016-07-14 00:38:06 +02:00
func writeStringLn(s string, buff *bytes.Buffer) {
buff.WriteString(s)
buff.WriteByte('\n')
2016-07-14 00:38:06 +02:00
}