|
@ -1,10 +1,11 @@ |
|
|
package main |
|
|
package main |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"context" |
|
|
|
|
|
"flag" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"os" |
|
|
"os" |
|
|
"strings" |
|
|
"strings" |
|
|
elastic "github.com/olivere/elastic/v7" |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func handleIndexCommand() { |
|
|
func handleIndexCommand() { |
|
@ -17,38 +18,44 @@ func handleIndexCommand() { |
|
|
subcmd := strings.ToLower(os.Args[2]) |
|
|
subcmd := strings.ToLower(os.Args[2]) |
|
|
switch subcmd { |
|
|
switch subcmd { |
|
|
case "list": |
|
|
case "list": |
|
|
cfgpath := "" |
|
|
|
|
|
if len(os.Args) == 4 { |
|
|
|
|
|
cfgpath = os.Args[3] |
|
|
|
|
|
} |
|
|
|
|
|
cfg := loadConfiguration(cfgpath) |
|
|
|
|
|
|
|
|
|
|
|
opts := []elastic.ClientOptionFunc{ |
|
|
|
|
|
elastic.SetURL(cfg.Elastic.URL), |
|
|
|
|
|
elastic.SetSniff(false), |
|
|
|
|
|
elastic.SetGzip(true), |
|
|
|
|
|
} |
|
|
|
|
|
if cfg.Elastic.User != "" && cfg.Elastic.Pass != "" { |
|
|
|
|
|
opts = append(opts, elastic.SetBasicAuth( |
|
|
|
|
|
cfg.Elastic.User, cfg.Elastic.Pass, |
|
|
|
|
|
)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
listcmd := flag.NewFlagSet("index list", flag.ExitOnError) |
|
|
|
|
|
listcmd.SetOutput(os.Stdout) |
|
|
|
|
|
cfgpath := listcmd.String("config", "", "path to configuration") |
|
|
|
|
|
listcmd.Parse(os.Args[3:]) |
|
|
|
|
|
|
|
|
es, err := elastic.NewClient(opts...) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Elastic connection error: %s\n", err.Error()) |
|
|
|
|
|
os.Exit(1) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
cfg := LoadConfig(*cfgpath) |
|
|
|
|
|
es := ClientFromConfig(cfg.Elastic) |
|
|
|
|
|
|
|
|
indexes, err := es.IndexNames() |
|
|
indexes, err := es.IndexNames() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Fprintf(os.Stderr, "Elastic connection error: %s\n", err.Error()) |
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Elastic error: %s\n", err.Error()) |
|
|
os.Exit(1) |
|
|
os.Exit(1) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for _, index := range indexes { |
|
|
for _, index := range indexes { |
|
|
fmt.Printf("%s\n", index) |
|
|
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": |
|
|
case "--help": |
|
|
printIndexUsage() |
|
|
printIndexUsage() |
|
@ -67,5 +74,6 @@ func printIndexUsage() { |
|
|
fmt.Printf("See '%s index <subcommand> --help' for information", os.Args[0]) |
|
|
fmt.Printf("See '%s index <subcommand> --help' for information", os.Args[0]) |
|
|
fmt.Printf(" on a specific command\n") |
|
|
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(" list list indexes\n") |
|
|
|
|
|
fmt.Printf(" delete delete index\n") |
|
|
} |
|
|
} |