Skip to content

Commit

Permalink
Move sqlite db file to .expense-tracker directory
Browse files Browse the repository at this point in the history
Signed-off-by: Masudur Rahman <[email protected]>
  • Loading branch information
masudur-rahman committed Feb 26, 2024
1 parent bfd90ae commit 64dddab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ bin/
.container-*
.dockerfile-*

expense-tracker.db
.expense-tracker/expense-tracker.db
2 changes: 1 addition & 1 deletion configs/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func InitiateDatabaseConnection(ctx context.Context) error {
}

func getSQLiteDatabase(ctx context.Context) (isql.Database, error) {
conn, err := lib.GetSQLiteConnection("expense-tracker.db")
conn, err := lib.GetSQLiteConnection(google.DatabasePath())
if err != nil {
return nil, err
}
Expand Down
17 changes: 12 additions & 5 deletions modules/google/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -21,6 +22,13 @@ const (
dbFolderName = ".expense-tracker"
)

func DatabasePath() string {
if err := os.MkdirAll(dbFolderName, os.ModePerm); err != nil {
log.Fatalln(err)
}
return filepath.Join(dbFolderName, dbFileName)
}

func getDriveService() (*drive.Service, error) {
creds, err := google.FindDefaultCredentials(context.Background(), drive.DriveScope)
if err != nil {
Expand Down Expand Up @@ -110,13 +118,12 @@ func SyncDatabaseFromDrive() error {
return err
}

upstreamDBPath := fmt.Sprintf("%s/%s", dbFolderName, dbFileName)
data, err := readFileFromDrive(svc, upstreamDBPath)
data, err := readFileFromDrive(svc, DatabasePath())
if err != nil {
return err
}

return os.WriteFile(dbFileName, data, 0666)
return os.WriteFile(DatabasePath(), data, 0666)
}

func SyncDatabaseToDrive() error {
Expand All @@ -125,8 +132,8 @@ func SyncDatabaseToDrive() error {
return err
}

upstreamDBPath := fmt.Sprintf("%s/%s", dbFolderName, dbFileName)
return uploadFileToDrive(svc, upstreamDBPath, dbFileName)
dbPath := DatabasePath()
return uploadFileToDrive(svc, dbPath, dbPath)
}

func SyncDatabaseToDrivePeriodically(interval time.Duration) {
Expand Down

0 comments on commit 64dddab

Please sign in to comment.