slinguist cli uses GetLanguage

cli handles error returned by filepath.Walk
This commit is contained in:
Manuel Carmona 2017-04-18 13:02:46 +02:00
parent 1b8d51419d
commit a029941876

View File

@ -5,6 +5,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"path/filepath" "path/filepath"
@ -12,18 +13,20 @@ import (
) )
func main() { func main() {
flag.Usage = usage
flag.Parse() flag.Parse()
root, err := filepath.Abs(flag.Arg(0)) root, err := filepath.Abs(flag.Arg(0))
ifError(err) if err != nil {
log.Fatal(err)
if root == "" {
usage()
} }
errors := false
o := make(map[string][]string, 0) o := make(map[string][]string, 0)
err = 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 { if err != nil {
return err errors = true
log.Println(err)
return filepath.SkipDir
} }
if slinguist.IsVendor(f.Name()) || slinguist.IsDotFile(f.Name()) { if slinguist.IsVendor(f.Name()) || slinguist.IsDotFile(f.Name()) {
@ -38,22 +41,36 @@ func main() {
return nil return nil
} }
l, safe := slinguist.GetLanguageByExtension(path) content, err := ioutil.ReadFile(path)
if !safe { if err != nil {
content, _ := ioutil.ReadFile(path) errors = true
l, safe = slinguist.GetLanguageByContent(path, content) log.Println(err)
return nil
}
l := slinguist.GetLanguage(path, content)
r, err := filepath.Rel(root, path)
if err != nil {
errors = true
log.Println(err)
return nil
} }
r, _ := filepath.Rel(root, path)
o[l] = append(o[l], r) o[l] = append(o[l], r)
return nil return nil
}) })
ifError(err) if err != nil {
log.Fatal(err)
}
js, _ := json.MarshalIndent(o, "", " ") js, _ := json.MarshalIndent(o, "", " ")
fmt.Printf("%s\n", js) fmt.Printf("%s\n", js)
if errors {
os.Exit(2)
}
} }
func usage() { func usage() {
@ -63,12 +80,4 @@ func usage() {
) )
flag.PrintDefaults() flag.PrintDefaults()
os.Exit(2)
}
func ifError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(2)
}
} }