Files
Clayton Craft a519769979 filelist/*: use "searching" instead of "including" in top-level msg
When this is printed, it's about to search the given path for stuff to
slurp up, but it hasn't actually included anything yet. Using
"Including" here was kinda confusing, so this changes it to use
"Searching for." Hopefully that's better!
2023-03-10 22:33:32 -08:00

52 lines
1.0 KiB
Go

package hookdirs
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist"
)
type HookDirs struct {
path string
}
// New returns a new HookDirs that will use the given path to provide a list
// of directories use.
func New(path string) *HookDirs {
return &HookDirs{
path: path,
}
}
func (h *HookDirs) List() (*filelist.FileList, error) {
log.Printf("- Searching for directories specified in %s", h.path)
files := filelist.NewFileList()
fileInfo, err := os.ReadDir(h.path)
if err != nil {
log.Println("-- Unable to find dir, skipping...")
return files, nil
}
for _, file := range fileInfo {
path := filepath.Join(h.path, file.Name())
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("getHookDirs: unable to open hook file: %w", err)
}
defer f.Close()
log.Printf("-- Creating directories from: %s\n", path)
s := bufio.NewScanner(f)
for s.Scan() {
dir := s.Text()
files.Add(dir, dir)
}
}
return files, nil
}