Skip to content

Commit 33a6a13

Browse files
authored
Merge 101163f into 8b93dfc
2 parents 8b93dfc + 101163f commit 33a6a13

12 files changed

+103
-53
lines changed

config/argument.go

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (a *Arg) NeedsOutputToFile() bool {
125125
return a.Output.FilePath != "" && a.Query != ""
126126
}
127127

128+
// usage return usage message.
128129
func usage(flag pflag.FlagSet) string {
129130
s := fmt.Sprintf("%s - execute SQL against CSV/TSV/LTSV/JSON with shell (%s)\n", color.GreenString("sqly"), GetVersion())
130131
s += "\n"
@@ -152,6 +153,7 @@ func usage(flag pflag.FlagSet) string {
152153
return s
153154
}
154155

156+
// version print version message.
155157
func version() {
156158
fmt.Fprintf(Stdout, "sqly %s\n", GetVersion())
157159
}

di/wire_gen.go

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/pages/auto_generate_files.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
### Auto generate files for Dependency Injection
2+
3+
In sqly, Dependency Injection is performed using `google/wire`. Initialization functions for each struct are aggregated in the `wire.go` file, and initialization functions for all packages are aggregated in the `di` package. To automatically generate the initialization code, please run the following command:
4+
5+
```shell
6+
go generate ./...
7+
```
8+
9+
or
10+
11+
```shell
12+
make generate
13+
```
14+
15+
### Auto generate files for Mock
16+
17+
In sqly, Mock is performed using `mockgen`. Mock files are generated in the `mock` package. To automatically generate the mock code, please run the following command:
18+
19+
```shell
20+
go generate ./...
21+
```
22+
23+
or
24+
25+
```shell
26+
make generate
27+
```

doc/pages/markdown/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ One of the unique strengths of `sqly` is that it allows you to interactively bui
1717
## Document for developers
1818

1919
- [How to build & test](./build_and_test.md)
20+
- [Auto-generate files](./auto_generate_files.md)
2021
- [Architecture](./architecture.md)
2122
- [Design Overview](./design_overview.md)
2223

shell/dump.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (c CommandList) dumpCommand(ctx context.Context, s *Shell, argv []string) e
2020
return nil
2121
}
2222

23-
table, err := s.sqlite3Interactor.List(ctx, argv[0])
23+
table, err := s.usecases.sqlite3.List(ctx, argv[0])
2424
if err != nil {
2525
return err
2626
}
@@ -39,21 +39,21 @@ func dumpToFile(s *Shell, filePath string, table *model.Table) error {
3939
var err error
4040
switch s.argument.Output.Mode {
4141
case model.PrintModeCSV:
42-
err = s.csvInteractor.Dump(filePath, table)
42+
err = s.usecases.csv.Dump(filePath, table)
4343
case model.PrintModeTSV:
44-
err = s.tsvInteractor.Dump(filePath, table)
44+
err = s.usecases.tsv.Dump(filePath, table)
4545
case model.PrintModeLTSV:
46-
err = s.ltsvInteractor.Dump(filePath, table)
46+
err = s.usecases.ltsv.Dump(filePath, table)
4747
case model.PrintModeJSON:
48-
err = s.jsonInteractor.Dump(filePath, table)
48+
err = s.usecases.json.Dump(filePath, table)
4949
case model.PrintModeExcel:
50-
err = s.excelInteractor.Dump(filePath, table)
50+
err = s.usecases.excel.Dump(filePath, table)
5151
case model.PrintModeMarkdownTable:
5252
fallthrough // TODO: support markdown table mode
5353
case model.PrintModeTable:
5454
fallthrough
5555
default:
56-
err = s.csvInteractor.Dump(filePath, table)
56+
err = s.usecases.csv.Dump(filePath, table)
5757
}
5858
return err
5959
}

shell/header.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (c CommandList) headerCommand(ctx context.Context, s *Shell, argv []string)
1818
return nil
1919
}
2020

21-
table, err := s.sqlite3Interactor.Header(ctx, argv[0])
21+
table, err := s.usecases.sqlite3.Header(ctx, argv[0])
2222
if err != nil {
2323
return err
2424
}

shell/import.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ func (c CommandList) importCommand(ctx context.Context, s *Shell, argv []string)
2525

