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.

155 lines
4.1 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
4 years ago
4 years ago
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  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. server := listcmd.String("server", "", "server name to use")
  23. listcmd.Parse(os.Args[3:])
  24. cfg := LoadConfig(*cfgpath)
  25. es := cfg.GetClient(*server)
  26. indexes, err := es.IndexNames()
  27. if err != nil {
  28. fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
  29. os.Exit(1)
  30. }
  31. for _, index := range indexes {
  32. fmt.Printf("%s\n", index)
  33. }
  34. case "delete":
  35. deletecmd := flag.NewFlagSet("index delete", flag.ExitOnError)
  36. deletecmd.SetOutput(os.Stdout)
  37. cfgpath := deletecmd.String("config", "", "path to configuration")
  38. server := deletecmd.String("server", "", "server name to use")
  39. iname := deletecmd.String("name", "", "(required) index name to delete")
  40. deletecmd.Parse(os.Args[3:])
  41. if *iname == "" {
  42. fmt.Fprintf(os.Stderr, "name parameter is required for index delete\n")
  43. deletecmd.PrintDefaults()
  44. os.Exit(1)
  45. }
  46. cfg := LoadConfig(*cfgpath)
  47. es := cfg.GetClient(*server)
  48. _, err := es.DeleteIndex(*iname).Do(context.Background())
  49. if err != nil {
  50. fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
  51. os.Exit(1)
  52. }
  53. case "create":
  54. createcmd := flag.NewFlagSet("index create", flag.ExitOnError)
  55. createcmd.SetOutput(os.Stdout)
  56. cfgpath := createcmd.String("config", "", "path to configuration")
  57. server := createcmd.String("server", "", "server name to use")
  58. iname := createcmd.String("name", "", "(required) index name to create")
  59. bodystr := createcmd.String("body", "",
  60. "json string to use as body options during create")
  61. bodyfile := createcmd.String("bodyfile", "",
  62. "json file to use as body options during create")
  63. createcmd.Parse(os.Args[3:])
  64. if *iname == "" {
  65. fmt.Fprintf(os.Stderr, "name parameter is required for index create\n")
  66. createcmd.PrintDefaults()
  67. os.Exit(1)
  68. }
  69. if *bodystr != "" && *bodyfile != "" {
  70. fmt.Fprintf(os.Stderr,
  71. "body parameter cannot be used with bodyfile parameter\n")
  72. os.Exit(1)
  73. }
  74. body := *bodystr
  75. if *bodyfile != "" {
  76. raw, err := ioutil.ReadFile(*bodyfile)
  77. if err != nil {
  78. fmt.Fprintf(os.Stderr, "read error: %s\n", err.Error())
  79. os.Exit(1)
  80. }
  81. body = string(raw)
  82. }
  83. cfg := LoadConfig(*cfgpath)
  84. es := cfg.GetClient(*server)
  85. ics := es.CreateIndex(*iname)
  86. if body != "" {
  87. ics.Body(body)
  88. }
  89. _, err := ics.Do(context.Background())
  90. if err != nil {
  91. fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
  92. os.Exit(1)
  93. }
  94. case "refresh":
  95. refreshcmd := flag.NewFlagSet("index refresh", flag.ExitOnError)
  96. refreshcmd.SetOutput(os.Stdout)
  97. cfgpath := refreshcmd.String("config", "", "path to configuration")
  98. server := refreshcmd.String("server", "", "server name to use")
  99. iname := refreshcmd.String("name", "", "(required) index name to refresh")
  100. refreshcmd.Parse(os.Args[3:])
  101. if *iname == "" {
  102. fmt.Fprintf(os.Stderr, "name parameter is required for index refresh\n")
  103. refreshcmd.PrintDefaults()
  104. os.Exit(1)
  105. }
  106. cfg := LoadConfig(*cfgpath)
  107. es := cfg.GetClient(*server)
  108. _, err := es.Refresh(*iname).Do(context.Background())
  109. if err != nil {
  110. fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
  111. os.Exit(1)
  112. }
  113. case "--help":
  114. printIndexUsage()
  115. os.Exit(0)
  116. default:
  117. fmt.Fprintf(os.Stderr, "%s: index '%s' is not a recognized subcommand\n",
  118. os.Args[0], os.Args[2])
  119. printIndexUsage()
  120. os.Exit(1)
  121. }
  122. }
  123. func printIndexUsage() {
  124. fmt.Printf("Usage: %s index <subcommand> ... \n", os.Args[0])
  125. fmt.Printf("See '%s index <subcommand> --help' for information", os.Args[0])
  126. fmt.Printf(" on a specific command\n")
  127. fmt.Printf("valid subcommands:\n")
  128. fmt.Printf(" list list indexes\n")
  129. fmt.Printf(" delete delete index\n")
  130. fmt.Printf(" create create index\n")
  131. fmt.Printf(" refresh refresh index\n")
  132. }