Files
esctl/pkg/es/index.go

81 lines
1.8 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"
"fmt"
"log/slog"
"codeberg.org/scip/esctl/pkg/cfg"
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/healthstatus"
)
func IndexList(conf *cfg.Config) error {
cat := conf.DefaultCluster.ES.Cat.Indices()
if conf.Failed {
cat = cat.Health(healthstatus.Red)
}
res, err := cat.Do(context.Background())
if err != nil {
return fmt.Errorf("Error getting indicies: %s", err)
}
slog.Debug("ES result", "indicies", res)
size := len(res)
if conf.MaxItems > 0 {
if size > conf.MaxItems {
size = conf.MaxItems
}
}
table := NewTable(3, size)
table.Addheaders("name", "size", "docscount")
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
}
func IndexShow(conf *cfg.Config, index string) error {
res, err := conf.DefaultCluster.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
}