2016-07-13 17:05:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-04-18 11:02:46 +00:00
|
|
|
"log"
|
2016-07-13 17:05:23 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-06-08 07:27:27 +00:00
|
|
|
"gopkg.in/src-d/enry.v1"
|
2016-07-13 17:05:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-04-18 11:02:46 +00:00
|
|
|
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))
|
2017-04-18 11:02:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2016-07-13 22:38:06 +00:00
|
|
|
}
|
2016-07-13 17:05:23 +00:00
|
|
|
|
2017-04-18 11:02:46 +00:00
|
|
|
errors := false
|
2017-06-02 10:41:39 +00:00
|
|
|
out := 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 {
|
2017-04-18 11:02:46 +00:00
|
|
|
errors = true
|
|
|
|
log.Println(err)
|
|
|
|
return filepath.SkipDir
|
2016-07-13 22:38:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:41:39 +00: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 11:56:07 +00:00
|
|
|
if enry.IsVendor(relativePath) || enry.IsDotFile(relativePath) ||
|
|
|
|
enry.IsDocumentation(relativePath) || enry.IsConfiguration(relativePath) {
|
2016-07-13 17:05:23 +00:00
|
|
|
if f.IsDir() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-18 11:02:46 +00:00
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
errors = true
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-13 17:05:23 +00:00
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
language := enry.GetLanguage(filepath.Base(path), content)
|
|
|
|
if language == enry.OtherLanguage {
|
2017-04-18 11:02:46 +00:00
|
|
|
return nil
|
2016-07-13 17:05:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:41:39 +00:00
|
|
|
out[language] = append(out[language], relativePath)
|
2016-07-13 17:05:23 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2017-04-18 11:02:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-07-13 22:38:06 +00:00
|
|
|
|
2017-06-02 10:41:39 +00:00
|
|
|
data, _ := json.MarshalIndent(out, "", " ")
|
|
|
|
fmt.Printf("%s\n", data)
|
2017-04-18 11:02:46 +00:00
|
|
|
|
|
|
|
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(
|
2017-06-14 10:43:26 +00:00
|
|
|
os.Stderr, "enry, A simple (and faster) implementation of github/linguist \nusage: %s <path>\n",
|
2016-07-13 22:38:06 +00:00
|
|
|
os.Args[0],
|
|
|
|
)
|
|
|
|
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|