From b0e28b4215b761c3c0878929f9fe76536538872f Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Sat, 18 Feb 2023 16:21:20 -0800 Subject: [PATCH] filelist/hookdirs: add new FileLister implementation for creating dirs --- internal/filelist/hookdirs/hookdirs.go | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 internal/filelist/hookdirs/hookdirs.go diff --git a/internal/filelist/hookdirs/hookdirs.go b/internal/filelist/hookdirs/hookdirs.go new file mode 100644 index 0000000..85d5837 --- /dev/null +++ b/internal/filelist/hookdirs/hookdirs.go @@ -0,0 +1,51 @@ +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("- Creating 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 +}