tartrazine/cli/slinguist/main.go

75 lines
1.2 KiB
Go
Raw Normal View History

2016-07-13 17:05:23 +00:00
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"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.Parse()
2016-07-13 22:38:06 +00:00
root, err := filepath.Abs(flag.Arg(0))
ifError(err)
if root == "" {
usage()
}
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 {
return err
}
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
}
l, safe := slinguist.GetLanguageByExtension(path)
if !safe {
content, _ := ioutil.ReadFile(path)
l, safe = slinguist.GetLanguageByContent(path, content)
}
2016-07-13 22:38:06 +00:00
r, _ := filepath.Rel(root, path)
o[l] = append(o[l], r)
2016-07-13 17:05:23 +00:00
return nil
})
2016-07-13 22:38:06 +00:00
ifError(err)
2016-07-13 17:05:23 +00:00
js, _ := json.MarshalIndent(o, "", " ")
fmt.Printf("%s\n", js)
}
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()
os.Exit(2)
}
func ifError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(2)
}
}