tartrazine/cli/slinguist/main.go

84 lines
1.3 KiB
Go
Raw Normal View History

2016-07-13 17:05:23 +00:00
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
2016-07-13 17:05:23 +00:00
"os"
"path/filepath"
2017-04-05 16:26:58 +00:00
"gopkg.in/src-d/simple-linguist.v1"
2016-07-13 17:05:23 +00:00
)
func main() {
flag.Usage = usage
2016-07-13 17:05:23 +00:00
flag.Parse()
2016-07-13 22:38:06 +00:00
root, err := filepath.Abs(flag.Arg(0))
if err != nil {
log.Fatal(err)
2016-07-13 22:38:06 +00:00
}
2016-07-13 17:05:23 +00:00
errors := false
2016-07-13 17:05:23 +00:00
o := make(map[string][]string, 0)
2016-07-13 22:38:06 +00: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-13 22:38:06 +00:00
}
2016-07-13 17:05:23 +00:00
if slinguist.IsVendor(f.Name()) || slinguist.IsDotFile(f.Name()) {
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 17:05:23 +00:00
l := slinguist.GetLanguage(filepath.Base(path), content)
r, err := filepath.Rel(root, path)
if err != nil {
errors = true
log.Println(err)
return nil
2016-07-13 17:05:23 +00:00
}
2016-07-13 22:38:06 +00:00
o[l] = append(o[l], r)
2016-07-13 17:05:23 +00:00
return nil
})
if err != nil {
log.Fatal(err)
}
2016-07-13 22:38:06 +00:00
2016-07-13 17:05:23 +00:00
js, _ := json.MarshalIndent(o, "", " ")
fmt.Printf("%s\n", js)
if errors {
os.Exit(2)
}
2016-07-13 17:05:23 +00:00
}
2016-07-13 22:38:06 +00:00
func usage() {
fmt.Fprintf(
os.Stderr, "simple-linguist, A simple (and faster) implementation of linguist \nusage: %s <path>\n",
os.Args[0],
)
flag.PrintDefaults()
}