0
0
Fork 0

use cobra command Annotations to identify mutating commands

some downstream applications need to know which sub-commands
may alter the index.  this new function allows them to be
identified, while not prescribing any particular behavior.
This commit is contained in:
Marty Schoch 2017-02-15 15:37:26 -05:00
parent c937e971af
commit 9ca14d29f1
4 changed files with 26 additions and 0 deletions

View File

@ -31,6 +31,9 @@ var bulkCmd = &cobra.Command{
Use: "bulk [index path] [data paths ...]",
Short: "bulk loads from newline delimited JSON files",
Long: `The bulk command will perform batch loading of documents in one or more newline delimited JSON files.`,
Annotations: map[string]string{
canMutateBleveIndex: "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("must specify at least one path")

View File

@ -31,6 +31,9 @@ var createCmd = &cobra.Command{
Use: "create [index path]",
Short: "creates a new index",
Long: `The create command will create a new empty index.`,
Annotations: map[string]string{
canMutateBleveIndex: "true",
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// override RootCmd version which opens existing index
if len(args) < 1 {

View File

@ -32,6 +32,9 @@ var indexCmd = &cobra.Command{
Use: "index [index path] [data paths ...]",
Short: "adds the files to the index",
Long: `The index command adds the specified files to the index.`,
Annotations: map[string]string{
canMutateBleveIndex: "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("must specify at least one path")

View File

@ -17,6 +17,7 @@ package cmd
import (
"fmt"
"os"
"strconv"
"github.com/blevesearch/bleve"
"github.com/spf13/cobra"
@ -26,6 +27,22 @@ var cfgFile string
var idx bleve.Index
const canMutateBleveIndex = "canMutateBleveIndex"
// CanMutateBleveIndex returns true if the command is capable
// of mutating the bleve index, or false if its operation is
// read-only
func CanMutateBleveIndex(c *cobra.Command) bool {
for k, v := range c.Annotations {
if k == canMutateBleveIndex {
if b, err := strconv.ParseBool(v); err == nil && b {
return true
}
}
}
return false
}
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "bleve",