0
0

Use filepath.Walk instead of rolling our own func

This commit is contained in:
Silvan Jegen 2015-10-13 21:04:51 +02:00
parent 08572e4925
commit 0ef988a318

View File

@ -20,9 +20,12 @@ import (
_ "github.com/blevesearch/bleve/config" _ "github.com/blevesearch/bleve/config"
) )
var indexPath = flag.String("index", "", "index path") var (
var keepExt = flag.Bool("keepExt", false, "keep extension in doc id") indexPath = flag.String("index", "", "index path")
var keepDir = flag.Bool("keepDir", false, "keep dir in doc id") keepExt = flag.Bool("keepExt", false, "keep extension in doc id")
keepDir = flag.Bool("keepDir", false, "keep dir in doc id")
rv chan file
)
func main() { func main() {
@ -72,12 +75,12 @@ type file struct {
} }
func handleArgs(args []string) chan file { func handleArgs(args []string) chan file {
rv := make(chan file) rv = make(chan file)
go func() { go func() {
for _, arg := range args { for _, arg := range args {
arg = filepath.Clean(arg) arg = filepath.Clean(arg)
handleArgRecursive(arg, rv) filepath.Walk(arg, getallfiles)
} }
close(rv) close(rv)
}() }()
@ -85,32 +88,23 @@ func handleArgs(args []string) chan file {
return rv return rv
} }
func handleArgRecursive(arg string, results chan file) { func getallfiles(path string, finfo os.FileInfo, err error) error {
stat, err := os.Stat(arg)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return return err
} }
if stat.IsDir() { if finfo.IsDir() {
// open the directory return nil
dirEntries, err := ioutil.ReadDir(arg)
if err != nil {
log.Fatal(err)
}
// walk the directory entries
for _, dirEntry := range dirEntries {
handleArgRecursive(arg+string(os.PathSeparator)+dirEntry.Name(), results)
}
} else {
bytes, err := ioutil.ReadFile(arg)
if err != nil {
log.Fatal(err)
}
results <- file{
filename: arg,
contents: bytes,
}
} }
bytes, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
rv <- file{
filename: filepath.Base(path),
contents: bytes,
}
return nil
} }