Skip to content

Commit

Permalink
feature: make the filename of the bookmarks database configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
aggregat4 committed Mar 20, 2024
1 parent 0a1c8b6 commit 6e0e42e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
6 changes: 4 additions & 2 deletions cmd/importer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 1 addition & 5 deletions internal/importer/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 6e0e42e

Please sign in to comment.