2026-04-21 15:05:15 +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"
|
2026-04-22 13:38:02 +02:00
|
|
|
"fmt"
|
2026-04-21 15:05:15 +02:00
|
|
|
"log/slog"
|
|
|
|
|
|
|
|
|
|
"codeberg.org/scip/esctl/pkg/cfg"
|
2026-04-22 13:38:02 +02:00
|
|
|
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/healthstatus"
|
2026-04-21 15:05:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func IndexList(conf *cfg.Config) error {
|
2026-04-22 13:38:02 +02:00
|
|
|
cat := conf.ES.Cat.Indices()
|
|
|
|
|
|
|
|
|
|
if conf.Failed {
|
|
|
|
|
cat = cat.Health(healthstatus.Red)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := cat.Do(context.Background())
|
2026-04-21 15:05:15 +02:00
|
|
|
if err != nil {
|
2026-04-22 13:38:02 +02:00
|
|
|
return fmt.Errorf("Error getting indicies: %s", err)
|
2026-04-21 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog.Debug("ES result", "indicies", res)
|
|
|
|
|
|
|
|
|
|
size := len(res)
|
|
|
|
|
|
|
|
|
|
if conf.MaxItems > 0 {
|
|
|
|
|
if size > conf.MaxItems {
|
|
|
|
|
size = conf.MaxItems
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 13:38:02 +02:00
|
|
|
table := NewTable(3, size)
|
|
|
|
|
table.Addheaders("name", "size", "docscount")
|
2026-04-21 15:05:15 +02:00
|
|
|
|
|
|
|
|
for idx, index := range res {
|
|
|
|
|
name := Colorize(*index.Health, *index.Index)
|
|
|
|
|
|
|
|
|
|
table.entries[idx] = []string{name, *index.DatasetSize, *index.DocsCount}
|
|
|
|
|
|
|
|
|
|
if idx == size-1 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table.Sort()
|
|
|
|
|
table.PrintMarkdown()
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-04-22 13:38:02 +02:00
|
|
|
|
|
|
|
|
func IndexShow(conf *cfg.Config, index string) error {
|
|
|
|
|
res, err := conf.ES.Indices.Get(index).Do(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to get index: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog.Debug("ES result", "index", res)
|
|
|
|
|
|
|
|
|
|
// FIXME: add table printer like SnapshotShow
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|