mirror of https://github.com/mickael-menu/zk
Move Diff to zk package and add Dir walking
parent
75d1a143e7
commit
b4f2c841eb
@ -0,0 +1,82 @@
|
||||
package zk
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mickael-menu/zk/util"
|
||||
)
|
||||
|
||||
// Dir represents a directory inside a slip box.
|
||||
type Dir struct {
|
||||
// Name of the directory, which is the path relative to the slip box's root.
|
||||
Name string
|
||||
// Absolute path to the directory.
|
||||
Path string
|
||||
// User configuration for this directory.
|
||||
Config DirConfig
|
||||
}
|
||||
|
||||
// FileMetadata holds information about a note file.
|
||||
type FileMetadata struct {
|
||||
Path Path
|
||||
Modified time.Time
|
||||
}
|
||||
|
||||
// Walk emits the metadata of each note stored in the directory.
|
||||
func (d Dir) Walk(logger util.Logger) <-chan FileMetadata {
|
||||
c := make(chan FileMetadata, 50)
|
||||
go func() {
|
||||
defer close(c)
|
||||
|
||||
err := filepath.Walk(d.Path, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
filename := info.Name()
|
||||
isHidden := strings.HasPrefix(filename, ".")
|
||||
|
||||
if info.IsDir() {
|
||||
if isHidden {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
||||
} else {
|
||||
// FIXME: Customize extension in config
|
||||
if isHidden || filepath.Ext(filename) != ".md" {
|
||||
return nil
|
||||
}
|
||||
|
||||
path, err := filepath.Rel(d.Path, path)
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
curDir := filepath.Dir(path)
|
||||
if curDir == "." {
|
||||
curDir = ""
|
||||
}
|
||||
|
||||
c <- FileMetadata{
|
||||
Path: Path{
|
||||
Dir: filepath.Join(d.Name, curDir),
|
||||
Filename: filename,
|
||||
},
|
||||
Modified: info.ModTime(),
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
}
|
||||
}()
|
||||
|
||||
return c
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package zk
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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 TestWalkRootDir(t *testing.T) {
|
||||
dir := Dir{Name: "", Path: root}
|
||||
res := toSlice(dir.Walk(&util.NullLogger))
|
||||
assert.Equal(t, res, []FileMetadata{
|
||||
{
|
||||
Path: Path{Dir: "", Filename: "a.md"},
|
||||
Modified: date("2021-01-03T11:30:26.069257899+01:00"),
|
||||
},
|
||||
{
|
||||
Path: Path{Dir: "", Filename: "b.md"},
|
||||
Modified: date("2021-01-03T11:30:27.545667767+01:00"),
|
||||
},
|
||||
{
|
||||
Path: Path{Dir: "dir1", Filename: "a.md"},
|
||||
Modified: date("2021-01-03T11:31:18.961628888+01:00"),
|
||||
},
|
||||
{
|
||||
Path: Path{Dir: "dir1", Filename: "b.md"},
|
||||
Modified: date("2021-01-03T11:31:24.692881103+01:00"),
|
||||
},
|
||||
{
|
||||
Path: Path{Dir: "dir1/dir1", Filename: "a.md"},
|
||||
Modified: date("2021-01-03T11:31:27.900472856+01:00"),
|
||||
},
|
||||
{
|
||||
Path: Path{Dir: "dir2", Filename: "a.md"},
|
||||
Modified: date("2021-01-03T11:31:51.001827456+01:00"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestWalkSubDir(t *testing.T) {
|
||||
dir := Dir{Name: "dir1", Path: filepath.Join(root, "dir1")}
|
||||
res := toSlice(dir.Walk(&util.NullLogger))
|
||||
assert.Equal(t, res, []FileMetadata{
|
||||
{
|
||||
Path: Path{Dir: "dir1", Filename: "a.md"},
|
||||
Modified: date("2021-01-03T11:31:18.961628888+01:00"),
|
||||
},
|
||||
{
|
||||
Path: Path{Dir: "dir1", Filename: "b.md"},
|
||||
Modified: date("2021-01-03T11:31:24.692881103+01:00"),
|
||||
},
|
||||
{
|
||||
Path: Path{Dir: "dir1/dir1", Filename: "a.md"},
|
||||
Modified: date("2021-01-03T11:31:27.900472856+01:00"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestWalkSubSubDir(t *testing.T) {
|
||||
dir := Dir{Name: "dir1/dir1", Path: filepath.Join(root, "dir1/dir1")}
|
||||
res := toSlice(dir.Walk(&util.NullLogger))
|
||||
assert.Equal(t, res, []FileMetadata{
|
||||
{
|
||||
Path: Path{Dir: "dir1/dir1", Filename: "a.md"},
|
||||
Modified: date("2021-01-03T11:31:27.900472856+01:00"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func date(s string) time.Time {
|
||||
date, _ := time.Parse(time.RFC3339, s)
|
||||
return date
|
||||
}
|
||||
|
||||
func toSlice(c <-chan FileMetadata) []FileMetadata {
|
||||
s := make([]FileMetadata, 0)
|
||||
for fm := range c {
|
||||
s = append(s, fm)
|
||||
}
|
||||
return s
|
||||
}
|
Loading…
Reference in New Issue