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.
65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
4 years ago
|
package paths
|
||
4 years ago
|
|
||
|
import (
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/mickael-menu/zk/util"
|
||
|
)
|
||
|
|
||
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
|
||
|
}
|
||
|
|
||
|
curDir := filepath.Dir(path)
|
||
|
if curDir == "." {
|
||
|
curDir = ""
|
||
|
}
|
||
|
|
||
4 years ago
|
c <- Metadata{
|
||
4 years ago
|
Path: path,
|
||
4 years ago
|
Modified: info.ModTime(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
logger.Println(err)
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
return c
|
||
|
}
|