79 lines
1.4 KiB
Go
Raw Normal View History

2024-03-22 18:05:47 +08:00
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)
}