Files
esctl/pkg/es/cluster_settings.go

118 lines
2.9 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"
"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 ClusterSettingsNames(conf *cfg.Config) ([]string, error) {
return validClusterSettings, nil
}
2026-05-11 10:28:25 +02:00
func ClusterSettingsList(conf *cfg.Config) error {
res, err := conf.DefaultCluster.ES().Cluster.GetSettings().
FlatSettings(true).
2026-05-11 10:28:25 +02:00
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get cluster settings: %w", esErrorString(err))
2026-05-11 10:28:25 +02:00
}
2026-08-09 21:53:47 +02:00
table := printer.NewTable(conf).WithSize(2, 0).
WithHeaders("setting", "value")
2026-07-07 23:46:43 +02:00
2026-07-07 07:29:03 +02:00
entries := [][]any{}
2026-05-11 10:28:25 +02:00
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 {
2026-07-07 07:29:03 +02:00
entries = append(entries, []any{topic, string(val)})
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: %w", value, err)
2026-05-11 10:28:25 +02:00
}
2026-07-07 23:46:43 +02:00
2026-05-11 10:28:25 +02:00
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: %w", value, err)
2026-05-11 10:28:25 +02:00
}
2026-07-07 23:46:43 +02:00
2026-05-11 10:28:25 +02:00
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: %w", 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: %w", value, err)
}
_, err = conf.DefaultCluster.ES().Cluster.PutSettings().
AddPersistent(setting, message).
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to set %s: %w", setting, esErrorString(err))
}
return nil
}