diff --git a/.golangci.yaml b/.golangci.yaml index c905a23..a687e02 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,5 +1,6 @@ linters: enable: + - errcheck - goimports - revive - govet @@ -10,17 +11,30 @@ linters: - rowserrcheck - nilerr - godot + - forbidigo + - mirror + - bodyclose issues: + include: + # https://golangci-lint.run/usage/configuration/#command-line-options exclude: - Rollback + - logger.Sync + - pgInstance.Stop - fmt.Printf - - fmt.Print + - Enter(.*)_(.*) + - Exit(.*)_(.*) linters-settings: + goimports: + # Put imports beginning with prefix after 3rd-party packages. + local-prefixes: github.com/boojack/slash revive: + # Default to run all linters so that new rules in the future could automatically be added to the static check. enable-all-rules: true rules: + # The following rules are too strict and make coding harder. We do not enable them for now. - name: file-header disabled: true - name: line-length-limit @@ -59,9 +73,14 @@ linters-settings: - ifElseChain govet: settings: - printf: - funcs: + printf: # The name of the analyzer, run `go tool vet help` to see the list of all analyzers + funcs: # Run `go tool vet help printf` to see the full configuration of `printf`. - common.Errorf + enable-all: true + disable: + - fieldalignment + - shadow forbidigo: forbid: - 'fmt\.Errorf(# Please use errors\.Wrap\|Wrapf\|Errorf instead)?' + - 'ioutil\.ReadDir(# Please use os\.ReadDir)?' diff --git a/api/v1/analytics.go b/api/v1/analytics.go index 24cdc28..a31428b 100644 --- a/api/v1/analytics.go +++ b/api/v1/analytics.go @@ -6,10 +6,11 @@ import ( "net/http" "strconv" - "github.com/boojack/slash/store" "github.com/labstack/echo/v4" "github.com/mssola/useragent" "golang.org/x/exp/slices" + + "github.com/boojack/slash/store" ) type ReferenceInfo struct { diff --git a/api/v1/auth.go b/api/v1/auth.go index 69f3f56..9aca390 100644 --- a/api/v1/auth.go +++ b/api/v1/auth.go @@ -7,12 +7,13 @@ import ( "net/http" "time" - "github.com/boojack/slash/api/auth" - storepb "github.com/boojack/slash/proto/gen/store" - "github.com/boojack/slash/store" "github.com/labstack/echo/v4" "github.com/pkg/errors" "golang.org/x/crypto/bcrypt" + + "github.com/boojack/slash/api/auth" + storepb "github.com/boojack/slash/proto/gen/store" + "github.com/boojack/slash/store" ) type SignInRequest struct { diff --git a/api/v1/jwt.go b/api/v1/jwt.go index da2a643..4fee265 100644 --- a/api/v1/jwt.go +++ b/api/v1/jwt.go @@ -5,13 +5,14 @@ import ( "net/http" "strings" + "github.com/golang-jwt/jwt/v4" + "github.com/labstack/echo/v4" + "github.com/pkg/errors" + "github.com/boojack/slash/api/auth" "github.com/boojack/slash/internal/util" storepb "github.com/boojack/slash/proto/gen/store" "github.com/boojack/slash/store" - "github.com/golang-jwt/jwt/v4" - "github.com/labstack/echo/v4" - "github.com/pkg/errors" ) const ( diff --git a/api/v1/redirector.go b/api/v1/redirector.go index 829ab3c..f9b568f 100644 --- a/api/v1/redirector.go +++ b/api/v1/redirector.go @@ -8,10 +8,11 @@ import ( "net/url" "strings" - storepb "github.com/boojack/slash/proto/gen/store" - "github.com/boojack/slash/store" "github.com/labstack/echo/v4" "github.com/pkg/errors" + + storepb "github.com/boojack/slash/proto/gen/store" + "github.com/boojack/slash/store" ) func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) { diff --git a/api/v1/shortcut.go b/api/v1/shortcut.go index 564f27d..9ed1bb2 100644 --- a/api/v1/shortcut.go +++ b/api/v1/shortcut.go @@ -7,11 +7,12 @@ import ( "net/http" "strings" + "github.com/labstack/echo/v4" + "github.com/pkg/errors" + "github.com/boojack/slash/internal/util" storepb "github.com/boojack/slash/proto/gen/store" "github.com/boojack/slash/store" - "github.com/labstack/echo/v4" - "github.com/pkg/errors" ) // Visibility is the type of a shortcut visibility. diff --git a/api/v1/user.go b/api/v1/user.go index 4d6101f..81776d2 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -6,10 +6,12 @@ import ( "net/http" "net/mail" + "github.com/labstack/echo/v4" + "github.com/pkg/errors" + "golang.org/x/crypto/bcrypt" + "github.com/boojack/slash/internal/util" "github.com/boojack/slash/store" - "github.com/labstack/echo/v4" - "golang.org/x/crypto/bcrypt" ) const ( @@ -60,13 +62,13 @@ type CreateUserRequest struct { func (create CreateUserRequest) Validate() error { if create.Email != "" && !validateEmail(create.Email) { - return fmt.Errorf("invalid email format") + return errors.New("invalid email format") } if create.Nickname != "" && len(create.Nickname) < 3 { - return fmt.Errorf("nickname is too short, minimum length is 3") + return errors.New("nickname is too short, minimum length is 3") } if len(create.Password) < 3 { - return fmt.Errorf("password is too short, minimum length is 3") + return errors.New("password is too short, minimum length is 3") } return nil diff --git a/api/v1/user_setting.go b/api/v1/user_setting.go index 6dfa1fc..0b42cda 100644 --- a/api/v1/user_setting.go +++ b/api/v1/user_setting.go @@ -2,7 +2,8 @@ package v1 import ( "encoding/json" - "fmt" + + "github.com/pkg/errors" ) type UserSettingKey string @@ -39,7 +40,7 @@ func (upsert UserSettingUpsert) Validate() error { localeValue := "en" err := json.Unmarshal([]byte(upsert.Value), &localeValue) if err != nil { - return fmt.Errorf("failed to unmarshal user setting locale value") + return errors.New("failed to unmarshal user setting locale value") } invalid := true @@ -50,10 +51,10 @@ func (upsert UserSettingUpsert) Validate() error { } } if invalid { - return fmt.Errorf("invalid user setting locale value") + return errors.New("invalid user setting locale value") } } else { - return fmt.Errorf("invalid user setting key") + return errors.New("invalid user setting key") } return nil diff --git a/api/v1/v1.go b/api/v1/v1.go index e4bac4f..1b5d892 100644 --- a/api/v1/v1.go +++ b/api/v1/v1.go @@ -1,10 +1,11 @@ package v1 import ( + "github.com/labstack/echo/v4" + "github.com/boojack/slash/server/profile" "github.com/boojack/slash/server/service/license" "github.com/boojack/slash/store" - "github.com/labstack/echo/v4" ) type APIV1Service struct { diff --git a/api/v1/workspace.go b/api/v1/workspace.go index 053958c..ef218b5 100644 --- a/api/v1/workspace.go +++ b/api/v1/workspace.go @@ -4,10 +4,11 @@ import ( "fmt" "net/http" + "github.com/labstack/echo/v4" + storepb "github.com/boojack/slash/proto/gen/store" "github.com/boojack/slash/server/profile" "github.com/boojack/slash/store" - "github.com/labstack/echo/v4" ) type WorkspaceProfile struct { diff --git a/api/v2/acl.go b/api/v2/acl.go index 4eaee30..9c29474 100644 --- a/api/v2/acl.go +++ b/api/v2/acl.go @@ -5,16 +5,17 @@ import ( "net/http" "strings" - "github.com/boojack/slash/api/auth" - "github.com/boojack/slash/internal/util" - storepb "github.com/boojack/slash/proto/gen/store" - "github.com/boojack/slash/store" "github.com/golang-jwt/jwt/v4" "github.com/pkg/errors" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" + + "github.com/boojack/slash/api/auth" + "github.com/boojack/slash/internal/util" + storepb "github.com/boojack/slash/proto/gen/store" + "github.com/boojack/slash/store" ) // ContextKey is the key type of context value. diff --git a/api/v2/shortcut_service.go b/api/v2/shortcut_service.go index 39481f3..63ccdd0 100644 --- a/api/v2/shortcut_service.go +++ b/api/v2/shortcut_service.go @@ -3,13 +3,14 @@ package v2 import ( "context" - apiv2pb "github.com/boojack/slash/proto/gen/api/v2" - storepb "github.com/boojack/slash/proto/gen/store" - "github.com/boojack/slash/store" "github.com/pkg/errors" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/encoding/protojson" + + apiv2pb "github.com/boojack/slash/proto/gen/api/v2" + storepb "github.com/boojack/slash/proto/gen/store" + "github.com/boojack/slash/store" ) type ShortcutService struct { diff --git a/api/v2/subscription_service.go b/api/v2/subscription_service.go index 6b87e7c..1e0597a 100644 --- a/api/v2/subscription_service.go +++ b/api/v2/subscription_service.go @@ -3,12 +3,13 @@ package v2 import ( "context" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + apiv2pb "github.com/boojack/slash/proto/gen/api/v2" "github.com/boojack/slash/server/profile" "github.com/boojack/slash/server/service/license" "github.com/boojack/slash/store" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) type SubscriptionService struct { diff --git a/api/v2/user_service.go b/api/v2/user_service.go index f7f9b14..49bb2d8 100644 --- a/api/v2/user_service.go +++ b/api/v2/user_service.go @@ -4,10 +4,6 @@ import ( "context" "time" - "github.com/boojack/slash/api/auth" - apiv2pb "github.com/boojack/slash/proto/gen/api/v2" - storepb "github.com/boojack/slash/proto/gen/store" - "github.com/boojack/slash/store" "github.com/golang-jwt/jwt/v4" "github.com/pkg/errors" "golang.org/x/crypto/bcrypt" @@ -15,6 +11,11 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/timestamppb" + + "github.com/boojack/slash/api/auth" + apiv2pb "github.com/boojack/slash/proto/gen/api/v2" + storepb "github.com/boojack/slash/proto/gen/store" + "github.com/boojack/slash/store" ) type UserService struct { diff --git a/api/v2/user_setting_service.go b/api/v2/user_setting_service.go index 0a7dad9..1a320ed 100644 --- a/api/v2/user_setting_service.go +++ b/api/v2/user_setting_service.go @@ -3,12 +3,13 @@ package v2 import ( "context" - apiv2pb "github.com/boojack/slash/proto/gen/api/v2" - storepb "github.com/boojack/slash/proto/gen/store" - "github.com/boojack/slash/store" "github.com/pkg/errors" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + + apiv2pb "github.com/boojack/slash/proto/gen/api/v2" + storepb "github.com/boojack/slash/proto/gen/store" + "github.com/boojack/slash/store" ) type UserSettingService struct { diff --git a/api/v2/v2.go b/api/v2/v2.go index 5c8ae77..fb39c90 100644 --- a/api/v2/v2.go +++ b/api/v2/v2.go @@ -4,16 +4,17 @@ import ( "context" "fmt" - apiv2pb "github.com/boojack/slash/proto/gen/api/v2" - "github.com/boojack/slash/server/profile" - "github.com/boojack/slash/server/service/license" - "github.com/boojack/slash/store" grpcRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/improbable-eng/grpc-web/go/grpcweb" "github.com/labstack/echo/v4" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/reflection" + + apiv2pb "github.com/boojack/slash/proto/gen/api/v2" + "github.com/boojack/slash/server/profile" + "github.com/boojack/slash/server/service/license" + "github.com/boojack/slash/store" ) type APIV2Service struct { diff --git a/api/v2/workspace_service.go b/api/v2/workspace_service.go index 4e60e41..9193ea9 100644 --- a/api/v2/workspace_service.go +++ b/api/v2/workspace_service.go @@ -3,13 +3,14 @@ package v2 import ( "context" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + apiv2pb "github.com/boojack/slash/proto/gen/api/v2" storepb "github.com/boojack/slash/proto/gen/store" "github.com/boojack/slash/server/profile" "github.com/boojack/slash/server/service/license" "github.com/boojack/slash/store" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) type WorkspaceService struct { diff --git a/cmd/slash/main.go b/cmd/slash/main.go index 69af11e..8250349 100644 --- a/cmd/slash/main.go +++ b/cmd/slash/main.go @@ -8,15 +8,16 @@ import ( "os/signal" "syscall" - "github.com/boojack/slash/internal/log" - "github.com/boojack/slash/server" - _profile "github.com/boojack/slash/server/profile" - "github.com/boojack/slash/store" - "github.com/boojack/slash/store/db" "github.com/spf13/cobra" "github.com/spf13/viper" "go.uber.org/zap" _ "modernc.org/sqlite" + + "github.com/boojack/slash/internal/log" + "github.com/boojack/slash/server" + "github.com/boojack/slash/server/profile" + "github.com/boojack/slash/store" + "github.com/boojack/slash/store/db" ) const ( @@ -24,25 +25,25 @@ const ( ) var ( - profile *_profile.Profile - mode string - port int - data string + serverProfile *profile.Profile + mode string + port int + data string rootCmd = &cobra.Command{ Use: "slash", Short: `An open source, self-hosted bookmarks and link sharing platform.`, Run: func(_cmd *cobra.Command, _args []string) { ctx, cancel := context.WithCancel(context.Background()) - db := db.NewDB(profile) + db := db.NewDB(serverProfile) if err := db.Open(ctx); err != nil { cancel() log.Error("failed to open database", zap.Error(err)) return } - storeInstance := store.New(db.DBInstance, profile) - s, err := server.NewServer(ctx, profile, storeInstance) + storeInstance := store.New(db.DBInstance, serverProfile) + s, err := server.NewServer(ctx, serverProfile, storeInstance) if err != nil { cancel() log.Error("failed to create server", zap.Error(err)) @@ -109,7 +110,7 @@ func init() { func initConfig() { viper.AutomaticEnv() var err error - profile, err = _profile.GetProfile() + serverProfile, err = profile.GetProfile() if err != nil { log.Error("failed to get profile", zap.Error(err)) return @@ -117,20 +118,20 @@ func initConfig() { println("---") println("Server profile") - println("dsn:", profile.DSN) - println("port:", profile.Port) - println("mode:", profile.Mode) - println("version:", profile.Version) + println("dsn:", serverProfile.DSN) + println("port:", serverProfile.Port) + println("mode:", serverProfile.Mode) + println("version:", serverProfile.Version) println("---") } func printGreetings() { - fmt.Println(greetingBanner) - fmt.Printf("Version %s has been started on port %d\n", profile.Version, profile.Port) - fmt.Println("---") - fmt.Println("See more in:") + println(greetingBanner) + fmt.Printf("Version %s has been started on port %d\n", serverProfile.Version, serverProfile.Port) + println("---") + println("See more in:") fmt.Printf("👉GitHub: %s\n", "https://github.com/boojack/slash") - fmt.Println("---") + println("---") } func main() { diff --git a/frontend/web/src/pages/SignIn.tsx b/frontend/web/src/pages/SignIn.tsx index 9be86bb..e5065c3 100644 --- a/frontend/web/src/pages/SignIn.tsx +++ b/frontend/web/src/pages/SignIn.tsx @@ -28,7 +28,7 @@ const SignIn: React.FC = () => { } if (mode === "demo") { - setEmail("steven@usememos.com"); + setEmail("steven@yourselfhosted.com"); setPassword("secret"); } }, []); diff --git a/internal/cron/cron.go b/internal/cron/cron.go index 4b4f314..7cb2b85 100644 --- a/internal/cron/cron.go +++ b/internal/cron/cron.go @@ -1,3 +1,5 @@ +package cron + // Package cron implements a crontab-like service to execute and schedule // repeative tasks/jobs. // @@ -6,13 +8,12 @@ // c := cron.New() // c.MustAdd("dailyReport", "0 0 * * *", func() { ... }) // c.Start() -package cron import ( - "errors" - "fmt" "sync" "time" + + "github.com/pkg/errors" ) type job struct { @@ -90,7 +91,7 @@ func (c *Cron) Add(jobID string, cronExpr string, run func()) error { schedule, err := NewSchedule(cronExpr) if err != nil { - return fmt.Errorf("failed to add new cron job: %w", err) + return errors.Wrap(err, "failed to add new cron job") } c.jobs[jobID] = &job{ diff --git a/internal/cron/schedule.go b/internal/cron/schedule.go index eaef644..0104f9c 100644 --- a/internal/cron/schedule.go +++ b/internal/cron/schedule.go @@ -1,11 +1,11 @@ package cron import ( - "errors" - "fmt" "strconv" "strings" "time" + + "github.com/pkg/errors" ) // Moment represents a parsed single time moment. @@ -132,7 +132,7 @@ func parseCronSegment(segment string, min int, max int) (map[int]struct{}, error return nil, err } if parsedStep < 1 || parsedStep > max { - return nil, fmt.Errorf("invalid segment step boundary - the step must be between 1 and the %d", max) + return nil, errors.New("invalid segment step boundary") } step = parsedStep default: @@ -167,7 +167,7 @@ func parseCronSegment(segment string, min int, max int) (map[int]struct{}, error return nil, err } if parsedMin < min || parsedMin > max { - return nil, fmt.Errorf("invalid segment range minimum - must be between %d and %d", min, max) + return nil, errors.New("invalid segment range minimum") } rangeMin = parsedMin @@ -176,7 +176,7 @@ func parseCronSegment(segment string, min int, max int) (map[int]struct{}, error return nil, err } if parsedMax < parsedMin || parsedMax > max { - return nil, fmt.Errorf("invalid segment range maximum - must be between %d and %d", rangeMin, max) + return nil, errors.New("invalid segment range maximum") } rangeMax = parsedMax default: diff --git a/server/embed_frontend.go b/server/embed_frontend.go index 4c7bfe1..4e759ff 100644 --- a/server/embed_frontend.go +++ b/server/embed_frontend.go @@ -5,9 +5,10 @@ import ( "io/fs" "net/http" - "github.com/boojack/slash/internal/util" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" + + "github.com/boojack/slash/internal/util" ) //go:embed dist diff --git a/server/profile/profile.go b/server/profile/profile.go index 03ff330..3dbaad5 100644 --- a/server/profile/profile.go +++ b/server/profile/profile.go @@ -7,8 +7,10 @@ import ( "runtime" "strings" - "github.com/boojack/slash/server/version" + "github.com/pkg/errors" "github.com/spf13/viper" + + "github.com/boojack/slash/server/version" ) // Profile is the configuration to start main server. @@ -44,7 +46,7 @@ func checkDSN(dataDir string) (string, error) { dataDir = strings.TrimRight(dataDir, "\\/") if _, err := os.Stat(dataDir); err != nil { - return "", fmt.Errorf("unable to access data folder %s, err %w", dataDir, err) + return "", errors.Wrapf(err, "unable to access data folder %s", dataDir) } return dataDir, nil diff --git a/server/resource.go b/server/resource.go index 87d3fd2..09531da 100644 --- a/server/resource.go +++ b/server/resource.go @@ -6,11 +6,12 @@ import ( "net/http" "os" + "github.com/h2non/filetype" + "github.com/labstack/echo/v4" + storepb "github.com/boojack/slash/proto/gen/store" "github.com/boojack/slash/server/profile" "github.com/boojack/slash/store" - "github.com/h2non/filetype" - "github.com/labstack/echo/v4" ) type ResourceService struct { diff --git a/server/server.go b/server/server.go index 1215fbf..9ef0657 100644 --- a/server/server.go +++ b/server/server.go @@ -8,15 +8,17 @@ import ( "strings" "time" + "github.com/google/uuid" + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" + "github.com/pkg/errors" + apiv1 "github.com/boojack/slash/api/v1" apiv2 "github.com/boojack/slash/api/v2" storepb "github.com/boojack/slash/proto/gen/store" "github.com/boojack/slash/server/profile" "github.com/boojack/slash/server/service/license" "github.com/boojack/slash/store" - "github.com/google/uuid" - "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" ) type Server struct { @@ -102,7 +104,7 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store s.apiV2Service = apiv2.NewAPIV2Service(secret, profile, store, licenseService, s.Profile.Port+1) // Register gRPC gateway as api v2. if err := s.apiV2Service.RegisterGateway(ctx, e); err != nil { - return nil, fmt.Errorf("failed to register gRPC gateway: %w", err) + return nil, errors.Wrap(err, "failed to register gRPC gateway") } // Register resource service. diff --git a/server/service/license/license.go b/server/service/license/license.go index ba78577..55a1a31 100644 --- a/server/service/license/license.go +++ b/server/service/license/license.go @@ -4,12 +4,13 @@ import ( "context" "time" + "github.com/pkg/errors" + "google.golang.org/protobuf/types/known/timestamppb" + apiv2pb "github.com/boojack/slash/proto/gen/api/v2" storepb "github.com/boojack/slash/proto/gen/store" "github.com/boojack/slash/server/profile" "github.com/boojack/slash/store" - "github.com/pkg/errors" - "google.golang.org/protobuf/types/known/timestamppb" ) type LicenseService struct { diff --git a/store/db/db.go b/store/db/db.go index 2ffb0ca..903744e 100644 --- a/store/db/db.go +++ b/store/db/db.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" "embed" - "errors" "fmt" "io/fs" "os" @@ -12,6 +11,8 @@ import ( "sort" "time" + "github.com/pkg/errors" + "github.com/boojack/slash/server/profile" "github.com/boojack/slash/server/version" ) @@ -39,7 +40,7 @@ func NewDB(profile *profile.Profile) *DB { func (db *DB) Open(ctx context.Context) (err error) { // Ensure a DSN is set before attempting to open the database. if db.profile.DSN == "" { - return fmt.Errorf("dsn required") + return errors.New("dsn required") } // Connect to the database with some sane settings: @@ -58,7 +59,7 @@ func (db *DB) Open(ctx context.Context) (err error) { // - https://www.sqlite.org/pragma.html sqliteDB, err := sql.Open("sqlite", db.profile.DSN+"?_pragma=foreign_keys(0)&_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)") if err != nil { - return fmt.Errorf("failed to open db with dsn: %s, err: %w", db.profile.DSN, err) + return errors.Wrapf(err, "failed to open db with dsn: %s", db.profile.DSN) } db.DBInstance = sqliteDB @@ -68,24 +69,24 @@ func (db *DB) Open(ctx context.Context) (err error) { // If db file not exists, we should create a new one with latest schema. if errors.Is(err, os.ErrNotExist) { if err := db.applyLatestSchema(ctx); err != nil { - return fmt.Errorf("failed to apply latest schema, err: %w", err) + return errors.Wrap(err, "failed to apply latest schema") } } else { - return fmt.Errorf("failed to get db file stat, err: %w", err) + return errors.Wrap(err, "failed to get db file stat") } } else { // If db file exists, we should check if we need to migrate the database. currentVersion := version.GetCurrentVersion(db.profile.Mode) migrationHistoryList, err := db.FindMigrationHistoryList(ctx, &MigrationHistoryFind{}) if err != nil { - return fmt.Errorf("failed to find migration history, err: %w", err) + return errors.Wrap(err, "failed to find migration history") } if len(migrationHistoryList) == 0 { _, err := db.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{ Version: currentVersion, }) if err != nil { - return fmt.Errorf("failed to upsert migration history, err: %w", err) + return errors.Wrap(err, "failed to upsert migration history") } return nil } @@ -103,11 +104,11 @@ func (db *DB) Open(ctx context.Context) (err error) { // backup the raw database file before migration rawBytes, err := os.ReadFile(db.profile.DSN) if err != nil { - return fmt.Errorf("failed to read raw database file, err: %w", err) + return errors.Wrap(err, "failed to read raw database file") } backupDBFilePath := fmt.Sprintf("%s/slash_%s_%d_backup.db", db.profile.Data, db.profile.Version, time.Now().Unix()) if err := os.WriteFile(backupDBFilePath, rawBytes, 0644); err != nil { - return fmt.Errorf("failed to write raw database file, err: %w", err) + return errors.Wrap(err, "failed to write raw database file") } println("succeed to copy a backup database file") @@ -117,7 +118,7 @@ func (db *DB) Open(ctx context.Context) (err error) { if version.IsVersionGreaterThan(normalizedVersion, latestMigrationHistoryVersion) && version.IsVersionGreaterOrEqualThan(currentVersion, normalizedVersion) { println("applying migration for", normalizedVersion) if err := db.applyMigrationForMinorVersion(ctx, minorVersion); err != nil { - return fmt.Errorf("failed to apply minor version migration: %w", err) + return errors.Wrap(err, "failed to apply minor version migration") } } } @@ -133,12 +134,12 @@ func (db *DB) Open(ctx context.Context) (err error) { // In non-prod mode, we should always migrate the database. if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) { if err := db.applyLatestSchema(ctx); err != nil { - return fmt.Errorf("failed to apply latest schema: %w", err) + return errors.Wrap(err, "failed to apply latest schema") } // In demo mode, we should seed the database. if db.profile.Mode == "demo" { if err := db.seed(ctx); err != nil { - return fmt.Errorf("failed to seed: %w", err) + return errors.Wrap(err, "failed to seed") } } } @@ -159,11 +160,11 @@ func (db *DB) applyLatestSchema(ctx context.Context) error { latestSchemaPath := fmt.Sprintf("%s/%s/%s", "migration", schemaMode, latestSchemaFileName) buf, err := migrationFS.ReadFile(latestSchemaPath) if err != nil { - return fmt.Errorf("failed to read latest schema %q, error %w", latestSchemaPath, err) + return errors.Wrapf(err, "failed to read latest schema %q", latestSchemaPath) } stmt := string(buf) if err := db.execute(ctx, stmt); err != nil { - return fmt.Errorf("migrate error: statement:%s err=%w", stmt, err) + return errors.Wrapf(err, "migrate error: statement %s", stmt) } return nil } @@ -171,7 +172,7 @@ func (db *DB) applyLatestSchema(ctx context.Context) error { func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion string) error { filenames, err := fs.Glob(migrationFS, fmt.Sprintf("%s/%s/*.sql", "migration/prod", minorVersion)) if err != nil { - return fmt.Errorf("failed to read ddl files, err: %w", err) + return errors.Wrap(err, "failed to read ddl files") } sort.Strings(filenames) @@ -181,12 +182,12 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st for _, filename := range filenames { buf, err := migrationFS.ReadFile(filename) if err != nil { - return fmt.Errorf("failed to read minor version migration file, filename=%s err=%w", filename, err) + return errors.Wrapf(err, "failed to read minor version migration file, filename %s", filename) } stmt := string(buf) migrationStmt += stmt if err := db.execute(ctx, stmt); err != nil { - return fmt.Errorf("migrate error: statement:%s err=%w", stmt, err) + return errors.Wrapf(err, "migrate error: statement %s", stmt) } } @@ -195,7 +196,7 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st if _, err = db.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{ Version: version, }); err != nil { - return fmt.Errorf("failed to upsert migration history with version: %s, err: %w", version, err) + return errors.Wrapf(err, "failed to upsert migration history with version %s", version) } return nil @@ -204,7 +205,7 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st func (db *DB) seed(ctx context.Context) error { filenames, err := fs.Glob(seedFS, fmt.Sprintf("%s/*.sql", "seed")) if err != nil { - return fmt.Errorf("failed to read seed files, err: %w", err) + return errors.Wrap(err, "failed to read seed files") } sort.Strings(filenames) @@ -213,11 +214,11 @@ func (db *DB) seed(ctx context.Context) error { for _, filename := range filenames { buf, err := seedFS.ReadFile(filename) if err != nil { - return fmt.Errorf("failed to read seed file, filename=%s err=%w", filename, err) + return errors.Wrapf(err, "failed to read seed file, filename %s", filename) } stmt := string(buf) if err := db.execute(ctx, stmt); err != nil { - return fmt.Errorf("seed error: statement:%s err=%w", stmt, err) + return errors.Wrapf(err, "seed error: statement %s", stmt) } } return nil @@ -226,7 +227,7 @@ func (db *DB) seed(ctx context.Context) error { // execute runs a single SQL statement within a transaction. func (db *DB) execute(ctx context.Context, stmt string) error { if _, err := db.DBInstance.ExecContext(ctx, stmt); err != nil { - return fmt.Errorf("failed to execute statement, err: %w", err) + return errors.Wrap(err, "failed to execute statement") } return nil diff --git a/store/db/seed/10001__user.sql b/store/db/seed/10001__user.sql index 0889bb2..125db86 100644 --- a/store/db/seed/10001__user.sql +++ b/store/db/seed/10001__user.sql @@ -27,7 +27,7 @@ VALUES ( 102, 'USER', - 'steven@usememos.com', + 'steven@yourselfhosted.com', 'Steven', -- raw password: secret '$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK' diff --git a/store/shortcut.go b/store/shortcut.go index 167bb11..df82f62 100644 --- a/store/shortcut.go +++ b/store/shortcut.go @@ -4,11 +4,13 @@ import ( "context" "database/sql" "encoding/json" + "errors" "fmt" "strings" - storepb "github.com/boojack/slash/proto/gen/store" "google.golang.org/protobuf/encoding/protojson" + + storepb "github.com/boojack/slash/proto/gen/store" ) // Visibility is the type of a visibility. @@ -134,7 +136,7 @@ func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*st set, args = append(set, "og_metadata = ?"), append(args, string(openGraphMetadataBytes)) } if len(set) == 0 { - return nil, fmt.Errorf("no update specified") + return nil, errors.New("no update specified") } args = append(args, update.ID) diff --git a/store/user.go b/store/user.go index d1dcf75..89152f4 100644 --- a/store/user.go +++ b/store/user.go @@ -2,7 +2,7 @@ package store import ( "context" - "fmt" + "errors" "strings" ) @@ -102,7 +102,7 @@ func (s *Store) UpdateUser(ctx context.Context, update *UpdateUser) (*User, erro } if len(set) == 0 { - return nil, fmt.Errorf("no fields to update") + return nil, errors.New("no fields to update") } stmt := ` diff --git a/store/user_setting.go b/store/user_setting.go index ef7cda1..1ff37ae 100644 --- a/store/user_setting.go +++ b/store/user_setting.go @@ -6,8 +6,9 @@ import ( "errors" "strings" - storepb "github.com/boojack/slash/proto/gen/store" "google.golang.org/protobuf/encoding/protojson" + + storepb "github.com/boojack/slash/proto/gen/store" ) type FindUserSetting struct { diff --git a/store/workspace_setting.go b/store/workspace_setting.go index d1d78a9..6109f8d 100644 --- a/store/workspace_setting.go +++ b/store/workspace_setting.go @@ -6,8 +6,9 @@ import ( "strconv" "strings" - storepb "github.com/boojack/slash/proto/gen/store" "google.golang.org/protobuf/encoding/protojson" + + storepb "github.com/boojack/slash/proto/gen/store" ) type FindWorkspaceSetting struct { diff --git a/test/server/auth_test.go b/test/server/auth_test.go index ab60974..aad2330 100644 --- a/test/server/auth_test.go +++ b/test/server/auth_test.go @@ -6,9 +6,10 @@ import ( "encoding/json" "testing" - apiv1 "github.com/boojack/slash/api/v1" "github.com/pkg/errors" "github.com/stretchr/testify/require" + + apiv1 "github.com/boojack/slash/api/v1" ) func TestAuthServer(t *testing.T) { diff --git a/test/server/server.go b/test/server/server.go index 3539683..8721bd3 100644 --- a/test/server/server.go +++ b/test/server/server.go @@ -10,16 +10,16 @@ import ( "testing" "time" + "github.com/pkg/errors" + // sqlite driver. + _ "modernc.org/sqlite" + "github.com/boojack/slash/api/auth" "github.com/boojack/slash/server" "github.com/boojack/slash/server/profile" "github.com/boojack/slash/store" "github.com/boojack/slash/store/db" "github.com/boojack/slash/test" - "github.com/pkg/errors" - - // sqlite driver. - _ "modernc.org/sqlite" ) type TestingServer struct { diff --git a/test/server/shortcut_test.go b/test/server/shortcut_test.go index f001af1..ae67981 100644 --- a/test/server/shortcut_test.go +++ b/test/server/shortcut_test.go @@ -7,9 +7,10 @@ import ( "fmt" "testing" - apiv1 "github.com/boojack/slash/api/v1" "github.com/pkg/errors" "github.com/stretchr/testify/require" + + apiv1 "github.com/boojack/slash/api/v1" ) func TestShortcutServer(t *testing.T) { diff --git a/test/server/user_test.go b/test/server/user_test.go index ee5df02..6d19414 100644 --- a/test/server/user_test.go +++ b/test/server/user_test.go @@ -7,9 +7,10 @@ import ( "fmt" "testing" - apiv1 "github.com/boojack/slash/api/v1" "github.com/pkg/errors" "github.com/stretchr/testify/require" + + apiv1 "github.com/boojack/slash/api/v1" ) func TestUserServer(t *testing.T) { @@ -31,7 +32,7 @@ func TestUserServer(t *testing.T) { user, err = s.getUserByID(user.ID) require.NoError(t, err) require.Equal(t, signup.Email, user.Email) - newEmail := "test@usermemos.com" + newEmail := "test@yourselfhosted.com" userPatch := &apiv1.PatchUserRequest{ Email: &newEmail, } diff --git a/test/store/activity_test.go b/test/store/activity_test.go index 8ae2a31..b2b5f23 100644 --- a/test/store/activity_test.go +++ b/test/store/activity_test.go @@ -4,8 +4,9 @@ import ( "context" "testing" - "github.com/boojack/slash/store" "github.com/stretchr/testify/require" + + "github.com/boojack/slash/store" ) func TestActivityStore(t *testing.T) { diff --git a/test/store/shortcut_test.go b/test/store/shortcut_test.go index bd6e49e..0ddf56d 100644 --- a/test/store/shortcut_test.go +++ b/test/store/shortcut_test.go @@ -4,9 +4,10 @@ import ( "context" "testing" + "github.com/stretchr/testify/require" + storepb "github.com/boojack/slash/proto/gen/store" "github.com/boojack/slash/store" - "github.com/stretchr/testify/require" ) func TestShortcutStore(t *testing.T) { diff --git a/test/store/store.go b/test/store/store.go index 903e766..2a21f42 100644 --- a/test/store/store.go +++ b/test/store/store.go @@ -5,12 +5,12 @@ import ( "fmt" "testing" + // sqlite driver. + _ "modernc.org/sqlite" + "github.com/boojack/slash/store" "github.com/boojack/slash/store/db" test "github.com/boojack/slash/test" - - // sqlite driver. - _ "modernc.org/sqlite" ) func NewTestingStore(ctx context.Context, t *testing.T) *store.Store { diff --git a/test/store/user_setting_test.go b/test/store/user_setting_test.go index 570fe79..776babe 100644 --- a/test/store/user_setting_test.go +++ b/test/store/user_setting_test.go @@ -4,9 +4,10 @@ import ( "context" "testing" + "github.com/stretchr/testify/require" + storepb "github.com/boojack/slash/proto/gen/store" "github.com/boojack/slash/store" - "github.com/stretchr/testify/require" ) func TestUserSettingStore(t *testing.T) { diff --git a/test/store/user_test.go b/test/store/user_test.go index e8d0871..9b8b6ed 100644 --- a/test/store/user_test.go +++ b/test/store/user_test.go @@ -4,10 +4,11 @@ import ( "context" "testing" - storepb "github.com/boojack/slash/proto/gen/store" - "github.com/boojack/slash/store" "github.com/stretchr/testify/require" "golang.org/x/crypto/bcrypt" + + storepb "github.com/boojack/slash/proto/gen/store" + "github.com/boojack/slash/store" ) func TestUserStore(t *testing.T) { diff --git a/test/store/workspace_setting_test.go b/test/store/workspace_setting_test.go index ab2b534..af33690 100644 --- a/test/store/workspace_setting_test.go +++ b/test/store/workspace_setting_test.go @@ -4,10 +4,11 @@ import ( "context" "testing" - storepb "github.com/boojack/slash/proto/gen/store" - "github.com/boojack/slash/store" "github.com/google/uuid" "github.com/stretchr/testify/require" + + storepb "github.com/boojack/slash/proto/gen/store" + "github.com/boojack/slash/store" ) func TestWorkspaceSettingStore(t *testing.T) {