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)
|
||||
}
|
Reference in New Issue
Block a user