diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dc10aae..9289187 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,7 +19,7 @@ jobs: with: go-version-file: "go.mod" - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v5 + uses: goreleaser/goreleaser-action@v6 with: version: latest args: release --clean diff --git a/.golangci.yml b/.golangci.yml index d6e784a..e306464 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,7 +7,6 @@ issues: linters: disable-all: true enable: - - errcheck - gosimple - govet - ineffassign diff --git a/config/argument.go b/config/argument.go index 1674452..181422c 100644 --- a/config/argument.go +++ b/config/argument.go @@ -98,28 +98,26 @@ func NewArg(args []string) (*Arg, error) { // newOutput retur *Output func newOutput(filePath string, of outputFlag) *Output { - mode := model.PrintModeTable + output := &Output{ + FilePath: filePath, + } switch { case of.excel: - mode = model.PrintModeCSV + output.Mode = model.PrintModeExcel case of.csv: - mode = model.PrintModeCSV + output.Mode = model.PrintModeCSV case of.tsv: - mode = model.PrintModeTSV + output.Mode = model.PrintModeTSV case of.ltsv: - mode = model.PrintModeLTSV + output.Mode = model.PrintModeLTSV case of.json: - mode = model.PrintModeJSON + output.Mode = model.PrintModeJSON case of.markdown: - mode = model.PrintModeMarkdownTable + output.Mode = model.PrintModeMarkdownTable default: - mode = model.PrintModeTable - } - - return &Output{ - FilePath: filePath, - Mode: mode, + output.Mode = model.PrintModeTable } + return output } // NeedsOutputToFile whether the data needs to be output to the file @@ -155,7 +153,7 @@ func usage(flag pflag.FlagSet) string { } func version() { - fmt.Fprintf(Stdout, "sqly %s\n", GetVersion()) + fmt.Fprintf(Stdout, "sqly %s\n", GetVersion()) //nolint:errcheck // ignore error } // GetVersion return sqly command version. diff --git a/domain/model/table.go b/domain/model/table.go index cfc897d..1a7cde6 100644 --- a/domain/model/table.go +++ b/domain/model/table.go @@ -164,17 +164,17 @@ func (t *Table) printMarkdownTable(out io.Writer) { // printCSV print all record with header; output format is csv func (t *Table) printCSV(out io.Writer) { - fmt.Fprintln(out, strings.Join(t.Header, ",")) + fmt.Fprintln(out, strings.Join(t.Header, ",")) //nolint:errcheck // ignore error for _, v := range t.Records { - fmt.Fprintln(out, strings.Join(v, ",")) + fmt.Fprintln(out, strings.Join(v, ",")) //nolint:errcheck // ignore error } } // printTSV print all record with header; output format is tsv func (t *Table) printTSV(out io.Writer) { - fmt.Fprintln(out, strings.Join(t.Header, "\t")) + fmt.Fprintln(out, strings.Join(t.Header, "\t")) //nolint:errcheck // ignore error for _, v := range t.Records { - fmt.Fprintln(out, strings.Join(v, "\t")) + fmt.Fprintln(out, strings.Join(v, "\t")) //nolint:errcheck // ignore error } } @@ -185,7 +185,7 @@ func (t *Table) printLTSV(out io.Writer) { for i, data := range v { r = append(r, t.Header[i]+":"+data) } - fmt.Fprintln(out, strings.Join(r, "\t")) + fmt.Fprintln(out, strings.Join(r, "\t")) //nolint:errcheck // ignore error } } @@ -202,10 +202,10 @@ func (t *Table) printJSON(out io.Writer) { } b, err := json.MarshalIndent(data, "", " ") if err != nil { - fmt.Fprintf(os.Stderr, "json marshal error: "+err.Error()) + fmt.Fprintf(os.Stderr, "json marshal error: "+err.Error()) //nolint:errcheck // ignore error return } - fmt.Fprintln(out, string(b)) + fmt.Fprintln(out, string(b)) //nolint:errcheck // ignore error } // printExcel print all record in excel format. diff --git a/infrastructure/persistence/json.go b/infrastructure/persistence/json.go index 3acb4a5..c5325a3 100644 --- a/infrastructure/persistence/json.go +++ b/infrastructure/persistence/json.go @@ -17,7 +17,7 @@ func NewJSONRepository() repository.JSONRepository { } func (r *jsonRepository) List(jsonFilePath string) (*model.JSON, error) { - bytes, err := os.ReadFile(jsonFilePath) + bytes, err := os.ReadFile(filepath.Clean(jsonFilePath)) if err != nil { return nil, err } diff --git a/main.go b/main.go index 5b3d194..f432c30 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,11 @@ func main() { func run(args []string) int { shell, cleanup, err := di.NewShell(args) if err != nil { - fmt.Fprintf(os.Stderr, "%s: %v\n", "failed to initialize sqly shell", err) + fmt.Fprintf( + os.Stderr, + "%s: %v\n", + "failed to initialize sqly shell", + err) return 1 } defer cleanup() diff --git a/shell/command.go b/shell/command.go index fcb9cf9..e7e68b8 100644 --- a/shell/command.go +++ b/shell/command.go @@ -49,7 +49,7 @@ func (c CommandList) hasCmdPrefix(s string) bool { // sortCommandNameKey returns an array of sorted keys (command names) // to sort the command list map func (c CommandList) sortCommandNameKey() []string { - var keys []string + keys := make([]string, 0, len(c)) for key := range c { keys = append(keys, key) } diff --git a/shell/dump.go b/shell/dump.go index acca658..5112229 100644 --- a/shell/dump.go +++ b/shell/dump.go @@ -11,11 +11,11 @@ import ( // 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.") + fmt.Fprintln(config.Stdout, "[Usage]") //nolint:errcheck // ignore error + fmt.Fprintln(config.Stdout, " .dump TABLE_NAME FILE_PATH") //nolint:errcheck // ignore error + fmt.Fprintln(config.Stdout, "[Note]") //nolint:errcheck // ignore error + fmt.Fprintln(config.Stdout, " Output will be in the format specified in .mode.") //nolint:errcheck // ignore error + fmt.Fprintln(config.Stdout, " table mode is not available in .dump. If mode is table, .dump output CSV file.") //nolint:errcheck // ignore error return nil } @@ -27,7 +27,7 @@ func (c CommandList) dumpCommand(s *Shell, argv []string) error { if err := dumpToFile(s, argv[1], table); err != nil { return err } - fmt.Fprintf(config.Stdout, "dump `%s` table to %s (mode=%s)\n", + fmt.Fprintf(config.Stdout, "dump `%s` table to %s (mode=%s)\n", //nolint:errcheck // ignore error color.CyanString(argv[0]), color.HiCyanString(argv[1]), dumpMode(s.argument.Output.Mode)) return nil diff --git a/shell/help.go b/shell/help.go index ce0d58c..524b3b8 100644 --- a/shell/help.go +++ b/shell/help.go @@ -11,7 +11,7 @@ import ( func (c CommandList) helpCommand(s *Shell, argv []string) error { for _, cmdName := range c.sortCommandNameKey() { fmt.Fprintf(config.Stdout, "%20s: %s\n", - color.CyanString(cmdName), c[cmdName].description) + color.CyanString(cmdName), c[cmdName].description) //nolint:errcheck // ignore error } return nil } diff --git a/shell/mode.go b/shell/mode.go index 2cd43e5..dadffb9 100644 --- a/shell/mode.go +++ b/shell/mode.go @@ -11,7 +11,7 @@ import ( func (c CommandList) modeCommand(s *Shell, argv []string) error { if len(argv) == 0 { fmt.Fprintln(config.Stdout, "[Usage]") - fmt.Fprintf(config.Stdout, " .mode OUTPUT_MODE ※ current mode=%s\n", s.argument.Output.Mode.String()) + fmt.Fprintf(config.Stdout, " .mode OUTPUT_MODE ※ current mode=%s\n", s.argument.Output.Mode.String()) //nolint:errcheck // ignore error fmt.Fprintln(config.Stdout, "[Output mode list]") fmt.Fprintln(config.Stdout, " table") fmt.Fprintln(config.Stdout, " markdown") diff --git a/shell/shell_test.go b/shell/shell_test.go index c8fd49e..6eaf1c9 100644 --- a/shell/shell_test.go +++ b/shell/shell_test.go @@ -89,7 +89,7 @@ func TestShell_Run(t *testing.T) { } // TODO: - got, err := os.ReadFile(file) + got, err := os.ReadFile(filepath.Clean(file)) if err != nil { t.Fatal(err) } @@ -542,7 +542,7 @@ func TestShell_exec(t *testing.T) { t.Fatal(err) } - got, err := os.ReadFile(file) + got, err := os.ReadFile(filepath.Clean(file)) if err != nil { t.Fatal(err) } @@ -569,7 +569,7 @@ func TestShell_exec(t *testing.T) { t.Fatal(err) } - got, err := os.ReadFile(file) + got, err := os.ReadFile(filepath.Clean(file)) if err != nil { t.Fatal(err) } @@ -596,7 +596,7 @@ func TestShell_exec(t *testing.T) { t.Fatal(err) } - got, err := os.ReadFile(file) + got, err := os.ReadFile(filepath.Clean(file)) if err != nil { t.Fatal(err) } @@ -623,7 +623,7 @@ func TestShell_exec(t *testing.T) { t.Fatal(err) } - got, err := os.ReadFile(file) + got, err := os.ReadFile(filepath.Clean(file)) if err != nil { t.Fatal(err) } @@ -650,7 +650,7 @@ func TestShell_exec(t *testing.T) { t.Fatal(err) } - got, err := os.ReadFile(file) + got, err := os.ReadFile(filepath.Clean(file)) if err != nil { t.Fatal(err) } diff --git a/usecase/ltsv.go b/usecase/ltsv.go index 9ca688c..8cffc7b 100644 --- a/usecase/ltsv.go +++ b/usecase/ltsv.go @@ -2,6 +2,7 @@ package usecase import ( "os" + "path/filepath" "github.com/nao1215/sqly/domain/model" "github.com/nao1215/sqly/domain/repository" @@ -19,7 +20,7 @@ func NewLTSVInteractor(r repository.LTSVRepository) *LTSVInteractor { // List get LTSV data. func (li *LTSVInteractor) List(ltsvFilePath string) (*model.LTSV, error) { - f, err := os.Open(ltsvFilePath) + f, err := os.Open(filepath.Clean(ltsvFilePath)) if err != nil { return nil, err } @@ -34,7 +35,7 @@ func (li *LTSVInteractor) List(ltsvFilePath string) (*model.LTSV, error) { // Dump write contents of DB table to LTSV file func (li *LTSVInteractor) Dump(ltsvFilePath string, table *model.Table) error { - f, err := os.OpenFile(ltsvFilePath, os.O_RDWR|os.O_CREATE, 0664) + f, err := os.OpenFile(filepath.Clean(ltsvFilePath), os.O_RDWR|os.O_CREATE, 0600) if err != nil { return err } diff --git a/usecase/sqlite3.go b/usecase/sqlite3.go index 5c61b23..7e678f3 100644 --- a/usecase/sqlite3.go +++ b/usecase/sqlite3.go @@ -85,7 +85,7 @@ func (si *SQLite3Interactor) ExecSQL(ctx context.Context, statement string) (*mo case si.sql.isInsert(argv[0]) || si.sql.isUpdate(argv[0]) || si.sql.isDelete(argv[0]): affectedRows, err := si.Repository.Exec(ctx, statement) if err != nil { - return nil, 0, fmt.Errorf("execute statement error: %v: %s", err, color.CyanString(statement)) + return nil, 0, fmt.Errorf("execute statement error: %w: %s", err, color.CyanString(statement)) } return nil, affectedRows, nil default: diff --git a/usecase/tsv.go b/usecase/tsv.go index 9f56b4d..c645723 100644 --- a/usecase/tsv.go +++ b/usecase/tsv.go @@ -37,7 +37,7 @@ func (ti *TSVInteractor) List(tsvFilePath string) (*model.TSV, error) { // Dump write contents of DB table to TSV file func (ti *TSVInteractor) Dump(tsvFilePath string, table *model.Table) error { - f, err := os.OpenFile(tsvFilePath, os.O_RDWR|os.O_CREATE, 0664) + f, err := os.OpenFile(filepath.Clean(tsvFilePath), os.O_RDWR|os.O_CREATE, 0600) if err != nil { return err }