vfs

package
v1.2.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 10 Imported by: 1

README

Virtual File System (VFS) Package

The VFS package provides a unified api for accessing multiple file system. It is extensible and new implementation can be easily plugged in.

The default package has methods for local file system.



Installation
go get oss.nandlabs.io/golly/vfs
Usage

A simple usage of the library to create a directory in the OS.

package main

import (
    "fmt"
    "oss.nandlabs.io/golly/vfs"
)

var (
    testManager = GetManager()
)

func GetRawPath(input string) (output string) {
    currentPath, _ := os.Getwd()
    u, _ := url.Parse(input)
    path := currentPath + u.Path
    output = u.Scheme + "://" + path
    return
}

func main() {
    u := GetRawPath("file:///test-data")
    _, err := testManager.MkdirRaw(u)
    if err != nil {
       fmt.Errorf("MkdirRaw() error = %v", err)
    }
}

Documentation

Overview

package vfs provides a set of utilities for working with virtual file systems in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseFile

type BaseFile struct {
	VFile
}

func (*BaseFile) AsBytes

func (b *BaseFile) AsBytes() ([]byte, error)

func (*BaseFile) AsString

func (b *BaseFile) AsString() (s string, err error)

func (*BaseFile) WriteString

func (b *BaseFile) WriteString(s string) (int, error)

type BaseVFS

type BaseVFS struct {
	VFileSystem
}

func (*BaseVFS) Copy

func (b *BaseVFS) Copy(src, dst *url.URL) (err error)

func (*BaseVFS) CopyRaw

func (b *BaseVFS) CopyRaw(src, dst string) (err error)

func (*BaseVFS) CreateRaw

func (b *BaseVFS) CreateRaw(u string) (file VFile, err error)

func (*BaseVFS) Delete

func (b *BaseVFS) Delete(src *url.URL) (err error)

func (*BaseVFS) DeleteMatching

func (b *BaseVFS) DeleteMatching(location *url.URL, filter FileFilter) (err error)

func (*BaseVFS) DeleteRaw

func (b *BaseVFS) DeleteRaw(u string) (err error)

func (*BaseVFS) Find

func (b *BaseVFS) Find(location *url.URL, filter FileFilter) (files []VFile, err error)

func (*BaseVFS) List

func (b *BaseVFS) List(src *url.URL) (files []VFile, err error)

func (*BaseVFS) ListRaw

func (b *BaseVFS) ListRaw(src string) (files []VFile, err error)

func (*BaseVFS) MkdirAllRaw

func (b *BaseVFS) MkdirAllRaw(u string) (vFile VFile, err error)

func (*BaseVFS) MkdirRaw

func (b *BaseVFS) MkdirRaw(u string) (vFile VFile, err error)

func (*BaseVFS) Move

func (b *BaseVFS) Move(src, dst *url.URL) (err error)

func (*BaseVFS) MoveRaw

func (b *BaseVFS) MoveRaw(src, dst string) (err error)

func (*BaseVFS) OpenRaw

func (b *BaseVFS) OpenRaw(l string) (file VFile, err error)

func (*BaseVFS) Walk

func (b *BaseVFS) Walk(u *url.URL, fn WalkFn) (err error)

func (*BaseVFS) WalkRaw

func (b *BaseVFS) WalkRaw(raw string, fn WalkFn) (err error)

type FileFilter

type FileFilter func(file VFile) (bool, error)

type Manager

type Manager interface {
	VFileSystem
	Register(vfs VFileSystem)
	IsSupported(scheme string) bool
}

func GetManager

func GetManager() Manager

type OsFile

type OsFile struct {
	*BaseFile

	Location *url.URL
	// contains filtered or unexported fields
}

func (*OsFile) AddProperty

func (o *OsFile) AddProperty(name string, value string) error

func (*OsFile) Close

func (o *OsFile) Close() error

func (*OsFile) ContentType

func (o *OsFile) ContentType() string

func (*OsFile) Delete

func (o *OsFile) Delete() error

func (*OsFile) DeleteAll

func (o *OsFile) DeleteAll() error

func (*OsFile) GetProperty

func (o *OsFile) GetProperty(name string) (v string, err error)

func (*OsFile) Info

func (o *OsFile) Info() (VFileInfo, error)

func (*OsFile) ListAll

func (o *OsFile) ListAll() (files []VFile, err error)

func (*OsFile) Parent

func (o *OsFile) Parent() (file VFile, err error)

func (*OsFile) Read

func (o *OsFile) Read(b []byte) (int, error)

func (*OsFile) Seek

func (o *OsFile) Seek(offset int64, whence int) (int64, error)

func (*OsFile) Url

func (o *OsFile) Url() *url.URL

func (*OsFile) Write

func (o *OsFile) Write(b []byte) (int, error)

type OsFs

