a command line interface for elastic (work in progress)
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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "strings"
  8. )
  9. func handleIndexCommand() {
  10. if len(os.Args) < 3 {
  11. fmt.Fprintf(os.Stderr, "%s index: subcommand missing\n", os.Args[0])
  12. printIndexUsage()
  13. os.Exit(1)
  14. }
  15. subcmd := strings.ToLower(os.Args[2])
  16. switch subcmd {
  17. case "list":
  18. listcmd := flag.NewFlagSet("index list", flag.ExitOnError)
  19. listcmd.SetOutput(os.Stdout)
  20. cfgpath := listcmd.String("config", "", "path to configuration")
  21. listcmd.Parse(os.Args[3:])
  22. cfg := LoadConfig(*cfgpath)
  23. es := ClientFromConfig(cfg.Elastic)
  24. indexes, err := es.IndexNames()
  25. if err != nil {
  26. fmt.Fprintf(os.Stderr, "Elastic error: %s\n", err.Error())
  27. os.Exit(1)
  28. }
  29. for _, index := range indexes {
  30. fmt.Printf("%s\n", index)
  31. }
  32. case "delete":
  33. deletecmd := flag.NewFlagSet("index delete", flag.ExitOnError)
  34. deletecmd.SetOutput(os.Stdout)
  35. cfgpath := deletecmd.String("config", "", "path to configuration")
  36. iname := deletecmd.String("name", "", "(required) index name to delete")
  37. deletecmd.Parse(os.Args[3:])
  38. if *iname == "" {
  39. fmt.Fprintf(os.Stderr, "Name parameter is required for index delete\n")
  40. deletecmd.PrintDefaults()
  41. os.Exit(1)
  42. }
  43. cfg := LoadConfig(*cfgpath)
  44. es := ClientFromConfig(cfg.Elastic)
  45. _, err := es.DeleteIndex(*iname).Do(context.Background())
  46. if err != nil {
  47. fmt.Fprintf(os.Stderr, "Elastic error: %s\n", err.Error())
  48. os.Exit(1)
  49. }
  50. case "--help":
  51. printIndexUsage()
  52. os.Exit(0)
  53. default:
  54. fmt.Fprintf(os.Stderr, "%s: index '%s' is not a recognized subcommand\n",
  55. os.Args[0], os.Args[2])
  56. printDefaultUsage()
  57. os.Exit(1)
  58. }
  59. }
  60. func printIndexUsage() {
  61. fmt.Printf("Usage: %s index <subcommand> ... \n", os.Args[0])
  62. fmt.Printf("See '%s index <subcommand> --help' for information", os.Args[0])
  63. fmt.Printf(" on a specific command\n")
  64. fmt.Printf("Valid subcommands:\n")
  65. fmt.Printf(" list list indexes\n")
  66. fmt.Printf(" delete delete index\n")
  67. }