command improvements

This commit is contained in:
Máximo Cuadros
2016-07-14 00:38:06 +02:00
parent 947a0d3d44
commit e0a990e4ea

View File

@@ -13,10 +13,19 @@ import (
func main() { func main() {
flag.Parse() flag.Parse()
root := flag.Arg(0) root, err := filepath.Abs(flag.Arg(0))
ifError(err)
if root == "" {
usage()
}
o := make(map[string][]string, 0) o := make(map[string][]string, 0)
filepath.Walk(root, func(path string, f os.FileInfo, err error) error { err = filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if slinguist.IsVendor(f.Name()) || slinguist.IsDotFile(f.Name()) { if slinguist.IsVendor(f.Name()) || slinguist.IsDotFile(f.Name()) {
if f.IsDir() { if f.IsDir() {
return filepath.SkipDir return filepath.SkipDir
@@ -36,10 +45,30 @@ func main() {
} }
o[l] = append(o[l], path) r, _ := filepath.Rel(root, path)
o[l] = append(o[l], r)
return nil return nil
}) })
ifError(err)
js, _ := json.MarshalIndent(o, "", " ") js, _ := json.MarshalIndent(o, "", " ")
fmt.Printf("%s\n", js) fmt.Printf("%s\n", js)
} }
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)
}
}