You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.9 KiB
79 lines
1.9 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func handleIndexCommand() {
|
|
if len(os.Args) < 3 {
|
|
fmt.Fprintf(os.Stderr, "%s index: subcommand missing\n", os.Args[0])
|
|
printIndexUsage()
|
|
os.Exit(1)
|
|
}
|
|
|
|
subcmd := strings.ToLower(os.Args[2])
|
|
switch subcmd {
|
|
case "list":
|
|
listcmd := flag.NewFlagSet("index list", flag.ExitOnError)
|
|
listcmd.SetOutput(os.Stdout)
|
|
cfgpath := listcmd.String("config", "", "path to configuration")
|
|
listcmd.Parse(os.Args[3:])
|
|
|
|
cfg := LoadConfig(*cfgpath)
|
|
es := ClientFromConfig(cfg.Elastic)
|
|
|
|
indexes, err := es.IndexNames()
|
|
if err != nil {
|
|
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)
|
|
cfgpath := deletecmd.String("config", "", "path to configuration")
|
|
iname := deletecmd.String("name", "", "(required) index name to delete")
|
|
deletecmd.Parse(os.Args[3:])
|
|
|
|
if *iname == "" {
|
|
fmt.Fprintf(os.Stderr, "Name parameter is required for index delete\n")
|
|
deletecmd.PrintDefaults()
|
|
os.Exit(1)
|
|
}
|
|
|
|
cfg := LoadConfig(*cfgpath)
|
|
es := ClientFromConfig(cfg.Elastic)
|
|
|
|
_, err := es.DeleteIndex(*iname).Do(context.Background())
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Elastic error: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
case "--help":
|
|
printIndexUsage()
|
|
os.Exit(0)
|
|
|
|
default:
|
|
fmt.Fprintf(os.Stderr, "%s: index '%s' is not a recognized subcommand\n",
|
|
os.Args[0], os.Args[2])
|
|
printDefaultUsage()
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
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(" list list indexes\n")
|
|
fmt.Printf(" delete delete index\n")
|
|
}
|