cmd/csvqlctl/csvqlctl.go   A
last analyzed

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 73
dl 0
loc 104
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A csvqlctl.New 0 2 1
A csvqlctl.*csvQlCtl.Command 0 46 4
A csvqlctl.*csvQlCtl.runE 0 16 4
1
package csvqlctl
2
3
import (
4
	"adrianolaselva.github.io/csvql/internal/csvql"
5
	"fmt"
6
	"github.com/spf13/cobra"
7
)
8
9
const (
10
	fileParam               = "file"
11
	fileShortParam          = "f"
12
	fileDelimiterParam      = "delimiter"
13
	fileShortDelimiterParam = "d"
14
	queryParam              = "query"
15
	queryShortParam         = "q"
16
	storageParam            = "storage"
17
	storageShortParam       = "s"
18
	exportParam             = "export"
19
	exportShortParam        = "e"
20
	typeParam               = "type"
21
	typeShortParam          = "t"
22
	linesParam              = "lines"
23
	linesShortParam         = "l"
24
	tableNameParam          = "collection"
25
	tableNameShortParam     = "c"
26
)
27
28
type CsvQlCtl interface {
29
	Command() (*cobra.Command, error)
30
	runE(cmd *cobra.Command, args []string) error
31
}
32
33
type csvQlCtl struct {
34
	params csvql.Params
35
}
36
37
func New() CsvQlCtl {
38
	return &csvQlCtl{}
39
}
40
41
func (c *csvQlCtl) Command() (*cobra.Command, error) {
42
	command := &cobra.Command{
43
		Use:     "run",
44
		Short:   "Load and run queries from csv file",
45
		Long:    `./csvql run -f test.csv -d ";"`,
46
		Example: `./csvql run -f test.csv -d ";"`,
47
		RunE:    c.runE,
48
	}
49
50
	command.
51
		PersistentFlags().
52
		StringArrayVarP(&c.params.FileInputs, fileParam, fileShortParam, []string{}, "origin file in csv")
53
54
	command.
55
		PersistentFlags().
56
		StringVarP(&c.params.Delimiter, fileDelimiterParam, fileShortDelimiterParam, ",", "csv delimiter")
57
58
	command.
59
		PersistentFlags().
60
		StringVarP(&c.params.Query, queryParam, queryShortParam, "", "query param")
61
62
	command.
63
		PersistentFlags().
64
		StringVarP(&c.params.Export, exportParam, exportShortParam, "", "export path")
65
66
	command.
67
		PersistentFlags().
68
		StringVarP(&c.params.Type, typeParam, typeShortParam, "", "format type [`jsonl`,`csv`]")
69
70
	command.
71
		PersistentFlags().
72
		StringVarP(&c.params.DataSourceName, storageParam, storageShortParam, "", "sqlite file")
73
74
	command.
75
		PersistentFlags().
76
		IntVarP(&c.params.Lines, linesParam, linesShortParam, 0, "number of lines to be read")
77
78
	if err := command.MarkPersistentFlagRequired(fileParam); err != nil {
79
		return nil, fmt.Errorf("failed to validate flag %s: %w", fileParam, err)
80
	}
81
82
	if c.params.Export != "" && c.params.Type == "" {
83
		return nil, fmt.Errorf("failed to validate flag")
84
	}
85
86
	return command, nil
87
}
88
89
func (c *csvQlCtl) runE(cmd *cobra.Command, _ []string) error {
90
	cmd.SilenceUsage = true
91
	csvQl, err := csvql.New(c.params)
92
	if err != nil {
93
		return fmt.Errorf("failed to initialize csvql: %w", err)
94
	}
95
	defer func(csvQl csvql.Csvql) {
96
		_ = csvQl.Close()
97
	}(csvQl)
98
99
	err = csvQl.Run()
100
	if err != nil {
101
		return fmt.Errorf("failed to run csvql: %w", err)
102
	}
103
104
	return nil
105
}
106