Skip to content

Commit

Permalink
Add Rel method to filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
jwillp committed Aug 27, 2024
1 parent c74f7f9 commit da73b21
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type FileSystem interface {
// differ depending on the underlying file system.
Abs(location string) (string, error)

// Rel a relative path that is lexically equivalent to targetPath when joined to basepath with an
// intervening separator.
// [Join](basepath, Rel(basepath, targpath)) is equivalent to targpath itself.
Rel(basePath, targetPath string) (string, error)

// StatPath returns file information for the specified location. This typically
// includes details like size, modification time, and whether the path is a file
// or directory.
Expand Down Expand Up @@ -54,6 +59,10 @@ var _ FileSystem = LocalFileSystem{}
// LocalFileSystem is an implementation of a FileSystem that works on the local file system where this program is running.
type LocalFileSystem struct{}

func (l LocalFileSystem) Rel(basePath, targetPath string) (string, error) {
return filepath.Rel(basePath, targetPath)
}

func (l LocalFileSystem) Remove(path string) error {
return os.Remove(path)
}
Expand Down
4 changes: 4 additions & 0 deletions filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ type mockFileSystem struct {
rmErr error
}

func (m *mockFileSystem) Rel(basePath, targetPath string) (string, error) {
return strings.ReplaceAll(targetPath, basePath, "./"), nil
}

func (m *mockFileSystem) WriteFile(filePath string, data []byte, _ fs.FileMode) error {
if m.writeFileErr != nil {
return m.writeFileErr
Expand Down

0 comments on commit da73b21

Please sign in to comment.