Use filepath.Walk instead of rolling our own func
This commit is contained in:
parent
08572e4925
commit
0ef988a318
@ -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)
|
}
|
||||||
|
|
||||||
|
bytes, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
rv <- file{
|
||||||
// walk the directory entries
|
filename: filepath.Base(path),
|
||||||
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,
|
contents: bytes,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user