Move zk/file to util/paths

pull/6/head
Mickaël Menu 3 years ago
parent ea3b6923c0
commit e0aaa60955
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -4,10 +4,10 @@ import (
"database/sql"
"time"
"github.com/mickael-menu/zk/core/file"
"github.com/mickael-menu/zk/core/note"
"github.com/mickael-menu/zk/util"
"github.com/mickael-menu/zk/util/errors"
"github.com/mickael-menu/zk/util/paths"
)
// NoteDAO persists notes in the SQLite database.
@ -51,7 +51,7 @@ func NewNoteDAO(tx Transaction, logger util.Logger) *NoteDAO {
}
}
func (d *NoteDAO) Indexed() (<-chan file.Metadata, error) {
func (d *NoteDAO) Indexed() (<-chan paths.Metadata, error) {
wrap := errors.Wrapper("failed to get indexed notes")
rows, err := d.indexedStmt.Query()
@ -59,7 +59,7 @@ func (d *NoteDAO) Indexed() (<-chan file.Metadata, error) {
return nil, wrap(err)
}
c := make(chan file.Metadata)
c := make(chan paths.Metadata)
go func() {
defer close(c)
defer rows.Close()
@ -74,7 +74,7 @@ func (d *NoteDAO) Indexed() (<-chan file.Metadata, error) {
d.logger.Err(wrap(err))
}
c <- file.Metadata{
c <- paths.Metadata{
Path: path,
Modified: modified,
}

@ -5,17 +5,17 @@ import (
"testing"
"time"
"github.com/mickael-menu/zk/core/file"
"github.com/mickael-menu/zk/core/note"
"github.com/mickael-menu/zk/util"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/paths"
)
func TestNoteDAOIndexed(t *testing.T) {
testTransaction(t, func(tx Transaction) {
dao := NewNoteDAO(tx, &util.NullLogger)
expected := []file.Metadata{
expected := []paths.Metadata{
{
Path: "f39c8.md",
Modified: date("2020-01-20T08:52:42.321024+01:00"),

@ -1,11 +0,0 @@
package file
import (
"time"
)
// Metadata holds information about a slip box file.
type Metadata struct {
Path string
Modified time.Time
}

@ -8,10 +8,10 @@ import (
"strings"
"time"
"github.com/mickael-menu/zk/core/file"
"github.com/mickael-menu/zk/core/zk"
"github.com/mickael-menu/zk/util"
"github.com/mickael-menu/zk/util/errors"
"github.com/mickael-menu/zk/util/paths"
"gopkg.in/djherbis/times.v1"
)
@ -29,7 +29,7 @@ type Metadata struct {
// Indexer persists the notes index.
type Indexer interface {
// Indexed returns the list of indexed note file metadata.
Indexed() (<-chan file.Metadata, error)
Indexed() (<-chan paths.Metadata, error)
// Add indexes a new note from its metadata.
Add(metadata Metadata) error
// Update updates the metadata of an already indexed note.
@ -42,29 +42,29 @@ type Indexer interface {
func Index(dir zk.Dir, indexer Indexer, logger util.Logger) error {
wrap := errors.Wrapper("indexation failed")
source := file.Walk(dir, dir.Config.Extension, logger)
source := paths.Walk(dir.Path, dir.Config.Extension, logger)
target, err := indexer.Indexed()
if err != nil {
return wrap(err)
}
err = file.Diff(source, target, func(change file.DiffChange) error {
err = paths.Diff(source, target, func(change paths.DiffChange) error {
switch change.Kind {
case file.DiffAdded:
case paths.DiffAdded:
metadata, err := metadata(change.Path, dir.Path)
if err == nil {
err = indexer.Add(metadata)
}
logger.Err(err)
case file.DiffModified:
case paths.DiffModified:
metadata, err := metadata(change.Path, dir.Path)
if err == nil {
err = indexer.Update(metadata)
}
logger.Err(err)
case file.DiffRemoved:
case paths.DiffRemoved:
err := indexer.Remove(change.Path)
logger.Err(err)
}

@ -1,4 +1,4 @@
package file
package paths
// DiffChange represents a file change made in a directory.
type DiffChange struct {
@ -15,10 +15,10 @@ const (
DiffRemoved
)
// Diff compares two sources of file.Metadata and report the file changes, using
// the file modification date.
// Diff compares two sources of Metadata and report the file changes, using the
// file modification date.
//
// Warning: The file.Metadata have to be sorted by their Path for the diffing to
// Warning: The Metadata have to be sorted by their Path for the diffing to
// work properly.
func Diff(source, target <-chan Metadata, callback func(DiffChange) error) error {
var err error

@ -1,4 +1,4 @@
package file
package paths
import (
"errors"

@ -4,8 +4,15 @@ import (
"os"
"path/filepath"
"strings"
"time"
)
// Metadata holds information about a file path.
type Metadata struct {
Path string
Modified time.Time
}
// Exists returns whether the given path exists on the file system.
func Exists(path string) (bool, error) {
fi, err := fileInfo(path)

@ -1,23 +1,23 @@
package file
package paths
import (
"os"
"path/filepath"
"strings"
"github.com/mickael-menu/zk/core/zk"
"github.com/mickael-menu/zk/util"
)
// Walk emits the metadata of each file stored in the directory.
func Walk(dir zk.Dir, extension string, logger util.Logger) <-chan Metadata {
// Walk emits the metadata of each file stored in the directory with the given extension.
// Hidden files and directories are ignored.
func Walk(basePath string, extension string, logger util.Logger) <-chan Metadata {
extension = "." + extension
c := make(chan Metadata, 50)
go func() {
defer close(c)
err := filepath.Walk(dir.Path, func(abs string, info os.FileInfo, err error) error {
err := filepath.Walk(basePath, func(abs string, info os.FileInfo, err error) error {
if err != nil {
return err
}
@ -35,7 +35,7 @@ func Walk(dir zk.Dir, extension string, logger util.Logger) <-chan Metadata {
return nil
}
path, err := filepath.Rel(dir.Path, abs)
path, err := filepath.Rel(basePath, abs)
if err != nil {
logger.Println(err)
return nil
@ -47,7 +47,7 @@ func Walk(dir zk.Dir, extension string, logger util.Logger) <-chan Metadata {
}
c <- Metadata{
Path: filepath.Join(dir.Name, curDir, filename),
Path: path,
Modified: info.ModTime(),
}
}

@ -1,21 +1,17 @@
package file
package paths
import (
"path/filepath"
"testing"
"time"
"github.com/mickael-menu/zk/core/zk"
"github.com/mickael-menu/zk/util"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/fixtures"
)
var root = fixtures.Path("walk")
func TestWalk(t *testing.T) {
var path = fixtures.Path("walk")
func TestWalkRootDir(t *testing.T) {
dir := zk.Dir{Name: "", Path: root}
testEqual(t, Walk(dir, "md", &util.NullLogger), []string{
testEqual(t, Walk(path, "md", &util.NullLogger), []string{
"a.md",
"b.md",
"dir1/a.md",
@ -25,27 +21,6 @@ func TestWalkRootDir(t *testing.T) {
})
}
func TestWalkSubDir(t *testing.T) {
dir := zk.Dir{Name: "dir1", Path: filepath.Join(root, "dir1")}
testEqual(t, Walk(dir, "md", &util.NullLogger), []string{
"dir1/a.md",
"dir1/b.md",
"dir1/dir1/a.md",
})
}
func TestWalkSubSubDir(t *testing.T) {
dir := zk.Dir{Name: "dir1/dir1", Path: filepath.Join(root, "dir1/dir1")}
testEqual(t, Walk(dir, "md", &util.NullLogger), []string{
"dir1/dir1/a.md",
})
}
func date(s string) time.Time {
date, _ := time.Parse(time.RFC3339, s)
return date
}
func testEqual(t *testing.T, actual <-chan Metadata, expected []string) {
popExpected := func() (string, bool) {
if len(expected) == 0 {
Loading…
Cancel
Save