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.

127 lines
3.2 KiB

4 years ago
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. "io/ioutil"
  8. "strings"
  9. )
  10. func handleIndexCommand() {
  11. if len(os.Args) < 3 {
  12. fmt.Fprintf(os.Stderr, "%s index: subcommand missing\n", os.Args[0])
  13. printIndexUsage()
  14. os.Exit(1)
  15. }
  16. subcmd := strings.ToLower(os.Args[2])
  17. switch subcmd {
  18. case "list":
  19. listcmd := flag.NewFlagSet("index list", flag.ExitOnError)
  20. listcmd.SetOutput(os.Stdout)
  21. cfgpath := listcmd.String("config", "", "path to configuration")
  22. listcmd.Parse(os.Args[3:])
  23. cfg := LoadConfig(*cfgpath)
  24. es := ClientFromConfig(cfg.Elastic)
  25. indexes, err := es.IndexNames()
  26. if err != nil {
  27. fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
  28. os.Exit(1)
  29. }
  30. for _, index := range indexes {
  31. fmt.Printf("%s\n", index)
  32. }
  33. case "delete":
  34. deletecmd := flag.NewFlagSet("index delete", flag.ExitOnError)
  35. deletecmd.SetOutput(os.Stdout)
  36. cfgpath := deletecmd.String("config", "", "path to configuration")
  37. iname := deletecmd.String("name", "", "(required) index name to delete")
  38. deletecmd.Parse(os.Args[3:])
  39. if *iname == "" {
  40. fmt.Fprintf(os.Stderr, "name parameter is required for index delete\n")
  41. deletecmd.PrintDefaults()
  42. os.Exit(1)
  43. }
  44. cfg := LoadConfig(*cfgpath)
  45. es := ClientFromConfig(cfg.Elastic)
  46. _, err := es.DeleteIndex(*iname).Do(context.Background())
  47. if err != nil {
  48. fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
  49. os.Exit(1)
  50. }
  51. case "create":
  52. createcmd := flag.NewFlagSet("index create", flag.ExitOnError)
  53. createcmd.SetOutput(os.Stdout)
  54. cfgpath := createcmd.String("config", "", "path to configuration")
  55. iname := createcmd.String("name", "", "(required) index name to create")
  56. bodystr := createcmd.String("body", "",
  57. "json string to use as body options during create")
  58. bodyfile := createcmd.String("bodyfile", "",
  59. "json file to use as body options during create")
  60. createcmd.Parse(os.Args[3:])
  61. if *iname == "" {
  62. fmt.Fprintf(os.Stderr, "name parameter is required for index create\n")
  63. createcmd.PrintDefaults()
  64. os.Exit(1)
  65. }
  66. if *bodystr != "" && *bodyfile != "" {
  67. fmt.Fprintf(os.Stderr,
  68. "body parameter cannot be used with bodyfile parameter\n")
  69. os.Exit(1)
  70. }
  71. body := *bodystr
  72. if *bodyfile != "" {
  73. raw, err := ioutil.ReadFile(*bodyfile)
  74. if err != nil {
  75. fmt.Fprintf(os.Stderr, "read error: %s\n", err.Error())
  76. os.Exit(1)
  77. }
  78. body = string(raw)
  79. }
  80. cfg := LoadConfig(*cfgpath)
  81. es := ClientFromConfig(cfg.Elastic)
  82. ics := es.CreateIndex(*iname)
  83. if body != "" {
  84. ics.Body(body)
  85. }
  86. _, err := ics.Do(context.Background())
  87. if err != nil {
  88. fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
  89. os.Exit(1)
  90. }
  91. case "--help":
  92. printIndexUsage()
  93. os.Exit(0)
  94. default:
  95. fmt.Fprintf(os.Stderr, "%s: index '%s' is not a recognized subcommand\n",
  96. os.Args[0], os.Args[2])
  97. printDefaultUsage()
  98. os.Exit(1)
  99. }
  100. }
  101. func printIndexUsage() {
  102. fmt.Printf("Usage: %s index <subcommand> ... \n", os.Args[0])
  103. fmt.Printf("See '%s index <subcommand> --help' for information", os.Args[0])
  104. fmt.Printf(" on a specific command\n")
  105. fmt.Printf("valid subcommands:\n")
  106. fmt.Printf(" list list indexes\n")
  107. fmt.Printf(" delete delete index\n")
  108. fmt.Printf(" create create index\n")
  109. }