wip: basic feat
This commit is contained in:
5
internal/cmd/init.go
Normal file
5
internal/cmd/init.go
Normal file
@ -0,0 +1,5 @@
|
||||
package cmd
|
||||
|
||||
func init() {
|
||||
initRootCommand()
|
||||
}
|
25
internal/cmd/root.go
Normal file
25
internal/cmd/root.go
Normal file
@ -0,0 +1,25 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"esgo2dump/internal/opt"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rootCommand = &cobra.Command{
|
||||
Use: "esgo2dump",
|
||||
Short: "esgo2dump is alternative to elasticdump",
|
||||
RunE: run,
|
||||
}
|
||||
|
||||
input string
|
||||
output string
|
||||
limit int
|
||||
)
|
||||
|
||||
func initRootCommand() {
|
||||
rootCommand.Flags().BoolVar(&opt.Debug, "debug", false, "")
|
||||
rootCommand.Flags().StringVarP(&input, "input", "i", "https://127.0.0.1:9200", "")
|
||||
rootCommand.Flags().StringVarP(&output, "output", "o", "output.json", "")
|
||||
rootCommand.Flags().IntVarP(&limit, "limit", "l", 100, "")
|
||||
}
|
78
internal/cmd/run.go
Normal file
78
internal/cmd/run.go
Normal file
@ -0,0 +1,78 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"esgo2dump/internal/es"
|
||||
"esgo2dump/internal/interfaces"
|
||||
"esgo2dump/internal/opt"
|
||||
"esgo2dump/internal/xfile"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"net/url"
|
||||
"os"
|
||||
)
|
||||
|
||||
func run(cmd *cobra.Command, args []string) error {
|
||||
var (
|
||||
err error
|
||||
ioi interfaces.DumpIO
|
||||
ioo interfaces.DumpIO
|
||||
)
|
||||
|
||||
if opt.Debug {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
|
||||
if ioi, err = newIO(input); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ioo, err = newIO(output); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = ioi
|
||||
_ = ioo
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func newIO(source string) (interfaces.DumpIO, error) {
|
||||
var (
|
||||
err error
|
||||
iurl *url.URL
|
||||
file *os.File
|
||||
)
|
||||
|
||||
logrus.Debugf("newIO: source string=%s", source)
|
||||
|
||||
if iurl, err = url.Parse(source); err != nil {
|
||||
logrus.Debugf("newIO: url parse source err=%v", err)
|
||||
goto ClientByFile
|
||||
}
|
||||
|
||||
if iurl.Scheme == "" {
|
||||
logrus.Debugf("newIO: url scheme default to 'http'")
|
||||
iurl.Scheme = "http"
|
||||
}
|
||||
|
||||
if !(iurl.Scheme == "http" || iurl.Scheme == "https") {
|
||||
logrus.Debugf("newIO: url scheme=%s invalid", iurl.Scheme)
|
||||
goto ClientByFile
|
||||
}
|
||||
|
||||
if iurl.Host == "" {
|
||||
logrus.Debug("newIO: url host empty")
|
||||
goto ClientByFile
|
||||
}
|
||||
|
||||
logrus.Debugf("newIO: source as url=%+v", *iurl)
|
||||
|
||||
return es.NewClient(iurl)
|
||||
|
||||
ClientByFile:
|
||||
if file, err = os.OpenFile(source, os.O_CREATE|os.O_RDWR, 0644); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return xfile.NewClient(file)
|
||||
}
|
7
internal/cmd/start.go
Normal file
7
internal/cmd/start.go
Normal file
@ -0,0 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import "context"
|
||||
|
||||
func Start(ctx context.Context) error {
|
||||
return rootCommand.ExecuteContext(ctx)
|
||||
}
|
65
internal/es/client.go
Normal file
65
internal/es/client.go
Normal file
@ -0,0 +1,65 @@
|
||||
package es
|
||||
|
||||
import (
|
||||
"esgo2dump/internal/interfaces"
|
||||
"fmt"
|
||||
elastic "github.com/elastic/go-elasticsearch/v8"
|
||||
"github.com/elastic/go-elasticsearch/v8/esapi"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func NewClient(url *url.URL) (interfaces.DumpIO, error) {
|
||||
|
||||
var (
|
||||
err error
|
||||
endpoint = fmt.Sprintf("%s://%s:%s", url.Scheme, url.Host, url.Port())
|
||||
c *elastic.Client
|
||||
infoResp *esapi.Response
|
||||
)
|
||||
|
||||
logrus.Debugf("es.NewClient: endpoint=%s", endpoint)
|
||||
|
||||
if c, err = elastic.NewClient(
|
||||
elastic.Config{
|
||||
Addresses: []string{endpoint},
|
||||
Username: "",
|
||||
Password: "",
|
||||
CACert: nil,
|
||||
RetryOnStatus: []int{429},
|
||||
MaxRetries: 3,
|
||||
RetryBackoff: nil,
|
||||
},
|
||||
); err != nil {
|
||||
logrus.Debugf("es.NewClient: elastic new client with endpont=%s err=%v", endpoint, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if infoResp, err = c.Info(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if infoResp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("info es status=%d", infoResp.StatusCode)
|
||||
}
|
||||
|
||||
return &client{c: c}, nil
|
||||
}
|
||||
|
||||
type client struct {
|
||||
c *elastic.Client
|
||||
}
|
||||
|
||||
func (c *client) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c client) Write(docs []map[string]any) (int, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c client) Read(i int) ([]map[string]any, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
7
internal/interfaces/dumpio.go
Normal file
7
internal/interfaces/dumpio.go
Normal file
@ -0,0 +1,7 @@
|
||||
package interfaces
|
||||
|
||||
type DumpIO interface {
|
||||
Write(docs []map[string]any) (int, error)
|
||||
Read(int) ([]map[string]any, error)
|
||||
Close() error
|
||||
}
|
5
internal/opt/var.go
Normal file
5
internal/opt/var.go
Normal file
@ -0,0 +1,5 @@
|
||||
package opt
|
||||
|
||||
var (
|
||||
Debug bool
|
||||
)
|
17
internal/util/ctx.go
Normal file
17
internal/util/ctx.go
Normal file
@ -0,0 +1,17 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Timeout(seconds ...int) context.Context {
|
||||
second := 30
|
||||
if len(seconds) > 0 && seconds[0] > 0 {
|
||||
second = seconds[0]
|
||||
}
|
||||
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(second)*time.Second)
|
||||
|
||||
return ctx
|
||||
}
|
29
internal/xfile/xfile.go
Normal file
29
internal/xfile/xfile.go
Normal file
@ -0,0 +1,29 @@
|
||||
package xfile
|
||||
|
||||
import (
|
||||
"esgo2dump/internal/interfaces"
|
||||
"os"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
f *os.File
|
||||
}
|
||||
|
||||
func (c client) Write(docs []map[string]any) (int, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c client) Read(i int) ([]map[string]any, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c client) Close() error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func NewClient(file *os.File) (interfaces.DumpIO, error) {
|
||||
return &client{f: file}, nil
|
||||
}
|
Reference in New Issue
Block a user