Skip to content

Commit

Permalink
Add symlink support to files.CopyDir
Browse files Browse the repository at this point in the history
  • Loading branch information
Sploder12 committed Jan 21, 2025
1 parent 8960fef commit b88e18d
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 76 deletions.
2 changes: 1 addition & 1 deletion internal/collector/sysinfo/sysinfo_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s Manager) readFile(file string) string {
return ""
}

return string(f)
return strings.TrimSpace(string(f))
}

func (s Manager) collectProduct() map[string]string {
Expand Down
2 changes: 1 addition & 1 deletion internal/collector/sysinfo/sysinfo_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestCollect(t *testing.T) {
t.FailNow()
}

root := filepath.Join(tmp, "linuxfs", tc.root)
root := filepath.Join(tmp, tc.root)
for _, f := range tc.missingFiles {
err := os.Remove(filepath.Join(root, f))
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ hardware:
product:
Family: Framework DIY
Name: DIY Framework Laptop 16
Vendor: |
Framework
Vendor: Framework
cpu:
cpu:
'Architecture:': x86_64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ hardware:
product:
Family: Framework DIY
Name: DIY Framework Laptop 16
Vendor: |
Framework
Vendor: Framework
cpu:
cpu: {}
gpus:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ hardware:
product:
Family: Framework DIY
Name: DIY Framework Laptop 16
Vendor: |
Framework
Vendor: Framework
cpu:
cpu:
'Architecture:': x86_64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ hardware:
product:
Family: Framework DIY
Name: DIY Framework Laptop 16
Vendor: |
Framework
Vendor: Framework
cpu:
cpu:
'Architecture:': x86_64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ hardware:
product:
Family: Framework DIY
Name: DIY Framework Laptop 16
Vendor: |
Framework
Vendor: Framework
cpu:
cpu:
'Architecture:': x86_64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ hardware:
product:
Family: Framework DIY
Name: DIY Framework Laptop 16
Vendor: |
Framework
Vendor: Framework
cpu:
cpu:
'Architecture:': x86_64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ hardware:
product:
Family: Framework DIY
Name: DIY Framework Laptop 16
Vendor: |
Framework
Vendor: Framework
cpu:
cpu:
'Architecture:': x86_64
Expand Down
17 changes: 17 additions & 0 deletions internal/testutils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package testutils

import (
"io"
"io/fs"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -35,6 +36,19 @@ func CopyFile(src, dst string) error {
return err
}

func CopySymlink(src, dst string) error {

Check failure on line 39 in internal/testutils/files.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (ubuntu-24.04)

exported: exported function CopySymlink should have comment or be unexported (revive)

Check failure on line 39 in internal/testutils/files.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (windows-2022)

exported: exported function CopySymlink should have comment or be unexported (revive)

Check failure on line 39 in internal/testutils/files.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

exported: exported function CopySymlink should have comment or be unexported (revive)

Check failure on line 39 in internal/testutils/files.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

exported: exported function CopySymlink should have comment or be unexported (revive)
lnk, err := os.Readlink(src)
if err != nil {
return err
}

err = os.Symlink(lnk, dst)
if err != nil {
return err
}
return nil
}

// CopyDir copies the contents of a directory to another directory.
func CopyDir(srcDir, dstDir string) error {
return filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
Expand All @@ -49,6 +63,9 @@ func CopyDir(srcDir, dstDir string) error {
if info.IsDir() {
return os.MkdirAll(dstPath, info.Mode())
}
if info.Mode()&fs.ModeSymlink > 0 {
return CopySymlink(path, dstPath)
}
return CopyFile(path, dstPath)
})
}
60 changes: 0 additions & 60 deletions internal/testutils/filesystem.go

This file was deleted.

0 comments on commit b88e18d

Please sign in to comment.