From 6d31d18e0c54e7eb0ea84b79b6efb2c32c147e4e Mon Sep 17 00:00:00 2001 From: "T. von Dein" Date: Tue, 23 Jun 2026 10:54:00 +0200 Subject: [PATCH] fix panic if using 'cluser ls -d' (#45) --- pkg/cfg/config.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/pkg/cfg/config.go b/pkg/cfg/config.go index d962cab..6b0d6c1 100644 --- a/pkg/cfg/config.go +++ b/pkg/cfg/config.go @@ -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)