diff --git a/.gitignore b/.gitignore index 8357fca..840b71d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ es +*.swp diff --git a/config.go b/config.go index 01f7cdc..8daa395 100644 --- a/config.go +++ b/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) } diff --git a/index.go b/index.go index 1a06988..d33c2d7 100644 --- a/index.go +++ b/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 ... \n", os.Args[0]) 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("valid subcommands:\n") fmt.Printf(" list list indexes\n") fmt.Printf(" delete delete index\n") + fmt.Printf(" create create index\n") } diff --git a/main.go b/main.go index 2c7b433..64fca82 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,6 @@ func printDefaultUsage() { fmt.Printf("Usage: %s ...\n", os.Args[0]) fmt.Printf("See '%s --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") }