mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 22:34:19 +02:00
started implementing auto config loader
This commit is contained in:
@@ -223,27 +223,35 @@ func (cluster *Cluster) IsReachable() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (conf *Config) SetupES() error {
|
||||
func (conf *Config) SetupElasticClient(name string, cluster *Cluster) error {
|
||||
cluster.Name = name
|
||||
cluster.DebugHTTP = conf.DebugHTTP
|
||||
|
||||
opts := cluster.getDefaultOptions()
|
||||
|
||||
switch {
|
||||
case cluster.Pass != "" && cluster.User != "":
|
||||
opts = append(opts, elasticsearch.WithBasicAuth(cluster.User, cluster.Pass))
|
||||
case cluster.Token != "":
|
||||
opts = append(opts, elasticsearch.WithAPIKey(cluster.Token))
|
||||
}
|
||||
|
||||
es, err := elasticsearch.NewTyped(opts...)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setup elasticsearch connection: %w", err)
|
||||
}
|
||||
|
||||
cluster.SetClient(es)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (conf *Config) SetupElasticClients() error {
|
||||
for name, cluster := range conf.Clusters {
|
||||
cluster.Name = name
|
||||
cluster.DebugHTTP = conf.DebugHTTP
|
||||
|
||||
opts := cluster.getDefaultOptions()
|
||||
|
||||
switch {
|
||||
case cluster.Pass != "" && cluster.User != "":
|
||||
opts = append(opts, elasticsearch.WithBasicAuth(cluster.User, cluster.Pass))
|
||||
case cluster.Token != "":
|
||||
opts = append(opts, elasticsearch.WithAPIKey(cluster.Token))
|
||||
if err := conf.SetupElasticClient(name, cluster); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
es, err := elasticsearch.NewTyped(opts...)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setup elasticsearch connection: %w", err)
|
||||
}
|
||||
|
||||
cluster.SetClient(es)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user