Skip to content

Commit

Permalink
osfs: add constructor with optional baseDir
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed May 13, 2024
1 parent 824cc8d commit d826aee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions util/fsutil/osfs/osfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ type FS struct {
dir string
}

// New create a new OS file system for the tree of files rooted at the directory "baseDir".
//
// Giving an empty string will use the current working directory as a base.
// The path is cleaned using `path.Clean()`.
func New(baseDir string) *FS {
return &FS{dir: path.Clean(baseDir)}
}

// Open opens the named file for reading. If successful, methods on
// the returned file can be used for reading; the associated file
// descriptor has mode `O_RDONLY`.
Expand Down Expand Up @@ -115,6 +123,8 @@ func (f *FS) RemoveAll(name string) error {
//
// Because `*osfs.FS` internally uses the functions from the `os` package, bear in mind
// that changing the working directory will affect all instances of `*osfs.FS`.
//
// You can't use `Sub` if the current `osfs.FS` has a rooted prefix.
func (f *FS) Sub(dir string) (*FS, error) {
if dir == "." {
return f, nil
Expand Down
5 changes: 5 additions & 0 deletions util/fsutil/osfs/osfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,9 @@ func TestOSFS(t *testing.T) {
assert.Equal(t, "invalid name", pathErr.Err.Error())
}
})

t.Run("New", func(t *testing.T) {
fs := New("/home")
assert.Equal(t, "/home", fs.dir)
})
}

0 comments on commit d826aee

Please sign in to comment.