|
|
@ -5,6 +5,7 @@ import ( |
|
|
|
"flag" |
|
|
|
"fmt" |
|
|
|
"os" |
|
|
|
"io/ioutil" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
|
|
|
@ -28,13 +29,14 @@ func handleIndexCommand() { |
|
|
|
|
|
|
|
indexes, err := es.IndexNames() |
|
|
|
if err != nil { |
|
|
|
fmt.Fprintf(os.Stderr, "Elastic error: %s\n", err.Error()) |
|
|
|
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
|
|
|
|
for _, index := range indexes { |
|
|
|
fmt.Printf("%s\n", index) |
|
|
|
} |
|
|
|
|
|
|
|
case "delete": |
|
|
|
deletecmd := flag.NewFlagSet("index delete", flag.ExitOnError) |
|
|
|
deletecmd.SetOutput(os.Stdout) |
|
|
@ -43,7 +45,7 @@ func handleIndexCommand() { |
|
|
|
deletecmd.Parse(os.Args[3:]) |
|
|
|
|
|
|
|
if *iname == "" { |
|
|
|
fmt.Fprintf(os.Stderr, "Name parameter is required for index delete\n") |
|
|
|
fmt.Fprintf(os.Stderr, "name parameter is required for index delete\n") |
|
|
|
deletecmd.PrintDefaults() |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
@ -53,7 +55,52 @@ func handleIndexCommand() { |
|
|
|
|
|
|
|
_, err := es.DeleteIndex(*iname).Do(context.Background()) |
|
|
|
if err != nil { |
|
|
|
fmt.Fprintf(os.Stderr, "Elastic error: %s\n", err.Error()) |
|
|
|
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
|
|
|
|
case "create": |
|
|
|
createcmd := flag.NewFlagSet("index create", flag.ExitOnError) |
|
|
|
createcmd.SetOutput(os.Stdout) |
|
|
|
cfgpath := createcmd.String("config", "", "path to configuration") |
|
|
|
iname := createcmd.String("name", "", "(required) index name to create") |
|
|
|
bodystr := createcmd.String("body", "", |
|
|
|
"json string to use as body options during create") |
|
|
|
bodyfile := createcmd.String("bodyfile", "", |
|
|
|
"json file to use as body options during create") |
|
|
|
createcmd.Parse(os.Args[3:]) |
|
|
|
|
|
|
|
if *iname == "" { |
|
|
|
fmt.Fprintf(os.Stderr, "name parameter is required for index create\n") |
|
|
|
createcmd.PrintDefaults() |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
|
|
|
|
if *bodystr != "" && *bodyfile != "" { |
|
|
|
fmt.Fprintf(os.Stderr, |
|
|
|
"body parameter cannot be used with bodyfile parameter\n") |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
|
|
|
|
body := *bodystr |
|
|
|
if *bodyfile != "" { |
|
|
|
raw, err := ioutil.ReadFile(*bodyfile) |
|
|
|
if err != nil { |
|
|
|
fmt.Fprintf(os.Stderr, "read error: %s\n", err.Error()) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
body = string(raw) |
|
|
|
} |
|
|
|
|
|
|
|
cfg := LoadConfig(*cfgpath) |
|
|
|
es := ClientFromConfig(cfg.Elastic) |
|
|
|
ics := es.CreateIndex(*iname) |
|
|
|
if body != "" { |
|
|
|
ics.Body(body) |
|
|
|
} |
|
|
|
_, err := ics.Do(context.Background()) |
|
|
|
if err != nil { |
|
|
|
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
|
|
|
@ -73,7 +120,8 @@ func printIndexUsage() { |
|
|
|
fmt.Printf("Usage: %s index <subcommand> ... \n", os.Args[0]) |
|
|
|
fmt.Printf("See '%s index <subcommand> --help' for information", os.Args[0]) |
|
|
|
fmt.Printf(" on a specific command\n") |
|
|
|
fmt.Printf("Valid subcommands:\n") |
|
|
|
fmt.Printf("valid subcommands:\n") |
|
|
|
fmt.Printf(" list list indexes\n") |
|
|
|
fmt.Printf(" delete delete index\n") |
|
|
|
fmt.Printf(" create create index\n") |
|
|
|
} |