2626
switch {
2727
case isCSV(v):
28-
table, err = s.csvInteractor.List(v)
28+
table, err = s.usecases.csv.List(v)
2929
if err != nil {
3030
return err
3131
}
3232
case isTSV(v):
33-
table, err = s.tsvInteractor.List(v)
33+
table, err = s.usecases.tsv.List(v)
3434
if err != nil {
3535
return err
3636
}
3737
case isLTSV(v):
38-
table, err = s.ltsvInteractor.List(v)
38+
table, err = s.usecases.ltsv.List(v)
3939
if err != nil {
4040
return err
4141
}
4242
case isJSON(v):
43-
table, err = s.jsonInteractor.List(v)
43+
table, err = s.usecases.json.List(v)
4444
if err != nil {
4545
return err
4646
}
@@ -56,7 +56,7 @@ func (c CommandList) importCommand(ctx context.Context, s *Shell, argv []string)
5656
return errors.New("sheet name is required")
5757
}
5858
}
59-
table, err = s.excelInteractor.List(v, sheetName)
59+
table, err = s.usecases.excel.List(v, sheetName)
6060
if err != nil {
6161
return err
6262
}
@@ -66,10 +66,10 @@ func (c CommandList) importCommand(ctx context.Context, s *Shell, argv []string)
6666
return errors.New("not support file format: " + path.Ext(v))
6767
}
6868

69-
if err := s.sqlite3Interactor.CreateTable(ctx, table); err != nil {
69+
if err := s.usecases.sqlite3.CreateTable(ctx, table); err != nil {
7070
return err
7171
}
72-
if err := s.sqlite3Interactor.Insert(ctx, table); err != nil {
72+
if err := s.usecases.sqlite3.Insert(ctx, table); err != nil {
7373
return err
7474
}
7575
}

shell/shell.go

+17-35
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/mattn/go-colorable"
1717
"github.com/nao1215/sqly/config"
1818
"github.com/nao1215/sqly/domain/model"
19-
"github.com/nao1215/sqly/usecase"
2019
)
2120

