158 lines
3.1 KiB
Go
Raw Normal View History

2024-03-22 18:05:47 +08:00
package cmd
import (
2024-03-25 20:22:18 +08:00
"context"
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-25 20:22:18 +08:00
err error
ioi interfaces.DumpIO
ioo interfaces.DumpIO
2024-03-22 18:05:47 +08:00
)
if opt.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
2024-03-25 20:22:18 +08:00
switch f_type {
case "data", "mapping", "setting":
default:
return fmt.Errorf("unknown type=%s", f_type)
}
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-25 20:22:18 +08:00
defer func() {
_ = ioi.Close()
_ = ioo.Close()
}()
switch f_type {
case "data":
return executeData(cmd.Context(), ioi, ioo)
case "mapping":
var mapping map[string]any
if mapping, err = ioi.ReadMapping(); err != nil {
return err
}
return ioo.WriteMapping(mapping)
case "setting":
var setting map[string]any
if setting, err = ioi.ReadSetting(); err != nil {
return err
}
return ioo.WriteSetting(setting)
default:
return fmt.Errorf("unknown type=%s", f_type)
}
}
func executeData(ctx context.Context, input, output interfaces.DumpIO) error {
var (
err error
lines []*interfaces.ESSource
succeed int
)
2024-03-23 21:25:19 +08:00
for {
select {
2024-03-25 20:22:18 +08:00
case <-ctx.Done():
2024-03-23 21:25:19 +08:00
default:
2024-03-22 18:05:47 +08:00
2024-03-25 20:22:18 +08:00
if lines, err = input.ReadData(f_limit); err != nil {
2024-03-23 21:25:19 +08:00
if errors.Is(err, io.EOF) {
return nil
}
return err
}
if len(lines) == 0 {
return nil
}
2024-03-25 20:22:18 +08:00
if succeed, err = output.WriteData(lines); err != nil {
2024-03-23 21:25:19 +08:00
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
}