Browse Source

Added index creation, cleaned up error output

master
Christopher Ramey 4 years ago
committed by Christopher Ramey
parent
commit
15ffc5e31d
  1. 1
      .gitignore
  2. 6
      config.go
  3. 56
      index.go
  4. 4
      main.go

1
.gitignore

@ -1 +1,2 @@
es
*.swp

6
config.go

@ -22,13 +22,13 @@ func LoadConfig(cfgpath string) *ESConfiguration {
if cfgpath != "" {
cfgfile, err := ioutil.ReadFile(cfgpath)
if err != nil {
fmt.Fprintf(os.Stderr, "Read error: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "read error: %s\n", err.Error())
os.Exit(1)
}
err = toml.Unmarshal(cfgfile, &cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Parse error: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "parse error: %s\n", err.Error())
os.Exit(1)
}
@ -56,7 +56,7 @@ func LoadConfig(cfgpath string) *ESConfiguration {
return &cfg
}
fmt.Fprintf(os.Stderr, "No valid configuration found in:\n")
fmt.Fprintf(os.Stderr, "no valid configuration found in:\n")
for _, path := range paths {
fmt.Fprintf(os.Stderr, " %s\n", path)
}

56
index.go

@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"io/ioutil"
"strings"
)
@ -28,13 +29,14 @@ func handleIndexCommand() {
indexes, err := es.IndexNames()
if err != nil {
fmt.Fprintf(os.Stderr, "Elastic 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)
@ -43,7 +45,7 @@ func handleIndexCommand() {
deletecmd.Parse(os.Args[3:])
if *iname == "" {
fmt.Fprintf(os.Stderr, "Name parameter is required for index delete\n")
fmt.Fprintf(os.Stderr, "name parameter is required for index delete\n")
deletecmd.PrintDefaults()
os.Exit(1)
}
@ -53,7 +55,52 @@ func handleIndexCommand() {
_, err := es.DeleteIndex(*iname).Do(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "Elastic error: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
os.Exit(1)
}
case "create":
createcmd := flag.NewFlagSet("index create", flag.ExitOnError)
createcmd.SetOutput(os.Stdout)
cfgpath := createcmd.String("config", "", "path to configuration")
iname := createcmd.String("name", "", "(required) index name to create")
bodystr := createcmd.String("body", "",
"json string to use as body options during create")
bodyfile := createcmd.String("bodyfile", "",
"json file to use as body options during create")
createcmd.Parse(os.Args[3:])
if *iname == "" {
fmt.Fprintf(os.Stderr, "name parameter is required for index create\n")
createcmd.PrintDefaults()
os.Exit(1)
}
if *bodystr != "" && *bodyfile != "" {
fmt.Fprintf(os.Stderr,
"body parameter cannot be used with bodyfile parameter\n")
os.Exit(1)
}
body := *bodystr
if *bodyfile != "" {
raw, err := ioutil.ReadFile(*bodyfile)
if err != nil {
fmt.Fprintf(os.Stderr, "read error: %s\n", err.Error())
os.Exit(1)
}
body = string(raw)
}
cfg := LoadConfig(*cfgpath)
es := ClientFromConfig(cfg.Elastic)
ics := es.CreateIndex(*iname)
if body != "" {
ics.Body(body)
}
_, err := ics.Do(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error())
os.Exit(1)
}
@ -73,7 +120,8 @@ func printIndexUsage() {
fmt.Printf("Usage: %s index <subcommand> ... \n", os.Args[0])
fmt.Printf("See '%s index <subcommand> --help' for information", os.Args[0])
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(" delete delete index\n")
fmt.Printf(" create create index\n")
}

4
main.go

@ -37,6 +37,6 @@ func printDefaultUsage() {
fmt.Printf("Usage: %s <command> ...\n", os.Args[0])
fmt.Printf("See '%s <command> --help' for information", os.Args[0])
fmt.Printf(" on a specific command\n")
fmt.Printf("Valid commands:\n")
fmt.Printf(" index add or delete indexes\n")
fmt.Printf("valid commands:\n")
fmt.Printf(" index create, delete, or list indexes\n")
}
Loading…
Cancel
Save