Skip to content

Commit

Permalink
feat: uniq db auto (#26)
Browse files Browse the repository at this point in the history
* feat: uniq db auto

* feat: uniq db auto

* feat: uniq db auto
  • Loading branch information
kevincobain2000 authored Jan 16, 2025
1 parent d6b43fc commit f372b5f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"database/sql"
"fmt"
"log/slog"
"os"
"time"
Expand Down Expand Up @@ -29,6 +30,12 @@ func InitDB(dbName string) (*sql.DB, error) {
return db, nil
}

func GetUniqDBName(f Flags) string {
suffix := Hash(fmt.Sprintf("%s-%s-%s-%d", f.FilePath, f.Match, f.Ignore, f.Every)) + ".sqlite"
dbName := f.DBPath + "." + suffix
return dbName
}

func DeleteDB(dbName string) error {
if _, err := os.Stat(dbName); err == nil {
if err := os.Remove(dbName); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"crypto/sha256"
"encoding/hex"
"log/slog"
"sync"

Expand All @@ -16,7 +17,7 @@ func Hash(s string) string {
h := sha256.New()
h.Write([]byte(s))
bs := h.Sum(nil)
return string(bs)
return hex.EncodeToString(bs[:3])
}

func Truncate(s string, n int) string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewWatcher(
filePath string,
f Flags,
) (*Watcher, error) {
dbName := f.DBPath + ".sqlite"
dbName := GetUniqDBName(f)
db, err := InitDB(dbName)
if err != nil {
return nil, err
Expand Down

0 comments on commit f372b5f

Please sign in to comment.