From cd97df108a7e1a7bf7180eb0734f0baaa85a2951 Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Thu, 11 Jul 2024 11:33:57 -0700 Subject: [PATCH] filelist: trim whitespace from lines read from files (MR 55) Fixes issues with leading/trailing whitespaces really messing with mkinitfs --- internal/filelist/hookdirs/hookdirs.go | 2 +- internal/filelist/hookfiles/hookfiles.go | 2 +- internal/filelist/modules/modules.go | 8 ++++---- internal/filelist/modules/modules_test.go | 1 + 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/internal/filelist/hookdirs/hookdirs.go b/internal/filelist/hookdirs/hookdirs.go index cbb61f6..1ecb9fb 100644 --- a/internal/filelist/hookdirs/hookdirs.go +++ b/internal/filelist/hookdirs/hookdirs.go @@ -44,7 +44,7 @@ func (h *HookDirs) List() (*filelist.FileList, error) { s := bufio.NewScanner(f) for s.Scan() { - dir := s.Text() + dir := strings.TrimSpace(s.Text()) if len(dir) == 0 || strings.HasPrefix(dir, "#") { continue } diff --git a/internal/filelist/hookfiles/hookfiles.go b/internal/filelist/hookfiles/hookfiles.go index 5a093ad..c834c88 100644 --- a/internal/filelist/hookfiles/hookfiles.go +++ b/internal/filelist/hookfiles/hookfiles.go @@ -59,7 +59,7 @@ func slurpFiles(fd io.Reader) (*filelist.FileList, error) { s := bufio.NewScanner(fd) for s.Scan() { - line := s.Text() + line := strings.TrimSpace(s.Text()) if len(line) == 0 || strings.HasPrefix(line, "#") { continue } diff --git a/internal/filelist/modules/modules.go b/internal/filelist/modules/modules.go index 4906e2a..e6294fd 100644 --- a/internal/filelist/modules/modules.go +++ b/internal/filelist/modules/modules.go @@ -83,7 +83,7 @@ func slurpModules(fd io.Reader, modDir string) (*filelist.FileList, error) { files := filelist.NewFileList() s := bufio.NewScanner(fd) for s.Scan() { - line := s.Text() + line := strings.TrimSpace(s.Text()) if len(line) == 0 || strings.HasPrefix(line, "#") { continue } @@ -103,8 +103,8 @@ func slurpModules(fd io.Reader, modDir string) (*filelist.FileList, error) { } } else if dir == "" { // item is a module name - if modFilelist, err := getModule(s.Text(), modDir); err != nil { - return nil, fmt.Errorf("unable to get module file %q: %w", s.Text(), err) + if modFilelist, err := getModule(line, modDir); err != nil { + return nil, fmt.Errorf("unable to get module file %q: %w", line, err) } else { for _, file := range modFilelist { files.Add(file, file) @@ -188,7 +188,7 @@ func getModuleDeps(modName string, modulesDep io.Reader) ([]string, error) { s := bufio.NewScanner(modulesDep) for s.Scan() { - line := s.Text() + line := strings.TrimSpace(s.Text()) if len(line) == 0 || strings.HasPrefix(line, "#") { continue } diff --git a/internal/filelist/modules/modules_test.go b/internal/filelist/modules/modules_test.go index c88be43..51d892c 100644 --- a/internal/filelist/modules/modules_test.go +++ b/internal/filelist/modules/modules_test.go @@ -18,6 +18,7 @@ func TestStripExts(t *testing.T) { {"another_file", "another_file"}, {"a.b.c.d.e.f.g.h.i", "a"}, {"virtio_blk.ko", "virtio_blk"}, + {"virtio_blk.ko ", "virtio_blk"}, } for _, table := range tables { out := stripExts(table.in)