From 7d0407013e87c7d4ac08a1e72dfdba44c27cc99f Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 24 Aug 2022 21:53:12 +0800 Subject: [PATCH] chore: make `golangci-lint` happy --- .github/workflows/tests.yml | 18 ++++++++++ .golangci.yaml | 62 +++++++++++++++++++++++++++++++++++ api/user_setting.go | 2 +- bin/server/cmd/root.go | 12 ++++--- bin/server/main.go | 12 +++++-- common/error.go | 2 +- server/profile/profile.go | 6 ++-- server/tag.go | 2 +- store/db/db.go | 22 +++---------- store/db/migration_history.go | 13 ++++++-- store/memo_organizer.go | 4 +++ store/store.go | 4 +-- store/user.go | 4 +++ store/user_setting.go | 2 +- 14 files changed, 130 insertions(+), 35 deletions(-) create mode 100644 .golangci.yaml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9d483e7c..e20c9e8d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -27,3 +27,21 @@ jobs: with: args: -v skip-cache: true + + go-tests: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-go@v3 + with: + go-version: 1.18 + check-latest: true + cache: true + - name: Run all tests + run: go test -v ./... | tee test.log; exit ${PIPESTATUS[0]} + - name: Pretty print tests running time + run: grep --color=never -e '--- PASS:' -e '--- FAIL:' test.log | sed 's/[:()]//g' | awk '{print $2,$3,$4}' | sort -t' ' -nk3 -r | awk '{sum += $3; print $1,$2,$3,sum"s"}' diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..a36d4502 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,62 @@ +linters: + enable: + - goimports + - revive + - govet + - staticcheck + - misspell + - gocritic + - sqlclosecheck + - rowserrcheck + - nilerr + - godot + +issues: + exclude: + - Rollback + +linters-settings: + revive: + enable-all-rules: true + rules: + - name: file-header + disabled: true + - name: line-length-limit + disabled: true + - name: function-length + disabled: true + - name: max-public-structs + disabled: true + - name: function-result-limit + disabled: true + - name: banned-characters + disabled: true + - name: argument-limit + disabled: true + - name: cognitive-complexity + disabled: true + - name: cyclomatic + disabled: true + - name: confusing-results + disabled: true + - name: add-constant + disabled: true + - name: flag-parameter + disabled: true + - name: nested-structs + disabled: true + - name: import-shadowing + disabled: true + - name: early-return + disabled: true + gocritic: + disabled-checks: + - ifElseChain + govet: + settings: + printf: + funcs: + - common.Errorf + forbidigo: + forbid: + - 'fmt\.Errorf(# Please use errors\.Wrap\|Wrapf\|Errorf instead)?' diff --git a/api/user_setting.go b/api/user_setting.go index d540a560..36727885 100644 --- a/api/user_setting.go +++ b/api/user_setting.go @@ -10,7 +10,7 @@ type UserSettingKey string const ( // UserSettingLocaleKey is the key type for user locale. UserSettingLocaleKey UserSettingKey = "locale" - // UserSettingMemoVisibilityKey is the key type for user perference memo default visibility. + // UserSettingMemoVisibilityKey is the key type for user preference memo default visibility. UserSettingMemoVisibilityKey UserSettingKey = "memoVisibility" ) diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go index da953912..0e1652cd 100644 --- a/bin/server/cmd/root.go +++ b/bin/server/cmd/root.go @@ -3,7 +3,6 @@ package cmd import ( "context" "fmt" - "os" "github.com/usememos/memos/server" "github.com/usememos/memos/server/profile" @@ -41,8 +40,11 @@ func Run(profile *profile.Profile) error { return s.Run() } -func Execute() { - profile := profile.GetProfile() +func Execute() error { + profile, err := profile.GetProfile() + if err != nil { + return err + } println("---") println("profile") @@ -54,6 +56,8 @@ func Execute() { if err := Run(profile); err != nil { fmt.Printf("error: %+v\n", err) - os.Exit(1) + return err } + + return nil } diff --git a/bin/server/main.go b/bin/server/main.go index 6820963e..670d2009 100644 --- a/bin/server/main.go +++ b/bin/server/main.go @@ -1,7 +1,15 @@ package main -import "github.com/usememos/memos/bin/server/cmd" +import ( + "os" + + _ "github.com/mattn/go-sqlite3" + + "github.com/usememos/memos/bin/server/cmd" +) func main() { - cmd.Execute() + if err := cmd.Execute(); err != nil { + os.Exit(1) + } } diff --git a/common/error.go b/common/error.go index b4d24e18..3545a9bb 100644 --- a/common/error.go +++ b/common/error.go @@ -9,7 +9,7 @@ type Code int // Application error codes. const ( - // 0 ~ 99 general error + // 0 ~ 99 general error. Ok Code = 0 Internal Code = 1 NotAuthorized Code = 2 diff --git a/server/profile/profile.go b/server/profile/profile.go index a2ac25f2..2bd8232a 100644 --- a/server/profile/profile.go +++ b/server/profile/profile.go @@ -45,7 +45,7 @@ func checkDSN(dataDir string) (string, error) { } // GetDevProfile will return a profile for dev or prod. -func GetProfile() *Profile { +func GetProfile() (*Profile, error) { profile := Profile{} flag.StringVar(&profile.Mode, "mode", "dev", "mode of server") flag.IntVar(&profile.Port, "port", 8080, "port of server") @@ -63,12 +63,12 @@ func GetProfile() *Profile { dataDir, err := checkDSN(profile.Data) if err != nil { fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err) - os.Exit(1) + return nil, err } profile.Data = dataDir profile.DSN = fmt.Sprintf("%s/memos_%s.db", dataDir, profile.Mode) profile.Version = common.GetCurrentVersion(profile.Mode) - return &profile + return &profile, nil } diff --git a/server/tag.go b/server/tag.go index e2b7227d..f6226da3 100644 --- a/server/tag.go +++ b/server/tag.go @@ -47,7 +47,7 @@ func (s *Server) registerTagRoutes(g *echo.Group) { tagMapSet := make(map[string]bool) - r, err := regexp.Compile("#(.+?) ") + r := regexp.MustCompile("#(.+?) ") if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compile regexp").SetInternal(err) } diff --git a/store/db/db.go b/store/db/db.go index d1e019a3..14d1dff3 100644 --- a/store/db/db.go +++ b/store/db/db.go @@ -14,8 +14,6 @@ import ( "github.com/usememos/memos/common" "github.com/usememos/memos/server/profile" - - _ "github.com/mattn/go-sqlite3" ) //go:embed migration @@ -175,11 +173,7 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st return err } - if err := tx.Commit(); err != nil { - return err - } - - return nil + return tx.Commit() } func (db *DB) seed(ctx context.Context) error { @@ -204,7 +198,7 @@ func (db *DB) seed(ctx context.Context) error { return nil } -// excecute runs a single SQL statement within a transaction. +// execute runs a single SQL statement within a transaction. func (db *DB) execute(ctx context.Context, stmt string) error { tx, err := db.Db.Begin() if err != nil { @@ -216,11 +210,7 @@ func (db *DB) execute(ctx context.Context, stmt string) error { return err } - if err := tx.Commit(); err != nil { - return err - } - - return nil + return tx.Commit() } // minorDirRegexp is a regular expression for minor version directory. @@ -264,9 +254,5 @@ func (db *DB) createMigrationHistoryTable(ctx context.Context) error { return err } - if err := tx.Commit(); err != nil { - return err - } - - return nil + return tx.Commit() } diff --git a/store/db/migration_history.go b/store/db/migration_history.go index da6eac8c..ef56da6e 100644 --- a/store/db/migration_history.go +++ b/store/db/migration_history.go @@ -33,9 +33,10 @@ func (db *DB) FindMigrationHistory(ctx context.Context, find *MigrationHistoryFi if len(list) == 0 { return nil, nil - } else { - return list[0], nil } + + migrationHistory := list[0] + return migrationHistory, nil } func (db *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) { @@ -92,6 +93,10 @@ func findMigrationHistoryList(ctx context.Context, tx *sql.Tx, find *MigrationHi migrationHistoryList = append(migrationHistoryList, &migrationHistory) } + if err := rows.Err(); err != nil { + return nil, err + } + return migrationHistoryList, nil } @@ -121,5 +126,9 @@ func upsertMigrationHistory(ctx context.Context, tx *sql.Tx, upsert *MigrationHi return nil, err } + if err := row.Err(); err != nil { + return nil, err + } + return &migrationHistory, nil } diff --git a/store/memo_organizer.go b/store/memo_organizer.go index 9aadb8a2..fd657447 100644 --- a/store/memo_organizer.go +++ b/store/memo_organizer.go @@ -95,6 +95,10 @@ func findMemoOrganizer(ctx context.Context, tx *sql.Tx, find *api.MemoOrganizerF return nil, FormatError(err) } + if err := row.Err(); err != nil { + return nil, err + } + return &memoOrganizerRaw, nil } diff --git a/store/store.go b/store/store.go index 82f2f884..736b295e 100644 --- a/store/store.go +++ b/store/store.go @@ -7,14 +7,14 @@ import ( "github.com/usememos/memos/server/profile" ) -// Store provides database access to all raw objects +// Store provides database access to all raw objects. type Store struct { db *sql.DB profile *profile.Profile cache api.CacheService } -// New creates a new instance of Store +// New creates a new instance of Store. func New(db *sql.DB, profile *profile.Profile) *Store { cacheService := NewCacheService() diff --git a/store/user.go b/store/user.go index b9b24cce..d2494d6b 100644 --- a/store/user.go +++ b/store/user.go @@ -258,6 +258,10 @@ func patchUser(ctx context.Context, tx *sql.Tx, patch *api.UserPatch) (*userRaw, return nil, FormatError(err) } + if err := row.Err(); err != nil { + return nil, err + } + return &userRaw, nil } diff --git a/store/user_setting.go b/store/user_setting.go index 39f69e6d..cb8faadc 100644 --- a/store/user_setting.go +++ b/store/user_setting.go @@ -111,7 +111,7 @@ func findUserSettingList(ctx context.Context, tx *sql.Tx, find *api.UserSettingF where, args := []string{"1 = 1"}, []interface{}{} if v := find.Key; v != nil { - where, args = append(where, "key = ?"), append(args, (*v).String()) + where, args = append(where, "key = ?"), append(args, v.String()) } where, args = append(where, "user_id = ?"), append(args, find.UserID)