Skip to content

Commit

Permalink
Merge 308fa7f into 2f3b30e
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] authored May 7, 2024
2 parents 2f3b30e + 308fa7f commit ba9dab7
Show file tree
Hide file tree
Showing 25 changed files with 69 additions and 78 deletions.
18 changes: 11 additions & 7 deletions config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ var (
Stdout = colorable.NewColorableStdout()
// Stderr is new instance of Writer which handles escape sequence for stderr.
Stderr = colorable.NewColorableStderr()
// query is SQL statement (for --sql option)
query *string
)

// Output is configuration for output data to file.
Expand Down Expand Up @@ -101,17 +99,23 @@ func NewArg(args []string) (*Arg, error) {
// newOutput retur *Output
func newOutput(filePath string, of outputFlag) *Output {
mode := model.PrintModeTable
if of.csv {
switch {
case of.excel:
mode = model.PrintModeCSV
} else if of.tsv {
case of.csv:
mode = model.PrintModeCSV
case of.tsv:
mode = model.PrintModeTSV
} else if of.ltsv {
case of.ltsv:
mode = model.PrintModeLTSV
} else if of.json {
case of.json:
mode = model.PrintModeJSON
} else if of.markdown {
case of.markdown:
mode = model.PrintModeMarkdownTable
default:
mode = model.PrintModeTable
}

return &Output{
FilePath: filePath,
Mode: mode,
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ func (c *Config) Dir() string {

// CreateDir make configuration directory.
func (c *Config) CreateDir() error {
return os.MkdirAll(c.Dir(), 0744)
return os.MkdirAll(c.Dir(), 0750)
}
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestConfig_CreateDir(t *testing.T) {
t.Fatal(err)
}

if runtime.GOOS != "windows" {
if runtime.GOOS != "windows" { //nolint
want := filepath.Join(homeDir, ".config", "sqly")
if !file.IsDir(want) {
t.Errorf("failed to create config directory at %s", want)
Expand Down
1 change: 1 addition & 0 deletions domain/errors.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package domain manage sqly domain logic.
package domain

import "errors"
Expand Down
4 changes: 2 additions & 2 deletions domain/model/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type History struct {

// ToTable convert History to Table.
func (h Histories) ToTable() *Table {
var records []Record
records := make([]Record, 0, len(h))
for _, v := range h {
records = append(records, Record{
strconv.Itoa(v.ID), v.Request,
Expand All @@ -33,7 +33,7 @@ func (h Histories) ToTable() *Table {

// ToStringList convert history to string list.
func (h Histories) ToStringList() []string {
var histories []string
histories := make([]string, 0, len(h))
for _, v := range h {
histories = append(histories, v.Request)
}
Expand Down
4 changes: 2 additions & 2 deletions domain/model/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type JSON struct {

// ToTable convert JSON to Table.
func (j *JSON) ToTable() *Table {
var keys []string
var keys []string //nolint
for _, json := range j.JSON {
for k := range json {
keys = append(keys, k)
Expand All @@ -25,7 +25,7 @@ func (j *JSON) ToTable() *Table {
header := sliceUnique(keys)
sort.Strings(header)

var records []Record
records := make([]Record, 0, len(j.JSON))
for _, json := range j.JSON {
r := Record{}
for _, h := range header {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/pflag v1.0.5
github.com/xuri/excelize/v2 v2.8.1
modernc.org/sqlite v1.29.8
modernc.org/sqlite v1.29.9
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc=
modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss=
modernc.org/sqlite v1.29.8 h1:nGKglNx9K5v0As+zF0/Gcl1kMkmaU1XynYyq92PbsC8=
modernc.org/sqlite v1.29.8/go.mod h1:lQPm27iqa4UNZpmr4Aor0MH0HkCLbt1huYDfWylLZFk=
modernc.org/sqlite v1.29.9 h1:9RhNMklxJs+1596GNuAX+O/6040bvOwacTxuFcRuQow=
modernc.org/sqlite v1.29.9/go.mod h1:ItX2a1OVGgNsFh6Dv60JQvGfJfTPHPVpV6DF59akYOA=
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
Expand Down
1 change: 1 addition & 0 deletions infrastructure/errors.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package infrastructure manage sqly infrastructure logic.
package infrastructure

import "errors"
Expand Down
11 changes: 6 additions & 5 deletions infrastructure/memory/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (r *sqlite3Repository) CreateTable(ctx context.Context, t *model.Table) err
if err != nil {
return err
}
defer tx.Rollback()
defer tx.Rollback() //nolint

_, err = tx.ExecContext(ctx, infra.GenerateCreateTableStatement((t)))
if err != nil {
Expand All @@ -46,13 +46,14 @@ func (r *sqlite3Repository) TablesName(ctx context.Context) ([]*model.Table, err
if err != nil {
return nil, err
}
defer tx.Rollback()
defer tx.Rollback() //nolint

rows, err := tx.QueryContext(ctx,
"SELECT name FROM sqlite_master WHERE type = 'table'")
if err != nil {
return nil, err
}
defer rows.Close() //nolint

tables := []*model.Table{}
var name string
Expand Down Expand Up @@ -84,7 +85,7 @@ func (r *sqlite3Repository) Insert(ctx context.Context, t *model.Table) error {
if err != nil {
return err
}
defer tx.Rollback()
defer tx.Rollback() //nolint

for _, v := range t.Records {
if _, err := tx.ExecContext(ctx, infra.GenerateInsertStatement(t.Name, v)); err != nil {
Expand Down Expand Up @@ -115,7 +116,7 @@ func (r *sqlite3Repository) Query(ctx context.Context, query string) (*model.Tab
if err != nil {
return nil, err
}
defer tx.Rollback()
defer tx.Rollback() //nolint

rows, err := tx.QueryContext(ctx, query)
if err != nil {
Expand Down Expand Up @@ -173,7 +174,7 @@ func (r *sqlite3Repository) Exec(ctx context.Context, statement string) (int64,
if err != nil {
return 0, err
}
defer tx.Rollback()
defer tx.Rollback() //nolint

result, err := tx.ExecContext(ctx, statement)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/persistence/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Test_csvRepository_List(t *testing.T) {

var tmpFile *os.File
var e error
if runtime.GOOS != "windows" {
if runtime.GOOS != "windows" { //nolint
tmpFile, e = os.CreateTemp(t.TempDir(), "dump.csv")
} else {
// See https://github.com/golang/go/issues/51442
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/persistence/excel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Test_excelRepository_Dump(t *testing.T) {
}

tempFilePath := filepath.Join(os.TempDir(), "dump.xlsx")
defer os.Remove(tempFilePath) // Clean up the temporary file after the test
defer os.Remove(tempFilePath) //nolint
if err := r.Dump(tempFilePath, excel); err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 4 additions & 3 deletions infrastructure/persistence/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (h *historyRepository) CreateTable(ctx context.Context) error {
if err != nil {
return err
}
defer tx.Rollback()
defer tx.Rollback() //nolint

q := "CREATE TABLE IF NOT EXISTS `history` (id INTEGER PRIMARY KEY AUTOINCREMENT, request TEXT)"
_, err = tx.ExecContext(ctx, q)
Expand All @@ -52,7 +52,7 @@ func (h *historyRepository) Create(ctx context.Context, t *model.Table) error {
if err != nil {
return err
}
defer tx.Rollback()
defer tx.Rollback() //nolint

for _, v := range t.Records {
if _, err := tx.ExecContext(ctx, infra.GenerateInsertStatement(t.Name, v)); err != nil {
Expand All @@ -68,13 +68,14 @@ func (h *historyRepository) List(ctx context.Context) (model.Histories, error) {
if err != nil {
return nil, err
}
defer tx.Rollback()
defer tx.Rollback() //nolint

rows, err := tx.QueryContext(ctx,
"SELECT `id`, `request` FROM `history` ORDER BY `id` ASC")
if err != nil {
return nil, err
}
defer rows.Close() //nolint

var id int
var request string
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/persistence/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_jsonRepository_List(t *testing.T) {

var tmpFile *os.File
var e error
if runtime.GOOS != "windows" {
if runtime.GOOS != "windows" { //nolint
tmpFile, e = os.CreateTemp(t.TempDir(), "dump.json")
} else {
// See https://github.com/golang/go/issues/51442
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/persistence/ltsv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func Test_ltsvRepository_List(t *testing.T) {

var tmpFile *os.File
var e error
if runtime.GOOS != "windows" {
if runtime.GOOS != "windows" { //nolint
tmpFile, e = os.CreateTemp(t.TempDir(), "dump.ltsv")
} else {
// See https://github.com/golang/go/issues/51442
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/persistence/tsv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Test_tsvRepository_List(t *testing.T) {

var tmpFile *os.File
var e error
if runtime.GOOS != "windows" {
if runtime.GOOS != "windows" { //nolint
tmpFile, e = os.CreateTemp(t.TempDir(), "dump.tsv")
} else {
// See https://github.com/golang/go/issues/51442
Expand Down
23 changes: 0 additions & 23 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,3 @@ func getStdoutForRunFunc(t *testing.T, f func([]string) int, list []string) []by
}
return buffer.Bytes()
}

func getStdout(t *testing.T, f func()) []byte {
t.Helper()
backupColorStdout := config.Stdout
defer func() {
config.Stdout = backupColorStdout
}()

r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
config.Stdout = w

f()
w.Close() //nolint

var buffer bytes.Buffer
if _, err := buffer.ReadFrom(r); err != nil {
t.Fatalf("failed to read buffer: %v", err)
}
return buffer.Bytes()
}
5 changes: 2 additions & 3 deletions shell/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c CommandList) dumpCommand(s *Shell, argv []string) error {
// 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 {
switch s.argument.Output.Mode { //nolint
case model.PrintModeCSV:
err = s.csvInteractor.Dump(filePath, table)
case model.PrintModeTSV:
Expand All @@ -55,8 +55,7 @@ func dumpToFile(s *Shell, filePath string, table *model.Table) error {

// dumpMode is dump mode.
func dumpMode(m model.PrintMode) string {
switch m {
case model.PrintModeTable:
if m == model.PrintModeTable {
return "csv"
}
return m.String()
Expand Down
12 changes: 8 additions & 4 deletions shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ func (s *Shell) communicate() error {
defer func() {
rawModeOff := exec.Command("/bin/stty", "-raw", "echo")
rawModeOff.Stdin = os.Stdin
_ = rawModeOff.Run()
rawModeOff.Wait()
if err := rawModeOff.Run(); err != nil {
fmt.Fprintf(Stderr, "failed to turn off raw mode: %v\n", err)
}
if err := rawModeOff.Wait(); err != nil {
fmt.Fprintf(Stderr, "failed to wait raw mode off: %v\n", err)
}
}()

for {
Expand All @@ -132,7 +136,7 @@ func (s *Shell) communicate() error {
// init store CSV data to in-memory DB and create table for sqly history.
func (s *Shell) init() error {
if err := s.historyInteractor.CreateTable(s.Ctx); err != nil {
return fmt.Errorf("failed to create table for sqly history: %v", err)
return fmt.Errorf("failed to create table for sqly history: %w", err)
}
if len(s.argument.FilePaths) == 0 {
return nil
Expand Down Expand Up @@ -302,7 +306,7 @@ func (s *Shell) recordUserRequest(ctx context.Context, request string) error {
}

if err := s.historyInteractor.Create(ctx, history); err != nil {
return fmt.Errorf("failed to store user input history: %v", err)
return fmt.Errorf("failed to store user input history: %w", err)
}
return nil
}
8 changes: 4 additions & 4 deletions shell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func TestShell_Run(t *testing.T) {
t.Run("print version", func(t *testing.T) {
config.Version = "(devel)"
config.Version = "(devel)" //nolint
defer func() {
config.Version = ""
}()
Expand All @@ -36,7 +36,7 @@ func TestShell_Run(t *testing.T) {
})

t.Run("print help", func(t *testing.T) {
config.Version = "(devel)"
config.Version = "(devel)" //nolint
defer func() {
config.Version = ""
}()
Expand All @@ -54,7 +54,7 @@ func TestShell_Run(t *testing.T) {
})

t.Run("SELECT * FROM actor ORDER BY actor ASC LIMIT 5", func(t *testing.T) {
config.Version = "(devel)"
config.Version = "(devel)" //nolint
defer func() {
config.Version = ""
}()
Expand All @@ -72,7 +72,7 @@ func TestShell_Run(t *testing.T) {
})

t.Run("execute sql and output result to file", func(t *testing.T) {
config.Version = "(devel)"
config.Version = "(devel)" //nolint
defer func() {
config.Version = ""
}()
Expand Down
5 changes: 3 additions & 2 deletions usecase/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package usecase

import (
"os"
"path/filepath"

"github.com/nao1215/sqly/domain/model"
"github.com/nao1215/sqly/domain/repository"
Expand All @@ -21,7 +22,7 @@ func NewCSVInteractor(r repository.CSVRepository) *CSVInteractor {
// The sqly command does not open many CSV files. Therefore, the file is
// opened and closed in the usecase layer without worrying about processing speed.
func (ci *CSVInteractor) List(csvFilePath string) (*model.CSV, error) {
f, err := os.Open(csvFilePath)
f, err := os.Open(filepath.Clean(csvFilePath))
if err != nil {
return nil, err
}
Expand All @@ -37,7 +38,7 @@ func (ci *CSVInteractor) List(csvFilePath string) (*model.CSV, error) {

// Dump write contents of DB table to CSV file
func (ci *CSVInteractor) Dump(csvFilePath string, table *model.Table) error {
f, err := os.OpenFile(csvFilePath, os.O_RDWR|os.O_CREATE, 0664)
f, err := os.OpenFile(filepath.Clean(csvFilePath), os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit ba9dab7

Please sign in to comment.