Documentation
¶
Index ¶
- Variables
- type Option
- type PathRewriter
- func AddPrefix(prefix string) PathRewriter
- func ChainRewriters(rewriters ...PathRewriter) PathRewriter
- func RegexRewrite(pattern, replacement string) (PathRewriter, error)
- func ReplacePrefix(oldPrefix, newPrefix string) PathRewriter
- func StaticMapping(mapping map[string]string) PathRewriter
- func StripPrefix(prefix string) PathRewriter
- type PatternType
- type Route
- type RouteCondition
- func And(conditions ...RouteCondition) RouteCondition
- func DirectoriesOnly() RouteCondition
- func FilesOnly() RouteCondition
- func MaxSize(bytes int64) RouteCondition
- func MinSize(bytes int64) RouteCondition
- func ModifiedBetween(start, end time.Time) RouteCondition
- func NewerThan(t time.Time) RouteCondition
- func Not(condition RouteCondition) RouteCondition
- func OlderThan(t time.Time) RouteCondition
- func Or(conditions ...RouteCondition) RouteCondition
- func SizeRange(minBytes, maxBytes int64) RouteCondition
- type RouteOption
- type Router
- type SwitchFS
- func (fs *SwitchFS) Chdir(dir string) error
- func (fs *SwitchFS) Chmod(name string, mode os.FileMode) error
- func (fs *SwitchFS) Chown(name string, uid, gid int) error
- func (fs *SwitchFS) Chtimes(name string, atime time.Time, mtime time.Time) error
- func (fs *SwitchFS) Create(name string) (absfs.File, error)
- func (fs *SwitchFS) Getwd() (string, error)
- func (fs *SwitchFS) Mkdir(name string, perm os.FileMode) error
- func (fs *SwitchFS) MkdirAll(name string, perm os.FileMode) error
- func (fs *SwitchFS) Open(name string) (absfs.File, error)
- func (fs *SwitchFS) OpenFile(name string, flag int, perm os.FileMode) (absfs.File, error)
- func (fs *SwitchFS) ReadDir(name string) ([]fs.DirEntry, error)
- func (fs *SwitchFS) ReadFile(name string) ([]byte, error)
- func (fs *SwitchFS) Remove(name string) error
- func (fs *SwitchFS) RemoveAll(path string) error
- func (fs *SwitchFS) Rename(oldpath, newpath string) error
- func (fs *SwitchFS) Router() Router
- func (fs *SwitchFS) Stat(name string) (os.FileInfo, error)
- func (fs *SwitchFS) Sub(dir string) (fs.FS, error)
- func (fs *SwitchFS) TempDir() string
- func (fs *SwitchFS) Truncate(name string, size int64) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoRoute is returned when no route matches the given path ErrNoRoute = errors.New("no route found for path") // ErrAllBackendsFailed is returned when both primary and failover backends fail ErrAllBackendsFailed = errors.New("all backends failed") // ErrCrossBackendOperation is returned for unsupported operations that span multiple backends ErrCrossBackendOperation = errors.New("operation spans multiple backends") // ErrInvalidPattern is returned when a route pattern is invalid ErrInvalidPattern = errors.New("invalid route pattern") // ErrDuplicateRoute is returned when attempting to add a route with an existing pattern ErrDuplicateRoute = errors.New("route with pattern already exists") // ErrNilBackend is returned when a nil backend is provided ErrNilBackend = errors.New("backend cannot be nil") )
Functions ¶
This section is empty.
Types ¶
type Option ¶
Option configures SwitchFS behavior
func WithDefault ¶
func WithDefault(backend absfs.FileSystem) Option
WithDefault sets the default backend for unmatched paths
func WithRoute ¶
func WithRoute(pattern string, backend absfs.FileSystem, opts ...RouteOption) Option
WithRoute adds a routing rule
func WithRouter ¶
WithRouter sets a custom router implementation
func WithTempDir ¶
WithTempDir sets the temporary directory path
type PathRewriter ¶
type PathRewriter interface {
// Rewrite transforms a path according to route rules
Rewrite(path string) string
}
PathRewriter rewrites/transforms paths for a route
func AddPrefix ¶
func AddPrefix(prefix string) PathRewriter
AddPrefix creates a rewriter that adds a prefix to paths
func ChainRewriters ¶
func ChainRewriters(rewriters ...PathRewriter) PathRewriter
ChainRewriters creates a rewriter that applies multiple rewriters in order
Example ¶
// Create a chain that transforms paths
chain := ChainRewriters(
StripPrefix("/virtual"),
AddPrefix("/real/storage"),
)
// /virtual/file.txt -> /real/storage/file.txt
result := chain.Rewrite("/virtual/file.txt")
_ = result // Use result
func RegexRewrite ¶
func RegexRewrite(pattern, replacement string) (PathRewriter, error)
RegexRewrite creates a rewriter that uses regex patterns
func ReplacePrefix ¶
func ReplacePrefix(oldPrefix, newPrefix string) PathRewriter
ReplacePrefix creates a rewriter that replaces one prefix with another
func StaticMapping ¶
func StaticMapping(mapping map[string]string) PathRewriter
StaticMapping creates a rewriter with a static path mapping
func StripPrefix ¶
func StripPrefix(prefix string) PathRewriter
StripPrefix creates a rewriter that removes a prefix from paths
type PatternType ¶
type PatternType int
PatternType defines how patterns are matched
const ( // PatternPrefix matches by simple prefix PatternPrefix PatternType = iota // PatternGlob matches using glob patterns PatternGlob // PatternRegex matches using regular expressions PatternRegex )
func (PatternType) String ¶
func (pt PatternType) String() string
String returns the string representation of PatternType
type Route ¶
type Route struct {
// Pattern is the path pattern (prefix, glob, or regex)
Pattern string
// Backend is the target backend filesystem
Backend absfs.FileSystem
// Priority determines match order (higher priority routes match first)
Priority int
// Type specifies how the pattern should be matched
Type PatternType
// Failover is an optional backup backend
Failover absfs.FileSystem
// Condition is an optional condition that must be met for routing
Condition RouteCondition
// Rewriter optionally transforms paths before passing to backend
Rewriter PathRewriter
// contains filtered or unexported fields
}
Route defines a routing rule
type RouteCondition ¶
type RouteCondition interface {
// Evaluate returns true if the condition is met for the given file info
Evaluate(path string, info os.FileInfo) bool
}
RouteCondition evaluates whether a route should match based on file metadata
func And ¶
func And(conditions ...RouteCondition) RouteCondition
And combines multiple conditions - all must be true
func DirectoriesOnly ¶
func DirectoriesOnly() RouteCondition
DirectoriesOnly creates a condition that matches only directories
func FilesOnly ¶
func FilesOnly() RouteCondition
FilesOnly creates a condition that matches only files (not directories)
func MaxSize ¶
func MaxSize(bytes int64) RouteCondition
MaxSize creates a condition that matches files <= maxSize bytes
func MinSize ¶
func MinSize(bytes int64) RouteCondition
MinSize creates a condition that matches files >= minSize bytes
func ModifiedBetween ¶
func ModifiedBetween(start, end time.Time) RouteCondition
ModifiedBetween creates a condition that matches files modified within a time range
func NewerThan ¶
func NewerThan(t time.Time) RouteCondition
NewerThan creates a condition that matches files modified after the given time
func OlderThan ¶
func OlderThan(t time.Time) RouteCondition
OlderThan creates a condition that matches files modified before the given time
func Or ¶
func Or(conditions ...RouteCondition) RouteCondition
Or combines multiple conditions - at least one must be true
func SizeRange ¶
func SizeRange(minBytes, maxBytes int64) RouteCondition
SizeRange creates a condition that matches files within a size range
type RouteOption ¶
RouteOption configures individual routes
func WithCondition ¶
func WithCondition(condition RouteCondition) RouteOption
WithCondition sets a condition that must be met for routing
Example ¶
backend, _ := memfs.NewFS()
// Route only large files to this backend
_, _ = New(
WithRoute("/data", backend,
WithPriority(100),
WithCondition(MinSize(1024*1024)), // Files >= 1MB
),
)
func WithFailover ¶
func WithFailover(backend absfs.FileSystem) RouteOption
WithFailover sets a failover backend
func WithPatternType ¶
func WithPatternType(pt PatternType) RouteOption
WithPatternType sets the pattern matching type
func WithRewriter ¶
func WithRewriter(rewriter PathRewriter) RouteOption
WithRewriter sets a path rewriter for the route
type Router ¶
type Router interface {
// AddRoute adds a routing rule
AddRoute(route Route) error
// RemoveRoute removes a routing rule by pattern
RemoveRoute(pattern string) error
// Route finds the backend for a given path
Route(path string) (absfs.FileSystem, error)
// RouteWithInfo finds the route for a given path with file info for condition evaluation
RouteWithInfo(path string, info os.FileInfo) (*Route, error)
// Routes returns all registered routes
Routes() []Route
}
Router manages routing decisions
type SwitchFS ¶
type SwitchFS struct {
// contains filtered or unexported fields
}
SwitchFS implements absfs.FileSystem with routing
func New ¶
New creates a new SwitchFS with the given options
Example ¶
backend1, _ := memfs.NewFS()
backend2, _ := memfs.NewFS()
// Create SwitchFS with multiple backends
fs, _ := New(
WithRoute("/hot", backend1, WithPriority(100)),
WithRoute("/cold", backend2, WithPriority(50)),
)
// Files under /hot go to backend1
f, _ := fs.Create("/hot/cache.dat")
f.Write([]byte("cached data"))
f.Close()
// Files under /cold go to backend2
f, _ = fs.Create("/cold/archive.dat")
f.Write([]byte("archived data"))
f.Close()
func (*SwitchFS) ReadDir ¶
ReadDir reads the named directory and returns a list of directory entries