From 37de744d433b9fec5024dc70593df91beb2710dc Mon Sep 17 00:00:00 2001 From: Christopher Ramey Date: Fri, 28 Aug 2020 19:46:51 -0800 Subject: [PATCH] Added support for multiple servers per configuration --- alias.go | 8 ++++---- config.go | 26 +++++++++++++++++++++----- elastic.go | 8 ++++---- index.go | 10 +++++----- main.go | 2 -- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/alias.go b/alias.go index 5e53222..9b300f5 100644 --- a/alias.go +++ b/alias.go @@ -25,7 +25,7 @@ func handleAliasCommand() { listcmd.Parse(os.Args[3:]) cfg := LoadConfig(*cfgpath) - es := ClientFromConfig(cfg.Elastic) + es := ClientFromConfig(cfg.GetServer("")) aliases, err := es.CatAliases().Do(context.Background()) if err != nil { @@ -64,7 +64,7 @@ func handleAliasCommand() { } cfg := LoadConfig(*cfgpath) - es := ClientFromConfig(cfg.Elastic) + es := ClientFromConfig(cfg.GetServer("")) _, err := es.Alias().Add(*iname, *aname).Do(context.Background()) if err != nil { @@ -93,7 +93,7 @@ func handleAliasCommand() { } cfg := LoadConfig(*cfgpath) - es := ClientFromConfig(cfg.Elastic) + es := ClientFromConfig(cfg.GetServer("")) _, err := es.Alias().Remove(*iname, *aname).Do(context.Background()) if err != nil { @@ -125,7 +125,7 @@ func handleAliasCommand() { } cfg := LoadConfig(*cfgpath) - es := ClientFromConfig(cfg.Elastic) + es := ClientFromConfig(cfg.GetServer("")) oinames := make([]string, 0, 10) if *oiname != "" { diff --git a/config.go b/config.go index 8daa395..baaa140 100644 --- a/config.go +++ b/config.go @@ -7,18 +7,34 @@ import ( "os" ) -type ESConfiguration struct { - Elastic ElasticConfiguration +type ElasticConfig struct { + Elastic ElasticSettings + Servers map[string]ElasticServer } -type ElasticConfiguration struct { +type ElasticSettings struct { + DefaultServer string +} + +type ElasticServer struct { URL string User string Pass string } -func LoadConfig(cfgpath string) *ESConfiguration { - cfg := ESConfiguration{} +func (ec *ElasticConfig) GetServer(name string) *ElasticServer { + if name == "" { + name = ec.Elastic.DefaultServer + } + + if serv, ok := ec.Servers[name]; ok { + return &serv + } + return nil +} + +func LoadConfig(cfgpath string) *ElasticConfig { + cfg := ElasticConfig{} if cfgpath != "" { cfgfile, err := ioutil.ReadFile(cfgpath) if err != nil { diff --git a/elastic.go b/elastic.go index 2da273c..53d9320 100644 --- a/elastic.go +++ b/elastic.go @@ -6,15 +6,15 @@ import ( "os" ) -func ClientFromConfig(cfg ElasticConfiguration) *elastic.Client { +func ClientFromConfig(server *ElasticServer) *elastic.Client { opts := []elastic.ClientOptionFunc{ - elastic.SetURL(cfg.URL), + elastic.SetURL(server.URL), elastic.SetSniff(false), elastic.SetGzip(true), } - if cfg.User != "" && cfg.Pass != "" { + if server.User != "" && server.Pass != "" { opts = append(opts, elastic.SetBasicAuth( - cfg.User, cfg.Pass, + server.User, server.Pass, )) } diff --git a/index.go b/index.go index 44ef725..d7ea097 100644 --- a/index.go +++ b/index.go @@ -4,8 +4,8 @@ import ( "context" "flag" "fmt" - "os" "io/ioutil" + "os" "strings" ) @@ -25,7 +25,7 @@ func handleIndexCommand() { listcmd.Parse(os.Args[3:]) cfg := LoadConfig(*cfgpath) - es := ClientFromConfig(cfg.Elastic) + es := ClientFromConfig(cfg.GetServer("")) indexes, err := es.IndexNames() if err != nil { @@ -51,7 +51,7 @@ func handleIndexCommand() { } cfg := LoadConfig(*cfgpath) - es := ClientFromConfig(cfg.Elastic) + es := ClientFromConfig(cfg.GetServer("")) _, err := es.DeleteIndex(*iname).Do(context.Background()) if err != nil { @@ -93,7 +93,7 @@ func handleIndexCommand() { } cfg := LoadConfig(*cfgpath) - es := ClientFromConfig(cfg.Elastic) + es := ClientFromConfig(cfg.GetServer("")) ics := es.CreateIndex(*iname) if body != "" { ics.Body(body) @@ -118,7 +118,7 @@ func handleIndexCommand() { } cfg := LoadConfig(*cfgpath) - es := ClientFromConfig(cfg.Elastic) + es := ClientFromConfig(cfg.GetServer("")) _, err := es.Refresh(*iname).Do(context.Background()) if err != nil { fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) diff --git a/main.go b/main.go index 016a297..d452255 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,6 @@ import ( "fmt" "os" "strings" - // elastic "github.com/olivere/elastic/v7" - // toml "github.com/pelletier/go-toml" ) func main() {