type OsFs struct {
	*BaseVFS
}

func (OsFs) Create

func (o OsFs) Create(u *url.URL) (file VFile, err error)

func (OsFs) Mkdir

func (o OsFs) Mkdir(u *url.URL) (file VFile, err error)

func (OsFs) MkdirAll

func (o OsFs) MkdirAll(u *url.URL) (file VFile, err error)

func (OsFs) Open

func (o OsFs) Open(u *url.URL) (file VFile, err error)

func (OsFs) Schemes

func (o OsFs) Schemes() []string

type VFile

type VFile interface {
	//Closer interface included from io package
	io.Closer
	//VFileContent provider interface included
	VFileContent
	//ListAll children of this file instance. can be nil in case of file object instead of directory
	ListAll() ([]VFile, error)
	//Delete the file object. If the file type is directory all files and subdirectories will be deleted
	Delete() error
	//DeleteAll deletes all the files and it's subdirectories
	DeleteAll() error
	//Info  Get the file ifo
	Info() (VFileInfo, error)
	//Parent of the file system
	Parent() (VFile, error)
	//Url of the file
	Url() *url.URL
	// AddProperty will add a property to the file
	AddProperty(name string, value string) error
	// GetProperty will add a property to the file
	GetProperty(name string) (string, error)
}

VFile interface provides the basic functions required to interact

type VFileContent

type VFileContent interface {
	io.ReadWriteSeeker
	//AsString content of the file. This should be used very carefully as it is not wise to load a large file in to string
	AsString() (string, error)
	//AsBytes content of the file.This should be used very carefully as it is not wise to load a large file into an array
	AsBytes() ([]byte, error)
	//WriteString method with write the string
	WriteString(s string) (int, error)
	//ContentType of the underlying content. If not set defaults to UTF-8 for text files
	ContentType() string
}

VFileContent interface providers access to the content

type VFileInfo

type VFileInfo interface {
	fs.FileInfo
}

type VFileSystem

type VFileSystem interface {
	//Copy File from one location to another. If the source resolves to a  directory, then all its nested children
	//will be copied. The source and destination can be of different filesystems.
	//Since the FS can be different it cannot guarantee to carry forward any symbolic links/shortcuts from source
	//instead it may try to create regular files/directories for the same even if the source and destination have
	//the same schemes
	Copy(src, dst *url.URL) error
	//CopyRaw is same as Copy except it accepts url as string
	CopyRaw(src, dst string) error
	//Create will create a new file this in the specified url. This is a
	Create(u *url.URL) (VFile, error)
	//CreateRaw is same as Create except it accepts the url as a string
	CreateRaw(raw string) (VFile, error)
	//Delete file . if the src resolves to a directory then all the files  and directories under this will be deleted
	Delete(src *url.URL) error
	//DeleteRaw is same as Delete except that it will accept url as a string
	DeleteRaw(src string) error
	//List function will list all the files if the type is a directory
	List(url *url.URL) ([]VFile, error)
	//ListRaw lists the file in the filesystem for a specific url
	ListRaw(url string) ([]VFile, error)
	//Mkdir will create the directory and will throw an error if exists or has permission issues or unable to create
	Mkdir(u *url.URL) (VFile, error)
	//MkdirRaw same as Mkdir, however it accepts  url as string
	MkdirRaw(u string) (VFile, error)
	//MkdirAll  will create all directories missing in the path
	//If the directory already exists it will not throw error, however if the path resolves to a file instead
	//it should error
	MkdirAll(u *url.URL) (VFile, error)
	//MkdirAllRaw same as MkdirAll, however it accepts  url as string
	MkdirAllRaw(u string) (VFile, error)
	//Move will
	Move(src, dst *url.URL) error
	//MoveRaw same as Move except it accepts url as string
	MoveRaw(src, dst string) error
	//Open a file based on the url of the file
	Open(u *url.URL) (VFile, error)
	// OpenRaw is same as Open function, however it accepts the url as string
	OpenRaw(u string) (VFile, error)
	//Schemes is the list of schemes supported by this file system
	Schemes() []string
	//Walk will walk through each of the files in the directory recursively.
	//If the URL resolves to a file it's not expected to throw an error instead the fn just be invoked with the VFile
	//representing the url once
	Walk(url *url.URL, fn WalkFn) error
	//Find files based on filter only works if the file.IsDir() is true
	Find(location *url.URL, filter FileFilter) ([]VFile, error)
	//WalkRaw is same as Walk except that it will accept the url as a string
	WalkRaw(raw string, fn WalkFn) error
	//DeleteMatching will delete only the files that match the filter.
	//If one of the file deletion fails with an error then it stops processing and returns error
	DeleteMatching(location *url.URL, filter FileFilter) error
}

type WalkFn

type WalkFn func(file VFile) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL