Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for fs.FS #46

Closed
plastikfan opened this issue Jun 11, 2024 · 2 comments
Closed

add support for fs.FS #46

plastikfan opened this issue Jun 11, 2024 · 2 comments
Assignees
Labels
research may require documentation, but no code wontfix This will not be worked on

Comments

@plastikfan
Copy link
Contributor

plastikfan commented Jun 11, 2024

This will temporarily be implemented in traverse under storage package, but will eventually be moved to replace the existing one in extendio.

Will require the use of an in memory filesystem; use the one already defined in extendio.

Actually, we should prefer to use std library dependencies wherever possible and it turns out that there is already an in memory file system defined in fstest.MapFS implemented as a map from file-path to fstest.MapFile.

We may not even need our own storage package, rather, we wriite our own file-system dependent code to rely on io.FS. Also, we can use testing/fstest to test file system dependent code.

traverse should not make explicit calls to the native fs, rather it should only ever depend on fsys.FS, just like fs.WalkDir.

Some key points to note

  • you can create a custom filesystem FS, from a root path by:
	root := "/usr/local/go/bin"
	fileSystem := os.DirFS(root)

See fs for more details (in particular, lookk at the test cases; there are examples of how to use the in memory fs)

Also, this is an interesting reference article that discusses the fs.FS interface.

@plastikfan plastikfan added the feature New feature or request label Jun 11, 2024
@plastikfan plastikfan self-assigned this Jun 11, 2024
@plastikfan
Copy link
Contributor Author

Actually, it seems no code is required to fix this. All we need to do is just depend on fsys.FS when we navigate the file system and use fstest.MapFS for unit-testing.

@plastikfan plastikfan added research may require documentation, but no code wontfix This will not be worked on and removed feature New feature or request labels Jun 11, 2024
@plastikfan
Copy link
Contributor Author

plastikfan commented Jun 14, 2024

It should be noted that there is an fs.FS that was released after io.FS and provides additionality functionlity, such as the ability to combine multiple file systems using fs.Merge. Most of the time traverse will create a single file sytem rooted at the path requested. However, there will come a point where access to another part of the file system, not under root is required, eg when save is invoked; at this point, we create a new fs merged with the root fs.

	rootFS, _ := os.Open("/path/to/root")
	defer rootFS.Close()
	
	anotherFS, _ := os.Open("/another/path")
	defer anotherFS.Close()
	
	combinedFS := fs.Union{
	    Filesystems: []fs.FS{rootFS, anotherFS},
	}
	
	// Use combinedFS for operations across both root paths

@plastikfan plastikfan changed the title add support for io.fs add support for fs.FS Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
research may require documentation, but no code wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

1 participant