120 lines
2.4 KiB
Go
Raw Normal View History

2024-03-22 18:05:47 +08:00
package cmd
import (
2024-03-23 21:25:19 +08:00
"encoding/json"
"errors"
2024-03-22 18:05:47 +08:00
"esgo2dump/internal/interfaces"
"esgo2dump/internal/opt"
2024-03-23 21:25:19 +08:00
"esgo2dump/internal/xes"
2024-03-22 18:05:47 +08:00
"esgo2dump/internal/xfile"
2024-03-23 21:25:19 +08:00
"fmt"
2024-03-22 18:05:47 +08:00
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2024-03-23 21:25:19 +08:00
"io"
2024-03-22 18:05:47 +08:00
"net/url"
"os"
)
func run(cmd *cobra.Command, args []string) error {
var (
2024-03-23 21:25:19 +08:00
err error
ioi interfaces.DumpIO
ioo interfaces.DumpIO
lines []*interfaces.ESSource
succeed int
2024-03-22 18:05:47 +08:00
)
if opt.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
2024-03-23 21:25:19 +08:00
if ioi, err = newIO(f_input, interfaces.IOInput); err != nil {
2024-03-22 18:05:47 +08:00
return err
}
2024-03-23 21:25:19 +08:00
if ioo, err = newIO(f_output, interfaces.IOOutput); err != nil {
2024-03-22 18:05:47 +08:00
return err
}
2024-03-23 21:25:19 +08:00
for {
select {
case <-cmd.Context().Done():
default:
succeed = 0
2024-03-22 18:05:47 +08:00
2024-03-23 21:25:19 +08:00
if lines, err = ioi.Read(f_limit); err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
if len(lines) == 0 {
return nil
}
if succeed, err = ioo.Write(lines); err != nil {
return err
}
if succeed != len(lines) {
return fmt.Errorf("cmd.run: got lines=%d, only succeed=%d", len(lines), succeed)
}
logrus.Infof("Dump: %d docs succeed!!!", succeed)
}
}
2024-03-22 18:05:47 +08:00
}
2024-03-23 21:25:19 +08:00
func newIO(source string, ioType interfaces.IO) (interfaces.DumpIO, error) {
2024-03-22 18:05:47 +08:00
var (
err error
iurl *url.URL
file *os.File
2024-03-23 21:25:19 +08:00
qm = make(map[string]any)
2024-03-22 18:05:47 +08:00
)
2024-03-23 21:25:19 +08:00
logrus.Debugf("newIO.%s: source string=%s", ioType.Code(), source)
2024-03-22 18:05:47 +08:00
if iurl, err = url.Parse(source); err != nil {
2024-03-23 21:25:19 +08:00
logrus.Debugf("newIO.%s: url parse source err=%v", ioType.Code(), err)
2024-03-22 18:05:47 +08:00
goto ClientByFile
}
if !(iurl.Scheme == "http" || iurl.Scheme == "https") {
2024-03-23 21:25:19 +08:00
logrus.Debugf("newIO.%s: url scheme=%s invalid", ioType.Code(), iurl.Scheme)
2024-03-22 18:05:47 +08:00
goto ClientByFile
}
if iurl.Host == "" {
2024-03-23 21:25:19 +08:00
logrus.Debugf("newIO.%s: url host empty", ioType.Code())
2024-03-22 18:05:47 +08:00
goto ClientByFile
}
2024-03-23 21:25:19 +08:00
if ioType == interfaces.IOInput && f_query != "" {
if err = json.Unmarshal([]byte(f_query), &qm); err != nil {
logrus.Debugf("newIO.%s: query=%s invalid to map[string]any", ioType.Code(), f_query)
return nil, fmt.Errorf("invalid query err=%v", err)
}
}
logrus.Debugf("newIO.%s: source as url=%+v", ioType.Code(), *iurl)
2024-03-22 18:05:47 +08:00
2024-03-23 21:25:19 +08:00
return xes.NewClient(iurl, qm)
2024-03-22 18:05:47 +08:00
ClientByFile:
2024-03-23 21:25:19 +08:00
if ioType == interfaces.IOOutput {
if _, err = os.Stat(source); !os.IsNotExist(err) {
return nil, fmt.Errorf("output_file=%s already exist", source)
}
}
2024-03-22 18:05:47 +08:00
if file, err = os.OpenFile(source, os.O_CREATE|os.O_RDWR, 0644); err != nil {
return nil, err
}
2024-03-23 21:25:19 +08:00
return xfile.NewClient(file, ioType)
2024-03-22 18:05:47 +08:00
}