2026-04-21 10:50:09 +02:00
|
|
|
/*
|
|
|
|
|
Copyright © 2026 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
package cfg
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-06 13:02:28 +02:00
|
|
|
"context"
|
2026-04-21 10:50:09 +02:00
|
|
|
"crypto/tls"
|
|
|
|
|
"errors"
|
2026-04-27 09:11:10 +02:00
|
|
|
"fmt"
|
2026-04-21 10:50:09 +02:00
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
|
2026-05-11 10:42:58 +02:00
|
|
|
"github.com/alecthomas/repr"
|
2026-04-21 10:50:09 +02:00
|
|
|
"github.com/elastic/elastic-transport-go/v8/elastictransport"
|
|
|
|
|
"github.com/elastic/go-elasticsearch/v9"
|
2026-04-27 09:11:10 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
2026-04-21 10:50:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2026-05-19 13:58:08 +02:00
|
|
|
Version string = `v0.0.11`
|
2026-04-21 10:50:09 +02:00
|
|
|
)
|
|
|
|
|
|
2026-04-27 09:11:10 +02:00
|
|
|
type Cluster struct {
|
|
|
|
|
Uri, User, Pass string
|
|
|
|
|
ES *elasticsearch.TypedClient
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 10:50:09 +02:00
|
|
|
type Config struct {
|
2026-05-06 07:25:27 +02:00
|
|
|
ConfigFile string // -c
|
|
|
|
|
CurrentCluster string // -C
|
|
|
|
|
Debug bool // -d
|
|
|
|
|
Clusters map[string]*Cluster
|
|
|
|
|
DefaultCluster *Cluster
|
|
|
|
|
Index string // index: -i
|
|
|
|
|
Failed, Partials bool // index: flags
|
2026-05-18 13:58:08 +02:00
|
|
|
Shards, Replicas int // index create+allocation: -s -r
|
2026-05-06 07:25:27 +02:00
|
|
|
Wait bool // index create: -w
|
2026-05-18 13:58:08 +02:00
|
|
|
Primary bool // index allocation: -p
|
2026-05-06 07:25:27 +02:00
|
|
|
From, To, MaxItems int // search: flags
|
|
|
|
|
Filter []string // search: -F
|
|
|
|
|
Exclude string // cluster compare: -e (regexp)
|
2026-05-13 13:51:09 +02:00
|
|
|
All, Verbose bool // cluster status: -a -v
|
2026-05-06 07:25:27 +02:00
|
|
|
Persistent, Transient, Default bool // -p -t -D cluster settings set
|
2026-05-13 13:51:09 +02:00
|
|
|
Force bool // ccr follower renew: -f
|
2026-05-18 13:56:07 +02:00
|
|
|
HaveJQ bool // determined at runtime by ourselfes
|
2026-04-21 10:50:09 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 09:11:10 +02:00
|
|
|
func NewConfig() *Config {
|
|
|
|
|
return &Config{Clusters: map[string]*Cluster{}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *Config) Init() error {
|
2026-04-29 10:26:53 +02:00
|
|
|
DefaultConfig := os.Getenv("HOME") + "/.config/esctl/config.yaml"
|
|
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
case fileExists(DefaultConfig):
|
|
|
|
|
conf.ConfigFile = DefaultConfig
|
|
|
|
|
fallthrough
|
|
|
|
|
case conf.ConfigFile != "":
|
2026-04-27 09:11:10 +02:00
|
|
|
if err := conf.LoadConfig(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-04-29 10:26:53 +02:00
|
|
|
default:
|
2026-04-27 09:11:10 +02:00
|
|
|
if err := conf.LoadEnv(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 15:01:18 +02:00
|
|
|
if err := conf.SetupES(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-05-06 13:02:28 +02:00
|
|
|
|
2026-04-27 09:11:10 +02:00
|
|
|
if conf.CurrentCluster != "" {
|
|
|
|
|
current, exists := conf.Clusters[conf.CurrentCluster]
|
|
|
|
|
if !exists {
|
|
|
|
|
return fmt.Errorf("no cluster with alias %s configured", conf.CurrentCluster)
|
|
|
|
|
} else {
|
|
|
|
|
conf.DefaultCluster = current
|
|
|
|
|
}
|
2026-05-06 13:02:28 +02:00
|
|
|
} else {
|
|
|
|
|
if len(conf.Clusters) == 1 {
|
2026-05-11 10:21:43 +02:00
|
|
|
for name, cluster := range conf.Clusters {
|
2026-05-06 13:02:28 +02:00
|
|
|
conf.DefaultCluster = cluster
|
2026-05-11 10:21:43 +02:00
|
|
|
conf.CurrentCluster = name
|
2026-05-06 13:02:28 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-05-11 10:21:43 +02:00
|
|
|
for name, cluster := range conf.Clusters {
|
2026-05-06 13:02:28 +02:00
|
|
|
_, err := cluster.ES.Cluster.Health().
|
|
|
|
|
Header("content-type", "application/json").
|
|
|
|
|
Header("accept", "application/json").
|
|
|
|
|
Do(context.Background())
|
|
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
conf.DefaultCluster = cluster
|
2026-05-11 10:21:43 +02:00
|
|
|
conf.CurrentCluster = name
|
2026-05-06 13:02:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-27 09:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-18 13:56:07 +02:00
|
|
|
conf.HaveJQ = isJQinstalled()
|
|
|
|
|
|
2026-05-11 10:42:58 +02:00
|
|
|
conf.PrintDebug()
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 10:46:17 +02:00
|
|
|
func (conf *Config) PrintDebug() {
|
2026-05-11 10:42:58 +02:00
|
|
|
if !conf.Debug {
|
2026-05-11 10:46:17 +02:00
|
|
|
return
|
2026-05-11 10:42:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clone := *conf
|
|
|
|
|
|
|
|
|
|
for name := range clone.Clusters {
|
|
|
|
|
clone.Clusters[name] = nil
|
|
|
|
|
}
|
|
|
|
|
clone.DefaultCluster = nil
|
|
|
|
|
|
|
|
|
|
fmt.Println("config:")
|
|
|
|
|
repr.Println(clone)
|
2026-04-27 09:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *Config) LoadEnv() error {
|
|
|
|
|
cluster := Cluster{
|
2026-04-21 10:50:09 +02:00
|
|
|
Uri: os.Getenv("ES_URI"),
|
|
|
|
|
User: os.Getenv("ES_USER"),
|
|
|
|
|
Pass: os.Getenv("ES_PASS"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch {
|
2026-04-27 09:11:10 +02:00
|
|
|
case cluster.Uri == "":
|
|
|
|
|
return errors.New("ES_URI unset")
|
|
|
|
|
case cluster.User == "":
|
|
|
|
|
return errors.New("ES_USER unset")
|
|
|
|
|
case cluster.Pass == "":
|
|
|
|
|
return errors.New("ES_PASS unset")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conf.Clusters["default"] = &cluster
|
|
|
|
|
conf.DefaultCluster = &cluster
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *Config) LoadConfig() error {
|
|
|
|
|
if conf.ConfigFile == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := os.ReadFile(conf.ConfigFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to read config file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newconf := &Config{}
|
|
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(data, newconf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to unmarshal config file: %w", err)
|
2026-04-21 10:50:09 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 09:11:10 +02:00
|
|
|
if len(newconf.Clusters) > 0 {
|
|
|
|
|
conf.Clusters = newconf.Clusters
|
|
|
|
|
|
|
|
|
|
_, exists := conf.Clusters["default"]
|
|
|
|
|
if !exists {
|
|
|
|
|
// no "default", just use the first we stumble upon
|
|
|
|
|
for _, cluster := range conf.Clusters {
|
|
|
|
|
conf.DefaultCluster = cluster
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
conf.DefaultCluster = newconf.Clusters["default"]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (conf *Config) SetupES() error {
|
|
|
|
|
for _, cluster := range conf.Clusters {
|
|
|
|
|
es, err := elasticsearch.NewTyped(
|
|
|
|
|
elasticsearch.WithAddresses(cluster.Uri),
|
|
|
|
|
elasticsearch.WithBasicAuth(cluster.User, cluster.Pass),
|
|
|
|
|
elasticsearch.WithTransportOptions(
|
|
|
|
|
elastictransport.WithTransport(
|
|
|
|
|
&http.Transport{
|
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
|
},
|
|
|
|
|
),
|
2026-04-21 10:50:09 +02:00
|
|
|
),
|
2026-04-27 09:11:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to setup elasticsearch connection: %w", err)
|
|
|
|
|
}
|
2026-04-21 10:50:09 +02:00
|
|
|
|
2026-04-27 09:11:10 +02:00
|
|
|
cluster.ES = es
|
|
|
|
|
}
|
2026-04-21 10:50:09 +02:00
|
|
|
|
2026-04-27 09:11:10 +02:00
|
|
|
return nil
|
2026-04-21 10:50:09 +02:00
|
|
|
}
|
2026-04-29 10:26:53 +02:00
|
|
|
|
|
|
|
|
func fileExists(filename string) bool {
|
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
// return false on any error
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !info.IsDir()
|
|
|
|
|
}
|