Skip to content

Commit

Permalink
fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kazhuravlev committed Dec 18, 2024
1 parent 6a14ec9 commit 148f2ab
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 41 deletions.
13 changes: 7 additions & 6 deletions cmd/gateway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ import (
"github.com/kazhuravlev/database-gateway/internal/storage"
"github.com/kazhuravlev/database-gateway/internal/storage/migrations"
"github.com/kazhuravlev/just"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)

func withConfig(action func(c *cli.Context, cfg config.Config) error) cli.ActionFunc {
func withConfig(action func(c *cli.Context, cfg config.Config) error) cli.ActionFunc { //nolint:gocritic
return func(c *cli.Context) error {
configFilename := c.String(keyConfig)

Expand All @@ -47,10 +46,10 @@ func withConfig(action func(c *cli.Context, cfg config.Config) error) cli.Action
}
}

func newMigrator(cfg config.PostgresConfig) (*migrator.Migrator, error) {
func newMigrator(cfg config.PostgresConfig) (*migrator.Migrator, error) { //nolint:gocritic
dbConn, err := pgdb.ConnectToPg(cfg)
if err != nil {
return nil, errors.Wrap(err, "connect to postgres")
return nil, fmt.Errorf("connect to postgres: %w", err)
}

migratorInst, err := migrator.New(migrator.NewOptions(
Expand All @@ -60,13 +59,15 @@ func newMigrator(cfg config.PostgresConfig) (*migrator.Migrator, error) {
dbConn,
))
if err != nil {
return nil, errors.Wrap(err, "create new migrator")
return nil, fmt.Errorf("create migrator: %w", err)
}

return migratorInst, nil
}

func withApp(cmd func(context.Context, *cli.Context, config.Config, *app.Service, *slog.Logger) error) func(*cli.Context, config.Config) error {
func withApp(
cmd func(context.Context, *cli.Context, config.Config, *app.Service, *slog.Logger) error,
) func(*cli.Context, config.Config) error {
return func(c *cli.Context, cfg config.Config) error {
ctx, cancel := context.WithCancel(c.Context)
defer cancel()
Expand Down
20 changes: 13 additions & 7 deletions cmd/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@ func main() {
}

if err := application.Run(os.Args); err != nil {
fmt.Println(err)
fmt.Println(err) //nolint:forbidigo
os.Exit(1)
}
}

func cmdRun(ctx context.Context, c *cli.Context, cfg config.Config, appInst *app.Service, logger *slog.Logger) error {
func cmdRun(
ctx context.Context,
_ *cli.Context,
cfg config.Config, //nolint:gocritic
appInst *app.Service,
logger *slog.Logger,
) error {
if err := cfg.Validate(); err != nil {
return fmt.Errorf("validate config: %w", err)
}
Expand All @@ -97,7 +103,7 @@ func cmdRun(ctx context.Context, c *cli.Context, cfg config.Config, appInst *app
return nil
}

func cmdGenerateModels(c *cli.Context, cfg config.Config) error {
func cmdGenerateModels(_ *cli.Context, cfg config.Config) error { //nolint:gocritic
// map[TABLE_NAME]map[FIELD_NAME]FIELD_TYPE
customFields := map[string]map[string]template.Type{
"query_results": {
Expand All @@ -107,7 +113,7 @@ func cmdGenerateModels(c *cli.Context, cfg config.Config) error {
},
}

postgresDSN := pgdb.BuildDbDsn(cfg.Storage)
postgresDSN := pgdb.BuildDBDsn(cfg.Storage)

dbTemplate := template.Default(postgres2.Dialect).
UseSchema(func(schema metadata.Schema) template.Schema {
Expand Down Expand Up @@ -137,7 +143,7 @@ func cmdGenerateModels(c *cli.Context, cfg config.Config) error {
return nil
}

func cmdMigrateUp(c *cli.Context, cfg config.Config) error {
func cmdMigrateUp(_ *cli.Context, cfg config.Config) error { //nolint:gocritic
migratorInst, err := newMigrator(cfg.Storage)
if err != nil {
return fmt.Errorf("create new migrator: %w", err)
Expand All @@ -150,7 +156,7 @@ func cmdMigrateUp(c *cli.Context, cfg config.Config) error {
return nil
}

func cmdMigrateDownOne(c *cli.Context, cfg config.Config) error {
func cmdMigrateDownOne(_ *cli.Context, cfg config.Config) error { //nolint:gocritic
migratorInst, err := newMigrator(cfg.Storage)
if err != nil {
return fmt.Errorf("create new migrator: %w", err)
Expand All @@ -163,7 +169,7 @@ func cmdMigrateDownOne(c *cli.Context, cfg config.Config) error {
return nil
}

func cmdMigrateCreateNew(c *cli.Context, cfg config.Config) error {
func cmdMigrateCreateNew(_ *cli.Context, cfg config.Config) error { //nolint:gocritic
migratorInst, err := newMigrator(cfg.Storage)
if err != nil {
return fmt.Errorf("create new migrator: %w", err)
Expand Down
7 changes: 6 additions & 1 deletion internal/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ func (s *Service) GetTargetByID(ctx context.Context, uID config.UserID, tID conf
return just.Pointer(adaptTarget(*res)), nil
}

func (s *Service) RunQuery(ctx context.Context, userID config.UserID, srvID config.TargetID, query string) (uuid6.UUID, *structs.QTable, error) {
func (s *Service) RunQuery(
ctx context.Context,
userID config.UserID,
srvID config.TargetID,
query string,
) (uuid6.UUID, *structs.QTable, error) {
srv, err := s.getTargetByID(ctx, userID, srvID)
if err != nil {
return uuid6.Nil(), nil, fmt.Errorf("get target by id: %w", err)
Expand Down
23 changes: 12 additions & 11 deletions internal/facade/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ func (s *Service) getServer(c echo.Context) error {
return fmt.Errorf("get target by id: %w", err)
}

formUrl := fmt.Sprintf("/servers/%s", srv.ID.S())
formURL := "/servers/%s" + srv.ID.S()

return Render(c, http.StatusOK, templates.PageTarget(user, *srv, formUrl, ``, nil, nil))
return Render(c, http.StatusOK, templates.PageTarget(user, *srv, formURL, ``, nil, nil))
}

func (s *Service) getAuth(c echo.Context) error {
Expand Down Expand Up @@ -278,18 +278,18 @@ func (s *Service) runQuery(c echo.Context) error {

query := params.Get("query")
format := params.Get("format")
formUrl := fmt.Sprintf("/servers/%s", srv.ID.S())
formURL := "/servers/%s" + srv.ID.S()

queryID, _, err := s.opts.app.RunQuery(c.Request().Context(), user.ID, srv.ID, query)
if err != nil {
return Render(c, http.StatusOK, templates.PageTarget(user, *srv, formUrl, query, nil, err)) //nolint:err113
return Render(c, http.StatusOK, templates.PageTarget(user, *srv, formURL, query, nil, err)) //nolint:err113
}

params2 := url.Values{}
params2.Set("format", format)
targetUrl := fmt.Sprintf("/servers/%s/%s?%s", srv.ID, queryID.S(), params2.Encode())
targetURL := fmt.Sprintf("/servers/%s/%s?%s", srv.ID, queryID.S(), params2.Encode())

return c.Redirect(http.StatusSeeOther, targetUrl)
return c.Redirect(http.StatusSeeOther, targetURL)
}

func (s *Service) getQueryResults(c echo.Context) error {
Expand All @@ -313,14 +313,15 @@ func (s *Service) getQueryResults(c echo.Context) error {
}

format := params.Get("format")
formUrl := fmt.Sprintf("/servers/%s", srv.ID.S())
formURL := "/servers/%s" + srv.ID.S()

switch format {
default:
correctedUrl := fmt.Sprintf(c.Request().URL.Path + "?format=html")
return c.Redirect(http.StatusSeeOther, correctedUrl)
correctedURL := c.Request().URL.Path + "?format=html"

return c.Redirect(http.StatusSeeOther, correctedURL)
case "html":
return Render(c, http.StatusOK, templates.PageTarget(user, *srv, formUrl, qRes.Query, &qRes.QTable, nil))
return Render(c, http.StatusOK, templates.PageTarget(user, *srv, formURL, qRes.Query, &qRes.QTable, nil))
case "json":
qTbl2 := just.SliceMap(qRes.QTable.Rows, func(row []string) map[string]any {
m := make(map[string]any, len(qRes.QTable.Headers))
Expand All @@ -333,7 +334,7 @@ func (s *Service) getQueryResults(c echo.Context) error {

resBuf, err := json.Marshal(qTbl2)
if err != nil {
return Render(c, http.StatusOK, templates.PageTarget(user, *srv, formUrl, qRes.Query, nil, err))
return Render(c, http.StatusOK, templates.PageTarget(user, *srv, formURL, qRes.Query, nil, err))
}

c.Response().Header().Set(echo.HeaderContentDisposition, fmt.Sprintf(`%s; filename="%s"`, "attachment", "response.json")) //nolint
Expand Down
15 changes: 8 additions & 7 deletions internal/migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package migrator

import (
"fmt"

_ "github.com/lib/pq"
"github.com/pkg/errors"
"github.com/pressly/goose/v3"
)

Expand All @@ -30,15 +31,15 @@ type Migrator struct {

func New(opts Options) (*Migrator, error) {
if err := opts.Validate(); err != nil {
return nil, errors.Wrap(err, "validate options")
return nil, fmt.Errorf("invalid options: %w", err)
}

goose.SetBaseFS(opts.migrationsFs)
goose.SetTableName(opts.migrationsTableName)
goose.SetSequential(true)

if err := goose.SetDialect(dialect); err != nil {
return nil, errors.Wrap(err, "set dialect")
return nil, fmt.Errorf("failed to set goose dialect: %w", err)
}

return &Migrator{
Expand All @@ -48,31 +49,31 @@ func New(opts Options) (*Migrator, error) {

func (m *Migrator) CreateNewMigration(name, typ string) error {
if err := goose.Create(m.opts.db, m.opts.migrationsDir, name, typ); err != nil {
return errors.Wrap(err, "create new migration")
return fmt.Errorf("failed to create migration: %w", err)
}

return nil
}

func (m *Migrator) Up() error {
if err := goose.Up(m.opts.db, "."); err != nil {
return errors.Wrap(err, "up migrations")
return fmt.Errorf("migrate up: %w", err)
}

return nil
}

func (m *Migrator) DownOne() error {
if err := goose.Down(m.opts.db, "."); err != nil {
return errors.Wrap(err, "down one migration")
return fmt.Errorf("migrate down: %w", err)
}

return nil
}

func (m *Migrator) DownAll() error {
if err := goose.DownTo(m.opts.db, ".", 0); err != nil {
return errors.Wrap(err, "down one migration")
return fmt.Errorf("migrate down one: %w", err)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions internal/pgdb/pg_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/kazhuravlev/just"
)

func BuildDbDsn(cfg config.PostgresConfig) string {
func BuildDBDsn(cfg config.PostgresConfig) string { //nolint:gocritic
return fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
cfg.Host,
Expand All @@ -37,15 +37,15 @@ func BuildDbDsn(cfg config.PostgresConfig) string {
)
}

func ConnectToPg(cfg config.PostgresConfig) (*sql.DB, error) {
postgresDSN := BuildDbDsn(cfg)
func ConnectToPg(cfg config.PostgresConfig) (*sql.DB, error) { //nolint:gocritic
postgresDSN := BuildDBDsn(cfg)
dbConn, err := sql.Open("postgres", postgresDSN)
if err != nil {
return nil, fmt.Errorf("connect to postgres: %w", err)
}

dbConn.SetMaxIdleConns(2) //nolint:gomnd // it is obvious
dbConn.SetConnMaxLifetime(5 * time.Minute) //nolint:gomnd // it is obvious
dbConn.SetMaxIdleConns(2) //nolint:mnd
dbConn.SetConnMaxLifetime(5 * time.Minute) //nolint:mnd
dbConn.SetMaxOpenConns(cfg.MaxPoolSize)

if err := dbConn.Ping(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/storage/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type InsertQueryResultsReq struct {
Response json.RawMessage
}

func (s *Service) InsertQueryResults(conn qrm.DB, req InsertQueryResultsReq) error {
func (*Service) InsertQueryResults(conn qrm.DB, req InsertQueryResultsReq) error { //nolint:gocritic
obj := model.QueryResults{
ID: req.ID,
UserID: req.UserID,
Expand All @@ -55,7 +55,7 @@ func (s *Service) InsertQueryResults(conn qrm.DB, req InsertQueryResultsReq) err
return nil
}

func (s *Service) GetQueryResults(conn qrm.DB, uid config.UserID, id uuid6.UUID) (*model.QueryResults, error) {
func (*Service) GetQueryResults(conn qrm.DB, uid config.UserID, id uuid6.UUID) (*model.QueryResults, error) {
var obj model.QueryResults
err := tbl.QueryResults.
SELECT(tbl.QueryResults.AllColumns).
Expand Down
1 change: 0 additions & 1 deletion internal/storage/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
)

var (
ErrBadRequest = errors.New("bad request")
ErrNotFound = errors.New("not found")
ErrIntegrityViolation = errors.New("integrity violation")
)
Expand Down
2 changes: 1 addition & 1 deletion internal/uuid6/uuid_v6.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

var nilUUID = ulid.ULID{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} //nolint:gochecknoglobals

type UUID ulid.ULID
type UUID ulid.ULID //nolint:recvcheck

func New() UUID {
return UUID(ulid.Make())
Expand Down

0 comments on commit 148f2ab

Please sign in to comment.