diff --git a/cmd/importer/main.go b/cmd/importer/main.go index 9d839fb..8421ea2 100644 --- a/cmd/importer/main.go +++ b/cmd/importer/main.go @@ -15,17 +15,19 @@ func main() { flag.StringVar(&importBookmarksHtmlFile, "importFile", "", "A bookmarks.html file to import in the database") var importBookmarksUsername string flag.StringVar(&importBookmarksUsername, "importUsername", "", "The username to import the bookmarks for") + var bookmarksDbFilename string + flag.StringVar(&bookmarksDbFilename, "bookmarksDbFilename", "", "Filename of the database to import into") flag.Parse() if importBookmarksHtmlFile != "" && importBookmarksUsername != "" { var store repository.Store - err := store.InitAndVerifyDb() + err := store.InitAndVerifyDb(bookmarksDbFilename) defer store.Close() if err != nil { log.Fatalf("Error initializing database: %s", err) } - err = importer.ImportBookmarks(&store, importBookmarksHtmlFile, importBookmarksUsername) + err = importer.ImportBookmarks(&store, importBookmarksHtmlFile, importBookmarksUsername, bookmarksDbFilename) if err != nil { log.Fatalf("Error importing bookmarks: %s", err) } diff --git a/cmd/server/main.go b/cmd/server/main.go index 72b447a..a0341eb 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -23,8 +23,10 @@ func main() { if err != nil { panic(fmt.Errorf("error loading .env file: %s", err)) } + var dbFilename = requireStringFromEnv("DELBM_DB_FILENAME") + var store repository.Store - err = store.InitAndVerifyDb() + err = store.InitAndVerifyDb(dbFilename) if err != nil { panic(err) } diff --git a/internal/importer/import.go b/internal/importer/import.go index cf92008..9bc3776 100644 --- a/internal/importer/import.go +++ b/internal/importer/import.go @@ -30,7 +30,7 @@ func removeHtmlTags(s string) string { return HtmlTagRegex.ReplaceAllString(s, "") } -func ImportBookmarks(store *repository.Store, importBookmarksJsonFile, importBookmarksUsername string) error { +func ImportBookmarks(store *repository.Store, importBookmarksJsonFile, importBookmarksUsername, bookmarksDbFilename string) error { file, err := os.Open(importBookmarksJsonFile) if err != nil { return err @@ -56,10 +56,6 @@ func ImportBookmarks(store *repository.Store, importBookmarksJsonFile, importBoo bookmarks = append(bookmarks, bookmark) } log.Println("Importing", len(bookmarks), "bookmarks for user", importBookmarksUsername) - err = store.InitAndVerifyDb() - if err != nil { - panic(err) - } userId, err := store.FindOrCreateUser(importBookmarksUsername) if err != nil { panic(err) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 147bf7b..6bb4fb0 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -19,9 +19,9 @@ func (store *Store) Close() { store.db.Close() } -func (store *Store) InitAndVerifyDb() error { +func (store *Store) InitAndVerifyDb(filename string) error { var err error - store.db, err = sql.Open("sqlite3", "file:bookmarks.sqlite?_foreign_keys=on") + store.db, err = sql.Open("sqlite3", "file:"+filename+"?_foreign_keys=on") if err != nil { return err }