diff --git a/.gitignore b/.gitignore index f674fcc..8357fca 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -./es +es diff --git a/config.go b/config.go index be0610f..01f7cdc 100644 --- a/config.go +++ b/config.go @@ -7,7 +7,7 @@ import ( "os" ) -type IndexerConfiguration struct { +type ESConfiguration struct { Elastic ElasticConfiguration } @@ -17,8 +17,8 @@ type ElasticConfiguration struct { Pass string } -func loadConfiguration(cfgpath string) *IndexerConfiguration { - cfg := IndexerConfiguration{} +func LoadConfig(cfgpath string) *ESConfiguration { + cfg := ESConfiguration{} if cfgpath != "" { cfgfile, err := ioutil.ReadFile(cfgpath) if err != nil { diff --git a/elastic.go b/elastic.go new file mode 100644 index 0000000..2da273c --- /dev/null +++ b/elastic.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + elastic "github.com/olivere/elastic/v7" + "os" +) + +func ClientFromConfig(cfg ElasticConfiguration) *elastic.Client { + opts := []elastic.ClientOptionFunc{ + elastic.SetURL(cfg.URL), + elastic.SetSniff(false), + elastic.SetGzip(true), + } + if cfg.User != "" && cfg.Pass != "" { + opts = append(opts, elastic.SetBasicAuth( + cfg.User, cfg.Pass, + )) + } + + es, err := elastic.NewClient(opts...) + if err != nil { + fmt.Fprintf(os.Stderr, "Elastic connection error: %s\n", err.Error()) + os.Exit(1) + } + + return es +} diff --git a/index.go b/index.go index 37d5b75..1a06988 100644 --- a/index.go +++ b/index.go @@ -1,10 +1,11 @@ package main import ( + "context" + "flag" "fmt" "os" "strings" - elastic "github.com/olivere/elastic/v7" ) func handleIndexCommand() { @@ -17,38 +18,44 @@ func handleIndexCommand() { subcmd := strings.ToLower(os.Args[2]) switch subcmd { 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() 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) } 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() @@ -67,5 +74,6 @@ func printIndexUsage() { fmt.Printf("See '%s index --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(" list list indexes\n") + fmt.Printf(" delete delete index\n") }