wip: basic feat

This commit is contained in:
loveuer
2024-03-22 18:05:47 +08:00
commit 983533237f
13 changed files with 327 additions and 0 deletions

5
internal/cmd/init.go Normal file
View File

@ -0,0 +1,5 @@
package cmd
func init() {
initRootCommand()
}

25
internal/cmd/root.go Normal file
View 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
View 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
View File

@ -0,0 +1,7 @@
package cmd
import "context"
func Start(ctx context.Context) error {
return rootCommand.ExecuteContext(ctx)
}