zk/internal/util/paths/walk.go
Mickaël Menu 50855154e2
Architecture (#27)
* Move everything under the internal package.
* Better separation between core and adapter packages, for easier unit testing.
* Simplify data models.
* Support multiple opened notebooks during runtime (useful for the LSP server).
* Proper surface API which might be exposed later as a public Go package.
2021-04-14 20:14:01 +02:00

60 lines
1.0 KiB
Go

package paths
import (
"os"
"path/filepath"
"strings"
"github.com/mickael-menu/zk/internal/util"
)
// 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(basePath, func(abs 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 {
if isHidden || filepath.Ext(filename) != extension {
return nil
}
path, err := filepath.Rel(basePath, abs)
if err != nil {
logger.Println(err)
return nil
}
c <- Metadata{
Path: path,
Modified: info.ModTime().UTC(),
}
}
return nil
})
if err != nil {
logger.Println(err)
}
}()
return c
}