mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 15:34:18 +02:00
118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
/*
|
|
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"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
func ClusterSettingsNames(conf *cfg.Config) ([]string, error) {
|
|
return validClusterSettings, nil
|
|
}
|
|
|
|
func ClusterSettingsList(conf *cfg.Config) error {
|
|
res, err := conf.DefaultCluster.ES().Cluster.GetSettings().
|
|
FlatSettings(true).
|
|
Do(context.Background())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get cluster settings: %w", esErrorString(err))
|
|
}
|
|
|
|
table := printer.NewTable(conf, 2, 0)
|
|
table.Addheaders("setting", "value")
|
|
|
|
entries := [][]any{}
|
|
|
|
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 {
|
|
entries = append(entries, []any{topic, string(val)})
|
|
}
|
|
|
|
table.Entries = entries
|
|
table.Sort()
|
|
|
|
if err := table.Print(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func ClusterSettingsSet(conf *cfg.Config, args cli.Args) error {
|
|
put := conf.DefaultCluster.ES().Cluster.PutSettings()
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
put.AddPersistent(setting, message)
|
|
}
|
|
}
|
|
|
|
_, err := put.Do(context.Background())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to set settings: %w", esErrorString(err))
|
|
}
|
|
|
|
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
|
|
}
|