1
// Copyright © 2016 Couchbase, Inc.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
25
"github.com/spf13/cobra"
28
var keepDir, keepExt, parseJSON bool
30
// indexCmd represents the index command
31
var indexCmd = &cobra.Command{
32
Use: "index [index path] [data paths ...]",
33
Short: "adds the files to the index",
34
Long: `The index command adds the specified files to the index.`,
35
Annotations: map[string]string{
36
canMutateBleveIndex: "true",
38
RunE: func(cmd *cobra.Command, args []string) error {
40
return fmt.Errorf("must specify at least one path")
42
for file := range handleArgs(args[1:]) {
45
docID := file.filename
47
_, docID = filepath.Split(docID)
50
ext := filepath.Ext(docID)
51
docID = docID[0 : len(docID)-len(ext)]
56
err = json.Unmarshal(file.contents, &doc)
58
return fmt.Errorf("error parsing JSON: %v", err)
61
fmt.Printf("Indexing: %s\n", docID)
62
err = idx.Index(docID, doc)
64
return fmt.Errorf("error indexing: %v", err)
76
func handleArgs(args []string) chan file {
78
go getAllFiles(args, rv)
82
func getAllFiles(args []string, rv chan file) {
83
for _, arg := range args {
84
arg = filepath.Clean(arg)
85
err := filepath.Walk(arg, func(path string, finfo os.FileInfo, err error) error {
94
bytes, err := ioutil.ReadFile(path)
99
filename: filepath.Base(path),
112
RootCmd.AddCommand(indexCmd)
114
indexCmd.Flags().BoolVarP(&keepDir, "keepDir", "d", false, "Keep the directory in the dodcument id, defaults false.")
115
indexCmd.Flags().BoolVarP(&keepExt, "keepExt", "x", false, "Keep the extension in the document id, defaults false.")
116
indexCmd.Flags().BoolVarP(&parseJSON, "json", "j", true, "Parse the contents as JSON, defaults true.")