0
0

Merge branch 'Shugyousha-simplebleveindex'

This commit is contained in:
Marty Schoch 2015-10-16 13:11:51 -04:00
commit 390781b379

View File

@ -20,9 +20,11 @@ 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")
)
func main() { func main() {
@ -73,44 +75,35 @@ type file struct {
func handleArgs(args []string) chan file { func handleArgs(args []string) chan file {
rv := make(chan file) rv := make(chan file)
go getAllFiles(args, rv)
go func() {
for _, arg := range args {
arg = filepath.Clean(arg)
handleArgRecursive(arg, rv)
}
close(rv)
}()
return rv return rv
} }
func handleArgRecursive(arg string, results chan file) { func getAllFiles(args []string, rv chan file) {
stat, err := os.Stat(arg) for _, arg := range args {
arg = filepath.Clean(arg)
err := filepath.Walk(arg, func(path string, finfo os.FileInfo, err error) error {
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
})
if err != nil {
log.Fatal(err)
} }
} }
close(rv)
}