Files
esctl/pkg/es/cluster_settings.go

124 lines
3.1 KiB
Go
Raw Normal View History

2026-05-11 10:28:25 +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 es
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/printer"
2026-05-11 10:28:25 +02:00
"github.com/urfave/cli/v3"
)
func ClusterSettingsList(conf *cfg.Config) error {
res, err := conf.DefaultCluster.ES().Cluster.GetSettings().
2026-05-11 10:28:25 +02:00
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get cluster settings: %s", esErrorString(err))
2026-05-11 10:28:25 +02:00
}
table := printer.NewTable(conf, 2, 0)
2026-05-11 10:28:25 +02:00
table.Addheaders("setting", "value")
entries := [][]string{}
settingshash := res.Persistent // == map[string]json.RawMessage
switch {
case conf.Transient:
settingshash = res.Transient
case conf.Default:
settingshash = res.Defaults
}
for topic, val := range settingshash {
data := map[string]any{}
err := json.Unmarshal(val, &data)
if err != nil {
return fmt.Errorf("failed to unmarshall setting for topic %s: %s", topic, err)
}
2026-06-15 15:18:21 +02:00
paths := getJsonPath(map[string]string{}, data, topic)
2026-05-11 10:28:25 +02:00
slog.Debug("settings", topic, paths)
for setting, value := range paths {
2026-05-11 10:46:17 +02:00
entries = append(entries, []string{setting, fmt.Sprintf("%v", value)})
2026-05-11 10:28:25 +02:00
}
}
table.Entries = entries
2026-05-11 10:28:25 +02:00
table.Sort()
if err := table.Print(); err != nil {
2026-05-11 10:28:25 +02:00
return err
}
return nil
}
func ClusterSettingsSet(conf *cfg.Config, args cli.Args) error {
put := conf.DefaultCluster.ES().Cluster.PutSettings()
2026-05-11 10:28:25 +02:00
for _, arg := range args.Slice() {
setting, value := splitArg(arg)
switch {
case conf.Transient:
message, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("failed to marshall transient value <%v> to valid JSON: %s", value, err)
}
put.AddTransient(setting, message)
case conf.Persistent:
fallthrough
default:
message, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("failed to marshall persistent value <%v> to valid JSON: %s", value, err)
}
put.AddPersistent(setting, message)
}
}
_, err := put.Do(context.Background())
2026-05-11 10:28:25 +02:00
if err != nil {
return fmt.Errorf("failed to set settings: %s", esErrorString(err))
2026-05-11 10:28:25 +02:00
}
return nil
}
func ClusterSettingsSetSingle(conf *cfg.Config, setting, value string) error {
message, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("failed to marshall persistent value <%v> to valid JSON: %s", value, err)
}
_, err = conf.DefaultCluster.ES().Cluster.PutSettings().
AddPersistent(setting, message).
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to set %s: %s", setting, esErrorString(err))
}
return nil
}