2221
var (
@@ -31,42 +30,24 @@ var (
3130
// Shell is main class of the sqly command.
3231
// Shell is the interface to the user and requests processing from the usecase layer.
3332
type Shell struct {
34-
argument *config.Arg
35-
config *config.Config
36-
commands CommandList
37-
csvInteractor usecase.CSVUsecase
38-
tsvInteractor usecase.TSVUsecase
39-
ltsvInteractor usecase.LTSVUsecase
40-
jsonInteractor usecase.JSONUsecase
41-
sqlite3Interactor usecase.DatabaseUsecase
42-
historyInteractor usecase.HistoryUsecase
43-
excelInteractor usecase.ExcelUsecase
33+
argument *config.Arg
34+
config *config.Config
35+
commands CommandList
36+
usecases Usecases
4437
}
4538

4639
// NewShell return *Shell.
4740
func NewShell(
4841
arg *config.Arg,
4942
cfg *config.Config,
5043
cmds CommandList,
51-
csv usecase.CSVUsecase,
52-
tsv usecase.TSVUsecase,
53-
ltsv usecase.LTSVUsecase,
54-
json usecase.JSONUsecase,
55-
sqlite3 usecase.DatabaseUsecase,
56-
history usecase.HistoryUsecase,
57-
excel usecase.ExcelUsecase,
44+
usecases Usecases,
5845
) *Shell {
5946
return &Shell{
60-
argument: arg,
61-
config: cfg,
62-
commands: cmds,
63-
csvInteractor: csv,
64-
tsvInteractor: tsv,
65-
ltsvInteractor: ltsv,
66-
jsonInteractor: json,
67-
sqlite3Interactor: sqlite3,
68-
historyInteractor: history,
69-
excelInteractor: excel,
47+
argument: arg,
48+
config: cfg,
49+
commands: cmds,
50+
usecases: usecases,
7051
}
7152
}
7253

@@ -130,7 +111,7 @@ func (s *Shell) communicate(ctx context.Context) error {
130111

131112
// init store CSV data to in-memory DB and create table for sqly history.
132113
func (s *Shell) init(ctx context.Context) error {
133-
if err := s.historyInteractor.CreateTable(ctx); err != nil {
114+
if err := s.usecases.history.CreateTable(ctx); err != nil {
134115
return fmt.Errorf("failed to create table for sqly history: %w", err)
135116
}
136117
if len(s.argument.FilePaths) == 0 {
@@ -150,7 +131,7 @@ func (s *Shell) printWelcomeMessage() {
150131

151132
// printPrompt print "sqly>" prompt and getting user input
152133
func (s *Shell) prompt(ctx context.Context) (string, error) {
153-
histories, err := s.historyInteractor.List(ctx)
134+
histories, err := s.usecases.history.List(ctx)
154135
if err != nil {
155136
return "", err
156137
}
@@ -175,6 +156,7 @@ func (s *Shell) prompt(ctx context.Context) (string, error) {
175156
prompt.OptionHistory(histories.ToStringList())), nil
176157
}
177158

159+
// completer return prompt.Suggest for auto-completion.
178160
func (s *Shell) completer(ctx context.Context, d prompt.Document) []prompt.Suggest {
179161
suggest := []prompt.Suggest{
180162
{Text: "SELECT", Description: "SQL: get records from table"},
@@ -217,7 +199,7 @@ func (s *Shell) completer(ctx context.Context, d prompt.Document) []prompt.Sugge
217199
})
218200
}
219201

220-
tables, err := s.sqlite3Interactor.TablesName(ctx)
202+
tables, err := s.usecases.sqlite3.TablesName(ctx)
221203
if err != nil {
222204
return prompt.FilterHasPrefix(suggest, d.GetWordBeforeCursor(), true)
223205
}
@@ -227,7 +209,7 @@ func (s *Shell) completer(ctx context.Context, d prompt.Document) []prompt.Sugge
227209
Description: "table: " + v.Name(),
228210
})
229211

230-
table, err := s.sqlite3Interactor.Header(ctx, v.Name())
212+
table, err := s.usecases.sqlite3.Header(ctx, v.Name())
231213
if err != nil {
232214
return prompt.FilterHasPrefix(suggest, d.GetWordBeforeCursor(), true)
233215
}
@@ -269,7 +251,7 @@ func (s *Shell) exec(ctx context.Context, request string) error {
269251

270252
func (s *Shell) execSQL(ctx context.Context, req string) error {
271253
req = strings.TrimRight(req, ";")
272-
table, affectedRows, err := s.sqlite3Interactor.ExecSQL(ctx, req)
254+
table, affectedRows, err := s.usecases.sqlite3.ExecSQL(ctx, req)
273255
if err != nil {
274256
return err
275257
}
@@ -298,11 +280,11 @@ func (s *Shell) outputToFile(table *model.Table) error {
298280

299281
// recordUserRequest record user request in DB.
300282
func (s *Shell) recordUserRequest(ctx context.Context, request string) error {
301-
histories, err := s.historyInteractor.List(ctx)
283+
histories, err := s.usecases.history.List(ctx)
302284
if err != nil {
303285
return err
304286
}
305-
if err := s.historyInteractor.Create(ctx, model.NewHistory(len(histories)+1, request)); err != nil {
287+
if err := s.usecases.history.Create(ctx, model.NewHistory(len(histories)+1, request)); err != nil {
306288
return fmt.Errorf("failed to store user input history: %w", err)
307289
}
308290
return nil

shell/shell_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ func newShell(t *testing.T, args []string) (*Shell, func(), error) {
790790
}
791791
historyRepository := persistence.NewHistoryRepository(historyDB)
792792
historyInteractor := interactor.NewHistoryInteractor(historyRepository)
793-
shellShell := NewShell(arg, configConfig, commandList, csvInteractor, tsvInteractor, ltsvInteractor, jsonInteractor, sqLite3Interactor, historyInteractor, excelInteractor)
793+
usecases := NewUsecases(csvInteractor, tsvInteractor, ltsvInteractor, jsonInteractor, sqLite3Interactor, historyInteractor, excelInteractor)
794+
shellShell := NewShell(arg, configConfig, commandList, usecases)
794795
return shellShell, func() {
795796
cleanup2()
796797
cleanup()

shell/tables.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// tablesCommand print all tables name in DB.
1515
func (c CommandList) tablesCommand(ctx context.Context, s *Shell, _ []string) error {
16-
tables, err := s.sqlite3Interactor.TablesName(ctx)
16+
tables, err := s.usecases.sqlite3.TablesName(ctx)
1717
if err != nil {
1818
return err
1919
}

shell/usecase.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package shell
2+
3+
import "github.com/nao1215/sqly/usecase"
4+
5+
// Usecases is a structure that holds the usecase layer.
6+
type Usecases struct {
7+
csv usecase.CSVUsecase
8+
tsv usecase.TSVUsecase
9+
ltsv usecase.LTSVUsecase
10+
json usecase.JSONUsecase
11+
sqlite3 usecase.DatabaseUsecase
12+
history usecase.HistoryUsecase
13+
excel usecase.ExcelUsecase
14+
}
15+
16+
// NewUsecases return *usecases that is assigned the result of parsing os.Args.
17+
func NewUsecases(
18+
csv usecase.CSVUsecase,
19+
tsv usecase.TSVUsecase,
20+
ltsv usecase.LTSVUsecase,
21+
json usecase.JSONUsecase,
22+
sqlite3 usecase.DatabaseUsecase,
23+
history usecase.HistoryUsecase,
24+
excel usecase.ExcelUsecase,
25+
) Usecases {
26+
return Usecases{
27+
csv: csv,
28+
tsv: tsv,
29+
ltsv: ltsv,
30+
json: json,
31+
sqlite3: sqlite3,
32+
history: history,
33+
excel: excel,
34+
}
35+
}

shell/wire.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ import "github.com/google/wire"
66
var Set = wire.NewSet(
77
NewShell,
88
NewCommands,
9+
NewUsecases,
910
)

0 commit comments

Comments
 (0)