-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump.go
62 lines (54 loc) · 1.67 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package shell
import (
"fmt"
"github.com/fatih/color"
"github.com/nao1215/sqly/config"
"github.com/nao1215/sqly/domain/model"
)
// dumpCommand dump specified table to csv file
func (c CommandList) dumpCommand(s *Shell, argv []string) error {
if len(argv) != 2 {
fmt.Fprintln(config.Stdout, "[Usage]")
fmt.Fprintln(config.Stdout, " .dump TABLE_NAME FILE_PATH")
fmt.Fprintln(config.Stdout, "[Note]")
fmt.Fprintln(config.Stdout, " Output will be in the format specified in .mode.")
fmt.Fprintln(config.Stdout, " table mode is not available in .dump. If mode is table, .dump output CSV file.")
return nil
}
table, err := s.sqlite3Interactor.List(s.Ctx, argv[0])
if err != nil {
return err
}
if err := dumpToFile(s, argv[1], table); err != nil {
return err
}
fmt.Fprintf(config.Stdout, "dump `%s` table to %s (mode=%s)\n",
color.CyanString(argv[0]), color.HiCyanString(argv[1]), dumpMode(s.argument.Output.Mode))
return nil
}
// dumpToFile is dump table data to file.
func dumpToFile(s *Shell, filePath string, table *model.Table) error {
var err error
switch s.argument.Output.Mode { //nolint
case model.PrintModeCSV:
err = s.csvInteractor.Dump(filePath, table)
case model.PrintModeTSV:
err = s.tsvInteractor.Dump(filePath, table)
case model.PrintModeLTSV:
err = s.ltsvInteractor.Dump(filePath, table)
case model.PrintModeJSON:
err = s.jsonInteractor.Dump(filePath, table)
case model.PrintModeExcel:
err = s.excelInteractor.Dump(filePath, table)
default:
err = s.csvInteractor.Dump(filePath, table)
}
return err
}
// dumpMode is dump mode.
func dumpMode(m model.PrintMode) string {
if m == model.PrintModeTable {
return "csv"
}
return m.String()
}