2024-03-22 18:05:47 +08:00
package cmd
import (
"context"
2024-03-26 17:23:10 +08:00
"github.com/loveuer/esgo2dump/internal/opt"
2024-03-22 18:05:47 +08:00
"github.com/spf13/cobra"
)
2025-02-07 18:00:10 +08:00
const (
example = `
2025-02-05 18:07:53 +08:00
esgo2dump - i https : //<user>:<password>@<es_node1_host>:<es_node1_port>,<es_node2_host>:<es_node2_port>/some_index?ping=false&sniff=false -o ./data.json
2024-03-22 18:05:47 +08:00
esgo2dump -- input = http : //127.0.0.1:9200/some_index --output=./data.json
esgo2dump -- input = http : //127.0.0.1:9200/some_index --output=http://192.168.1.1:9200/some_index --limit=5000
esgo2dump -- input = https : //username:password@127.0.0.1:9200/some_index --output=./data.json
2024-05-08 19:02:49 +08:00
esgo2dump -- input = http : //127.0.0.1:9200/some_index --source='id;name;age;address' --output=./data.json
2024-03-27 17:44:01 +08:00
esgo2dump -- input = http : //127.0.0.1:9200/some_index --output=./data.json --query='{"match": {"name": "some_name"}}'
2025-02-07 18:00:10 +08:00
esgo2dump -- input = http : //127.0.0.1:9200/some_index --output=./data.json --query_file=my_queries.json`
)
var rootCommand = & cobra . Command {
Use : "esgo2dump" ,
Short : "esgo2dump is alternative to elasticdump" ,
Example : example ,
SilenceUsage : true ,
SilenceErrors : true ,
PreRunE : preRun ,
RunE : run ,
2025-02-05 18:07:53 +08:00
}
2024-03-22 18:05:47 +08:00
2025-02-07 18:00:10 +08:00
func initRoot ( cmds ... * cobra . Command ) * cobra . Command {
2024-12-13 15:01:40 +08:00
rootCommand . PersistentFlags ( ) . BoolVar ( & opt . Cfg . Debug , "debug" , false , "" )
rootCommand . PersistentFlags ( ) . BoolVar ( & opt . Cfg . Dev , "dev" , false , "" )
2025-01-02 21:38:30 -08:00
rootCommand . PersistentFlags ( ) . BoolVar ( & opt . Cfg . DisablePing , "disable-ping" , false , "" )
2024-12-13 15:01:40 +08:00
rootCommand . PersistentFlags ( ) . BoolVarP ( & opt . Cfg . Args . Version , "version" , "v" , false , "print esgo2dump version" )
2024-03-22 18:05:47 +08:00
2025-02-07 18:00:10 +08:00
// rootCommand.Flags().IntVar(&opt.Cfg.Args.Timeout, "timeout", 30, "max timeout seconds per operation with limit")
2024-12-13 15:01:40 +08:00
rootCommand . Flags ( ) . StringVarP ( & opt . Cfg . Args . Input , "input" , "i" , "" , "*required: input file or es url (example :data.json / http://127.0.0.1:9200/my_index)" )
rootCommand . Flags ( ) . StringVarP ( & opt . Cfg . Args . Output , "output" , "o" , "output.json" , "" )
rootCommand . Flags ( ) . StringVarP ( & opt . Cfg . Args . Type , "type" , "t" , "data" , "data/mapping/setting" )
2025-02-05 18:07:53 +08:00
rootCommand . Flags ( ) . StringVar ( & opt . Cfg . Args . Field , "field" , "" , "query include field, use ',' to separate" )
rootCommand . Flags ( ) . StringVar ( & opt . Cfg . Args . Sort , "sort" , "" , "sort, <field>:<direction> format, for example: time:desc or name:asc, user ',' to separate" )
2024-12-13 15:01:40 +08:00
rootCommand . Flags ( ) . StringVar ( & opt . Cfg . Args . Query , "query" , "" , ` query dsl, example: { "bool": { "must":[ { "term": { "name": { "value":"some_name"}}}],"must_not":[ { "range": { "age": { "gte":18,"lt":60}}}]}} ` )
rootCommand . Flags ( ) . StringVar ( & opt . Cfg . Args . QueryFile , "query_file" , "" , ` query json file (will execute line by line) ` )
rootCommand . Flags ( ) . IntVar ( & opt . Cfg . Args . Limit , "limit" , 100 , "" )
2025-02-05 18:07:53 +08:00
rootCommand . Flags ( ) . IntVar ( & opt . Cfg . Args . Max , "max" , 0 , "max dump records" )
2025-02-07 18:00:10 +08:00
rootCommand . AddCommand ( cmds ... )
return rootCommand
2024-03-22 18:05:47 +08:00
}
2025-02-05 18:07:53 +08:00
func Run ( ctx context . Context ) error {
2024-03-22 18:05:47 +08:00
return rootCommand . ExecuteContext ( ctx )
}