filesystem

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: MIT Imports: 7 Imported by: 0

README

go-vfs

codecov Go MIT

virtual file system for go

How to use

package main

import (
	"fmt"
	"github.com/lazychanger/filesystem"
	_ "github.com/lazychanger/filesystem/driver/memory"
	_ "github.com/lazychanger/filesystem/driver/os"
	"io"
	"log"
	"os"
)

func main() {
	memvfs, _ := filesystem.Open("memory:///?maxsize=1000000")
	
	// more 

	wd, _ := os.Getwd()
	
	osvfs, _ := filesystem.Open(fmt.Sprintf("os://%s/runtime", wd))

	// more

}

Features

  • more filesystem driver. eg. s3, etcd

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDsn

func BuildDsn(conf config) string

BuildDsn builds the DSN string from the config.

func ReadDir

func ReadDir(vfs FileSystem, name string) ([]fs.DirEntry, error)

ReadDir see fs.ReadDir

func ReadFile

func ReadFile(vfs FileSystem, name string) ([]byte, error)

ReadFile see fs.ReadFile

func RegisterDriver

func RegisterDriver(driver string, d Driver)

RegisterDriver register FileSystem

func WriteFile

func WriteFile(vfs FileSystem, name string, data []byte) error

WriteFile see os.WriteFile

Types

type Config

type Config struct {
}

func (*Config) Decode

func (conf *Config) Decode(query url.Values) error

func (*Config) Driver

func (conf *Config) Driver() string

func (*Config) Encode

func (conf *Config) Encode() url.Values

func (*Config) Host

func (conf *Config) Host() string

func (*Config) Path

func (conf *Config) Path() string

func (*Config) StringDecode

func (conf *Config) StringDecode(querystring string) error

func (*Config) StringEncode

func (conf *Config) StringEncode() string

func (*Config) UserInfo

func (conf *Config) UserInfo() *url.Userinfo

type Driver

type Driver interface {
	Open(uri *url.URL) (FileSystem, error)
}

type File

type File interface {
	fs.File
	io.Writer
}

func OpenFile

func OpenFile(vfs FileSystem, name string) (File, error)

OpenFile see os.OpenFile

type FileSystem

type FileSystem interface {

	// Open opens the named file for reading.
	// if file not exist, the error is os.ErrNotExist.
	Open(name string) (File, error)

	// Create creates the named file with mode 0666 (before umask), truncating
	// it if it already exists. If successful, methods on the returned
	// File can be used for writing; the associated file descriptor has
	// mode O_RDWR.
	// If there is an error, it will be of type *PathError.
	Create(name string) (File, error)

	// Mkdir creates a new directory with the specified name and permission
	// bits (before umask).
	// If there is an error, it will be of type *PathError.
	Mkdir(name string, perm os.FileMode) error

	// MkdirAll creates a directory named path,
	// along with any necessary parents, and returns nil,
	// or else returns an error.
	// The permission bits perm (before umask) are used for all
	// directories that MkdirAll creates.
	// If path is already a directory, MkdirAll does nothing
	// and returns nil.
	MkdirAll(path string, perm os.FileMode) error

	// Remove removes the named file or (empty) directory.
	// If there is an error, it will be of type *PathError.
	Remove(name string) error

	// RemoveAll removes path and any children it contains.
	// It removes everything it can but returns the first error
	// it encounters. If the path does not exist, RemoveAll
	// returns nil (no error).
	RemoveAll(path string) error

	// Rename renames (moves) oldpath to newpath.
	// If newpath already exists and is not a directory, Rename replaces it.
	// OS-specific restrictions may apply when oldpath and newpath are in different directories.
	// If there is an error, it will be of type *LinkError.
	Rename(oldpath, newpath string) error

	// Sub returns a Filesystem that is backed by the receiver
	Sub(dir string) (FileSystem, error)

	// Stat returns a FileInfo describing the named file.
	Stat(name string) (os.FileInfo, error)

	// Exists checks if the file exists
	Exists(name string) bool

	// IsFile returns true if the given path is a file
	IsFile(name string) bool

	// IsDir returns true if the given path is a directory
	IsDir(name string) bool
}

func Open

func Open(dns string) (FileSystem, error)

Open driver, return fs.FS and error use uri.Scheme to get driver use uri.User to get auth use uri.Host to get host use uri.Path to get root path use uri.RawQuery to get setting eg. os:///tmp/filesystem?a=1&b=2

    memory:///?maxsize=10240000
	   s3://user:pass@host/bucket?region=us-east-1
	   ftp://user:pass@host/path?passive=true
	   sftp://user:pass@host/path?passive=true

type OpenFileFs

type OpenFileFs interface {
	FileSystem
	// OpenFile see os.OpenFile
	OpenFile(name string) (File, error)
}

type ReadDirFS

type ReadDirFS interface {
	FileSystem
	// ReadDir see fs.ReadDirFS
	ReadDir(name string) ([]fs.DirEntry, error)
}

type ReadDirFile

type ReadDirFile interface {
	File
	// ReadDir see fs.ReadDirFile
	ReadDir(n int) ([]fs.DirEntry, error)
}

type ReadFileFS

type ReadFileFS interface {
	FileSystem
	// ReadFile see fs.ReadFile
	ReadFile(name string) ([]byte, error)
}

type WriteFileFS

type WriteFileFS interface {
	FileSystem
	// WriteFile see os.WriteFile
	WriteFile(name string, data []byte) error
}

Directories

Path Synopsis
driver
os

Jump to

Keyboard shortcuts

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