2014-09-01 22:14:29 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
// except in compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the
|
|
|
|
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
// either express or implied. See the License for the specific language governing permissions
|
|
|
|
// and limitations under the License.
|
2014-09-02 16:54:50 +02:00
|
|
|
|
2014-09-01 22:14:29 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/blevesearch/bleve"
|
2015-09-16 23:18:46 +02:00
|
|
|
_ "github.com/blevesearch/bleve/config"
|
2014-09-01 22:14:29 +02:00
|
|
|
)
|
|
|
|
|
2015-10-13 21:04:51 +02:00
|
|
|
var (
|
|
|
|
indexPath = flag.String("index", "", "index path")
|
|
|
|
keepExt = flag.Bool("keepExt", false, "keep extension in doc id")
|
|
|
|
keepDir = flag.Bool("keepDir", false, "keep dir in doc id")
|
|
|
|
rv chan file
|
|
|
|
)
|
2014-09-01 22:14:29 +02:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *indexPath == "" {
|
|
|
|
log.Fatal("must specify index path")
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the index
|
|
|
|
index, err := bleve.Open(*indexPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-04-07 17:42:46 +02:00
|
|
|
defer func() {
|
|
|
|
cerr := index.Close()
|
|
|
|
if cerr != nil {
|
|
|
|
log.Fatalf("error closing index: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2014-09-01 22:14:29 +02:00
|
|
|
|
|
|
|
if flag.NArg() < 1 {
|
|
|
|
log.Fatal("must specify at least one path to index")
|
|
|
|
}
|
|
|
|
|
|
|
|
for file := range handleArgs(flag.Args()) {
|
|
|
|
// index the files
|
|
|
|
docID := file.filename
|
|
|
|
if !*keepDir {
|
|
|
|
_, docID = filepath.Split(docID)
|
|
|
|
}
|
|
|
|
if !*keepExt {
|
|
|
|
ext := filepath.Ext(docID)
|
|
|
|
docID = docID[0 : len(docID)-len(ext)]
|
|
|
|
}
|
|
|
|
log.Printf("Indexing: %s", docID)
|
|
|
|
err = index.Index(docID, file.contents)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type file struct {
|
|
|
|
filename string
|
|
|
|
contents []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleArgs(args []string) chan file {
|
2015-10-13 21:04:51 +02:00
|
|
|
rv = make(chan file)
|
2014-09-01 22:14:29 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for _, arg := range args {
|
|
|
|
arg = filepath.Clean(arg)
|
2015-10-13 21:04:51 +02:00
|
|
|
filepath.Walk(arg, getallfiles)
|
2014-09-01 22:14:29 +02:00
|
|
|
}
|
|
|
|
close(rv)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
2015-10-13 21:04:51 +02:00
|
|
|
func getallfiles(path string, finfo os.FileInfo, err error) error {
|
2014-09-01 22:14:29 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2015-10-13 21:04:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if finfo.IsDir() {
|
|
|
|
return nil
|
2014-09-01 22:14:29 +02:00
|
|
|
}
|
|
|
|
|
2015-10-13 21:04:51 +02:00
|
|
|
bytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
rv <- file{
|
|
|
|
filename: filepath.Base(path),
|
|
|
|
contents: bytes,
|
2014-09-01 22:14:29 +02:00
|
|
|
}
|
2015-10-13 21:04:51 +02:00
|
|
|
|
|
|
|
return nil
|
2014-09-01 22:14:29 +02:00
|
|
|
}
|