Files
godeez/internal/store/store.go
T

32 lines
604 B
Go

package store
import (
"errors"
"fmt"
"path/filepath"
"time"
bolt "go.etcd.io/bbolt"
bolterrors "go.etcd.io/bbolt/errors"
)
type Store struct {
db *bolt.DB
}
func Open(dir string) (*Store, error) {
db, err := bolt.Open(filepath.Join(dir, ".tracks.db"), 0600, &bolt.Options{Timeout: 5 * time.Second})
if err != nil {
if errors.Is(err, bolterrors.ErrTimeout) {
return nil, fmt.Errorf("database is already in use by another process")
}
return nil, fmt.Errorf("failed to open database: %w", err)
}
return &Store{db: db}, nil
}
func (s *Store) Close() error {
return s.db.Close()
}