Commit 6e926d6

mo khan <mo@mokhan.ca>
2022-05-16 02:54:40
refactor: extract x package
1 parent 234dba5
Changed files (3)
cmd/migrate/main.go
@@ -1,7 +1,6 @@
 package main
 
 import (
-	"log"
 	"os"
 	"strings"
 
@@ -9,32 +8,22 @@ import (
 	"github.com/golang-migrate/migrate/v4/database/sqlite3"
 	"github.com/golang-migrate/migrate/v4/source/file"
 	"mokhan.ca/xlgmokha/idp/pkg/db"
+	"mokhan.ca/xlgmokha/idp/pkg/x"
 )
 
-func Check(err error) {
-	if err != nil {
-		log.Fatal(err)
-	}
-}
-
-func Must[T any](item T, err error) T {
-	Check(err)
-	return item
-}
-
 func main() {
-	db := Must(db.New("file:db/development.db"))
+	db := x.Must(db.New("file:db/development.db"))
 	defer db.Close()
 
-	instance := Must(sqlite3.WithInstance(db, &sqlite3.Config{}))
-	files := Must((&file.File{}).Open("./db/migrate"))
+	instance := x.Must(sqlite3.WithInstance(db, &sqlite3.Config{}))
+	files := x.Must((&file.File{}).Open("./db/migrate"))
 	defer files.Close()
 
-	migrations := Must(migrate.NewWithInstance("file", files, "sqlite3", instance))
+	migrations := x.Must(migrate.NewWithInstance("file", files, "sqlite3", instance))
 
 	if len(os.Args) == 2 && strings.ToLower(os.Args[1]) == "down" {
-		Check(migrations.Down())
+		x.Check(migrations.Down())
 	} else {
-		Check(migrations.Up())
+		x.Check(migrations.Up())
 	}
 }
pkg/tasks/create_client.go
@@ -3,6 +3,7 @@ package tasks
 import (
 	"mokhan.ca/xlgmokha/idp/pkg/db"
 	"mokhan.ca/xlgmokha/idp/pkg/dto"
+	"mokhan.ca/xlgmokha/idp/pkg/x"
 )
 
 func CreateClient(request dto.ClientRegistrationRequest) (*dto.ClientInformationResponse, error) {
@@ -10,7 +11,7 @@ func CreateClient(request dto.ClientRegistrationRequest) (*dto.ClientInformation
 	if err := response.Valid(); err != nil {
 		return nil, err
 	}
-	db, _ := db.New("file:db/development.db")
+	db := x.Must(db.New("file:db/development.db"))
 	defer db.Close()
 
 	return response, nil
pkg/x/must.go
@@ -0,0 +1,12 @@
+package x
+
+func Check(err error) {
+	if err != nil {
+		panic(err)
+	}
+}
+
+func Must[T any](item T, err error) T {
+	Check(err)
+	return item
+}