mirror of https://github.com/mickael-menu/zk
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
4 years ago
|
package paths
|
||
4 years ago
|
|
||
|
import (
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"strings"
|
||
|
|
||
3 years ago
|
"github.com/mickael-menu/zk/internal/util"
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
// 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 {
|
||
4 years ago
|
extension = "." + extension
|
||
|
|
||
4 years ago
|
c := make(chan Metadata, 50)
|
||
4 years ago
|
go func() {
|
||
|
defer close(c)
|
||
|
|
||
4 years ago
|
err := filepath.Walk(basePath, func(abs string, info os.FileInfo, err error) error {
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
filename := info.Name()
|
||
|
isHidden := strings.HasPrefix(filename, ".")
|
||
|
|
||
|
if info.IsDir() {
|
||
|
if isHidden {
|
||
|
return filepath.SkipDir
|
||
|
}
|
||
|
|
||
|
} else {
|
||
4 years ago
|
if isHidden || filepath.Ext(filename) != extension {
|
||
4 years ago
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
path, err := filepath.Rel(basePath, abs)
|
||
4 years ago
|
if err != nil {
|
||
|
logger.Println(err)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
c <- Metadata{
|
||
4 years ago
|
Path: path,
|
||
4 years ago
|
Modified: info.ModTime().UTC(),
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
logger.Println(err)
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
return c
|
||
|
}
|