/* 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 . */ package cmd import ( "context" "errors" "codeberg.org/scip/esctl/pkg/cfg" "codeberg.org/scip/esctl/pkg/es" "github.com/urfave/cli/v3" ) func Doc(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "doc", Usage: "manage documents", Commands: []*cli.Command{ DocAdd(conf), DocShow(conf), DocDelete(conf), }, } } func DocAdd(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "add", Aliases: []string{"+"}, Usage: "add JSON document index", UsageText: "add [options] -i ''", Flags: []cli.Flag{ &cli.StringFlag{ Name: "index", Usage: "index to work on", Sources: cli.EnvVars("ES_INDEX"), Destination: &conf.Index, Aliases: []string{"i"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { args := cmd.Args() if args.Len() != 1 { return errors.New("missing arguments: ") } return es.DocAdd(conf, cmd.Args().Get(0)) }, } } func DocShow(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "show", Aliases: []string{"sh"}, Usage: "show a JSON document", UsageText: "show [options] -i ", Flags: []cli.Flag{ &cli.StringFlag{ Name: "index", Usage: "index to work on", Sources: cli.EnvVars("ES_INDEX"), Destination: &conf.Index, Aliases: []string{"i"}, }, &cli.StringFlag{ Name: "path", Usage: "jsonPath filter (e.g. source.message)", Destination: &conf.Path, Aliases: []string{"p"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { args := cmd.Args() if args.Len() != 1 { return errors.New("missing arguments: ") } return es.DocShow(conf, cmd.Args().Get(0)) }, } } func DocDelete(conf *cfg.Config) *cli.Command { return &cli.Command{ Name: "delete", Aliases: []string{"rm"}, Usage: "delete JSON document[s] from index[es]", UsageText: "delete [options] -i [<[field]pattern> ...]\n" + SearchUsage, Flags: []cli.Flag{ &cli.BoolFlag{ Name: "all", Usage: "delete all docs (dangerous!)", Destination: &conf.All, Aliases: []string{"a"}, }, &cli.StringFlag{ Name: "index", Usage: "index to work on", Sources: cli.EnvVars("ES_INDEX"), Destination: &conf.Index, Aliases: []string{"i"}, }, &cli.StringSliceFlag{ Name: "filter", Usage: "additional boolean filters. format: key=value", Destination: &conf.Filter, Aliases: []string{"F"}, }, &cli.StringFlag{ Name: "timerange", Usage: "field: to (e.g. @timestamp:2026-05-05 to 2026-05-15)", Destination: &conf.Range, Aliases: []string{"r"}, }, &cli.StringFlag{ Name: "timestamp-format", Usage: "a valid ES builtin timestamp or custom format", Destination: &conf.TimestampFormat, Value: "strict_date_hour_minute", }, &cli.BoolFlag{ Name: "or", Usage: "logical operator (default: and)", Destination: &conf.Or, Aliases: []string{"O"}, }, }, Action: func(ctx context.Context, cmd *cli.Command) error { args := cmd.Args() if args.Len() == 0 && !conf.All { return errors.New("missing arguments: ") } return es.DocDelete(conf, cmd.Args().Slice()) }, } }