Skip to content

Commit

Permalink
chore: add more linters (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth authored Aug 6, 2024
1 parent 047a5fd commit 2ffe5d2
Show file tree
Hide file tree
Showing 21 changed files with 381 additions and 357 deletions.
21 changes: 21 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
linters:
enable:
- errcheck
- errname
- errorlint
- goconst
- gofumpt
- gosimple
- govet
- ineffassign
- nilerr
- prealloc
- predeclared
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- unconvert
- unused
- usestdlibvars
- wastedassign
3 changes: 3 additions & 0 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (

func getDB(dbpath string) (*sql.DB, error) {
db, err := sql.Open("sqlite", dbpath)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
return db, err
Expand Down
34 changes: 8 additions & 26 deletions cmd/db_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const (
)

var (
errDBDowngraded = errors.New(`Looks like you downgraded omm. You should either delete omm's
database file (you will lose data by doing that), or upgrade omm to
the latest version.`)
errDBDowngraded = errors.New("database downgraded")
errDBMigrationFailed = errors.New("database migration failed")
errCouldntFetchDBVersion = errors.New("couldn't fetch version")
)

type dbVersionInfo struct {
Expand Down Expand Up @@ -55,23 +55,17 @@ LIMIT 1;
}

func upgradeDBIfNeeded(db *sql.DB) error {
latestVersionInDB, versionErr := fetchLatestDBVersion(db)
if versionErr != nil {
return fmt.Errorf(`Couldn't get omm's latest database version. This is a fatal error; let %s
know about this via %s.
Error: %s`,
author,
repoIssuesUrl,
versionErr)
latestVersionInDB, err := fetchLatestDBVersion(db)
if err != nil {
return fmt.Errorf("%w: %s", errCouldntFetchDBVersion, err.Error())
}

if latestVersionInDB.version > latestDBVersion {
return errDBDowngraded
}

if latestVersionInDB.version < latestDBVersion {
err := upgradeDB(db, latestVersionInDB.version)
err = upgradeDB(db, latestVersionInDB.version)
if err != nil {
return err
}
Expand All @@ -86,19 +80,7 @@ func upgradeDB(db *sql.DB, currentVersion int) error {
migrateQuery := migrations[i]
migrateErr := runMigration(db, migrateQuery, i)
if migrateErr != nil {
return fmt.Errorf(`Something went wrong migrating omm's database to version %d. This is not
supposed to happen. You can try running omm by passing it a custom database
file path (using --dbpath; this will create a new database) to see if that fixes
things. If that works, you can either delete the previous database, or keep
using this new database (both are not ideal).
If you can, let %s know about this error via %s.
Sorry for breaking the upgrade step!
---
DB Error: %s
`, i, author, repoIssuesUrl, migrateErr)
return fmt.Errorf("%w (version %d): %v", errDBMigrationFailed, i, migrateErr.Error())
}
}
return nil
Expand Down
1 change: 0 additions & 1 deletion cmd/db_migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

func TestMigrationsAreSetupCorrectly(t *testing.T) {

migrations := getMigrations()
for i := 2; i <= latestDBVersion; i++ {
m, ok := migrations[i]
Expand Down
2 changes: 0 additions & 2 deletions cmd/guide.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func getContext(summary string) (string, error) {
}

func insertGuideTasks(db *sql.DB) error {

tasks := make([]types.Task, len(guideEntries))

now := time.Now()
Expand All @@ -78,7 +77,6 @@ func insertGuideTasks(db *sql.DB) error {
var err error
for i, e := range guideEntries {
ctxs[i], err = getContext(guideEntries[i].summary)

if err != nil {
continue
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ package cmd

import (
"database/sql"
"errors"
"fmt"
"time"

pers "github.com/dhth/omm/internal/persistence"
)

var (
errImportWillExceedTaskLimit = fmt.Errorf("Import will exceed maximum number of tasks allowed, which is %d. Archive/Delete tasks that are not active using ctrl+d/ctrl+x.", pers.TaskNumLimit)
)
var errWillExceedCapacity = errors.New("import will exceed capacity")

func importTask(db *sql.DB, taskSummary string) error {
numTasks, err := pers.FetchNumActiveTasksFromDB(db)
if err != nil {
return err
}
if numTasks+1 > pers.TaskNumLimit {
return errImportWillExceedTaskLimit
return fmt.Errorf("%w (current task count: %d)", errWillExceedCapacity, numTasks)
}

now := time.Now()
Expand All @@ -31,7 +30,7 @@ func importTasks(db *sql.DB, taskSummaries []string) error {
return err
}
if numTasks+len(taskSummaries) > pers.TaskNumLimit {
return errImportWillExceedTaskLimit
return fmt.Errorf("%w (current task count: %d)", errWillExceedCapacity, numTasks)
}

now := time.Now()
Expand Down
Loading

0 comments on commit 2ffe5d2

Please # to comment.