mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 07:54:17 +02:00
add doc show, enhance search, add jsonpath to search and doc show
This commit is contained in:
64
cmd/doc.go
64
cmd/doc.go
@@ -33,6 +33,7 @@ func Doc(conf *cfg.Config) *cli.Command {
|
||||
|
||||
Commands: []*cli.Command{
|
||||
DocAdd(conf),
|
||||
DocShow(conf),
|
||||
//Delete(conf),
|
||||
},
|
||||
}
|
||||
@@ -43,16 +44,71 @@ func DocAdd(conf *cfg.Config) *cli.Command {
|
||||
Name: "add",
|
||||
Aliases: []string{"+"},
|
||||
Usage: "add JSON document index",
|
||||
UsageText: "add [options] <index> '<json-doc>'",
|
||||
UsageText: "add [options] -i <index> '<json-doc>'",
|
||||
|
||||
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() != 2 {
|
||||
return errors.New("missing arguments: <index> <json-doc>")
|
||||
if args.Len() != 1 {
|
||||
return errors.New("missing arguments: <json-doc>")
|
||||
}
|
||||
|
||||
return es.DocAdd(conf, cmd.Args().Get(0), cmd.Args().Get(1))
|
||||
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 <index> <id>",
|
||||
|
||||
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"},
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "help-jsonpath",
|
||||
Usage: "show jsonPath help",
|
||||
Destination: &conf.Subhelp,
|
||||
Aliases: []string{"H"},
|
||||
},
|
||||
},
|
||||
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
if conf.Subhelp {
|
||||
return showJsonPathHelp()
|
||||
}
|
||||
|
||||
args := cmd.Args()
|
||||
|
||||
if args.Len() != 1 {
|
||||
return errors.New("missing arguments: <id>")
|
||||
}
|
||||
|
||||
return es.DocShow(conf, cmd.Args().Get(0))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"codeberg.org/scip/esctl/pkg/cfg"
|
||||
"codeberg.org/scip/esctl/pkg/es"
|
||||
@@ -28,10 +28,20 @@ import (
|
||||
|
||||
func Search(conf *cfg.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "search",
|
||||
Aliases: []string{"/"},
|
||||
Usage: "search within an index",
|
||||
UsageText: "search [options] <field=pattern> ...",
|
||||
Name: "search",
|
||||
Aliases: []string{"/"},
|
||||
Usage: "search within an index",
|
||||
UsageText: `search [options] [<[field<sep>]pattern> ...]
|
||||
|
||||
<sep> might be one of:
|
||||
=: Must match
|
||||
!=: Must not match
|
||||
?: Should match
|
||||
|
||||
You can omit a field spec and thereby search across all fields.
|
||||
|
||||
You can also search multiple fields by separating them with comma, eg:
|
||||
user,group=root`,
|
||||
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
@@ -57,19 +67,63 @@ func Search(conf *cfg.Config) *cli.Command {
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "filter",
|
||||
Usage: "additional filters. format: key=value",
|
||||
Usage: "additional boolean filters. format: key=value",
|
||||
Destination: &conf.Filter,
|
||||
Aliases: []string{"F"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "jsonpath",
|
||||
Usage: "jsonPath filter (e.g. source.message)",
|
||||
Destination: &conf.Path,
|
||||
Aliases: []string{"p"},
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "help-jsonpath",
|
||||
Usage: "show jsonPath help",
|
||||
Destination: &conf.Subhelp,
|
||||
Aliases: []string{"H"},
|
||||
},
|
||||
},
|
||||
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
args := cmd.Args()
|
||||
if args.Len() == 0 {
|
||||
return errors.New("at least one query must be specified (format: field=pattern)")
|
||||
if conf.Subhelp {
|
||||
return showJsonPathHelp()
|
||||
}
|
||||
|
||||
args := cmd.Args()
|
||||
|
||||
return es.Search(conf, args.Slice())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func showJsonPathHelp() error {
|
||||
_, err := fmt.Println(`jsonPath usage:
|
||||
|
||||
name.last >> "Anderson"
|
||||
age >> 37
|
||||
children >> ["Sara","Alex","Jack"]
|
||||
children.# >> 3
|
||||
children.1 >> "Alex"
|
||||
child*.2 >> "Jack"
|
||||
c?ildren.0 >> "Sara"
|
||||
fav\.movie >> "Deer Hunter"
|
||||
friends.#.first >> ["Dale","Roger","Jane"]
|
||||
friends.1.last >> "Craig"
|
||||
|
||||
You can also query an array for the first match by using #(...), or
|
||||
find all matches with #(...)#. Queries support the ==, !=, <, <=, >,
|
||||
>= comparison operators and the simple pattern matching % (like) and
|
||||
!% (not like) operators. Eg:
|
||||
|
||||
friends.#(last=="Murphy").first >> "Dale"
|
||||
friends.#(last=="Murphy")#.first >> ["Dale","Jane"]
|
||||
friends.#(age>45)#.last >> ["Craig","Murphy"]
|
||||
friends.#(first%"D*").last >> "Murphy"
|
||||
friends.#(first!%"D*").last >> "Craig"
|
||||
friends.#(nets.#(=="fb"))#.first >> ["Dale","Roger"]
|
||||
|
||||
Documentation: https://github.com/tidwall/gjson/blob/master/SYNTAX.md`)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user