C2FO / vfs

Pluggable, extensible virtual file system for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Assume os backend by default in vfssimple

tomzx opened this issue · comments

Is your feature request related to a problem? Please describe.
Currently you need to specify file paths with a schema. However, in many cases, existing code uses absolute/relative paths to read the local filesystem without specifying the file:// scheme. To make it easier to transition to vfssimple it would be great if those existing file paths continued to work transparently.

Describe the solution you'd like

vfssimple.NewFile("relative/file.txt") // Read using the os backend (currently results in ErrMissingScheme)
vfssimple.newFile("/absolute/file.txt") // Read using the os backend (currently results in ErrMissingScheme)
vfssimple.newFile("file:///absolute/file.txt") // Read using the os backend

Describe alternatives you've considered
n/a

Additional context
n/a

While we actually have done this (a long time ago) in vfscp (see https://github.com/C2FO/vfs/blob/master/vfscp/vfscp.go#L85-L101), I'm not sure we want to choose one scheme over another as default in vfssimple. However it should be trivial for you to it in thin wrapper using the same or similar code linked above. I'd even be open to moving that to the utils package and making it public (something like utils.PathToURI).

So you could write a wrapper name GetFile like:

func main() {
    file, err := GetFile("relative/file.txt")
    if err != nil {
       panic(err)
    }
    fmt.Println(file)
    // file:///some/absolute/path/to/relative/file.txt
}

func GetFile(path string) (vfs.File, error) {
    uri, err := utils.PathToURI(path)
    if err != nil {
        return nil, err
    }
    return vfssimple.NewFile(uri)
}