fix panic if using 'cluser ls -d' (#45)

This commit is contained in:
T. von Dein
2026-06-23 10:54:00 +02:00
parent 355e3051b4
commit 6d31d18e0c

View File

@@ -26,6 +26,7 @@ import (
"log/slog"
"net/http"
"os"
"reflect"
"github.com/alecthomas/repr"
"github.com/elastic/elastic-transport-go/v8/elastictransport"
@@ -162,6 +163,7 @@ func (conf *Config) Init() error {
if err == nil {
conf.DefaultCluster = cluster
conf.CurrentCluster = name
break
}
}
}
@@ -174,17 +176,33 @@ func (conf *Config) Init() error {
return nil
}
// we are using reflect to clone a config obj w/o the ES stuff for
// shorter repr.Println() output (the ES structure is just too large)
func (conf *Config) Clone() Config {
clone := Config{}
ref := reflect.ValueOf(*conf)
typeOfS := ref.Type()
for i := 0; i < ref.NumField(); i++ {
field := typeOfS.Field(i).Name
if field == "Clusters" || field == "DefaultCluster" || !ref.Field(i).CanInterface() {
continue
}
reflect.ValueOf(&clone).Elem().FieldByName(field).Set(reflect.ValueOf(ref.Field(i).Interface()))
}
return clone
}
func (conf *Config) PrintDebug() {
if !conf.Debug {
return
}
clone := *conf
for name := range clone.Clusters {
clone.Clusters[name] = nil
}
clone.DefaultCluster = nil
clone := conf.Clone()
fmt.Println("config:")
repr.Println(clone)