fs

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: ISC Imports: 8 Imported by: 5

README

Flexible File System

GitHub Tag Go Reference License Go Report Card Contributors Issues

The fs package provides a flexible file system abstraction for working with both local and embedded file systems. It offers various utilities for file operations such as checking existence, reading files, searching, and more.

Installation

To use the fs package, add it to your Go project:

go get github.com/go-universal/fs

Features

  • Support for both local and embedded file systems.
  • File existence checks.
  • File reading and opening.
  • Searching for files by patterns or content.
  • Lookup for multiple files matching a pattern.
  • Integration with fs.FS and http.FileSystem.

Usage

Creates a FlexibleFS instance backed by the local file system at the specified directory path.

fs := fs.NewDir(".")

Creates a FlexibleFS instance backed by the provided embedded file system.

//go:embed *.go
var embeds embed.FS

fs := fs.NewEmbed(embeds, "prefix")

Exists

Checks if a file with the given name exists in the file system.

exists, err := fs.Exists("file.go")
if err != nil {
    log.Fatal(err)
}
fmt.Println("File exists:", exists)

Open

Opens a file with the given name and returns an fs.File interface for reading the file.

file, err := fs.Open("file.go")
if err != nil {
    log.Fatal(err)
}
defer file.Close()

ReadFile

Reads the entire content of the file with the given name and returns the content as a byte slice.

content, err := fs.ReadFile("file.go")
if err != nil {
    log.Fatal(err)
}
fmt.Println(string(content))

Searches for a phrase in files within the specified directory, optionally ignoring certain files and filtering by extension.

Example:

result, err := fs.Search(".", "main", "", "go")
if err != nil {
    log.Fatal(err)
}
if result != nil {
    fmt.Println("Found:", *result)
} else {
    fmt.Println("No match found")
}

Find

Searches for a file matching the given regex pattern in the specified directory.

result, err := fs.Find(".", ".*\\.go")
if err != nil {
    log.Fatal(err)
}
if result != nil {
    fmt.Println("Found:", *result)
} else {
    fmt.Println("No match found")
}

Lookup

Searches for files matching the given regex pattern in the specified directory and returns a slice of matching file paths.

results, err := fs.Lookup(".", ".*\\.go")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Found files:", results)

PathOf

Joins the provided path parts into a single normalized path, applying the file system's prefix if applicable.

fullPath := embeddedFs.PathOf("relative", "path", "to", "file")

Prefix

Returns the path prefix for embedded file system.

prefix := fs.Prefix()

IsEmbedded

Returns true if the file system is an embedded file system.

isEmbedded := fs.IsEmbedded()

FS

Returns the underlying fs.FS interface of the file system.

fsInterface := fs.FS()

Http

Returns the http.FileSystem instance of the file system.

httpFS := fs.Http()
http.Handle("/", http.FileServer(httpFS))
log.Fatal(http.ListenAndServe(":8080", nil))

License

This project is licensed under the ISC License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlexibleFS

type FlexibleFS interface {
	// Exists checks if a file with the given name exists in the file system.
	// It returns a boolean indicating existence and an error if any occurs.
	Exists(path string) (bool, error)

	// Open opens a file with the given name and returns an fs.File interface
	// for reading the file. It returns an error if the file cannot be opened.
	Open(path string) (fs.File, error)

	// ReadFile reads the entire content of the file with the given name and
	// returns the content as a byte slice. It returns an error if the file
	// cannot be read.
	ReadFile(path string) ([]byte, error)

	// Search searches for a phrase in files within the specified directory,
	// optionally ignoring certain files and filtering by extension.
	Search(dir, phrase, ignore, ext string) (*string, error)

	// Find searches for a file matching the given pattern in the specified
	// path. It returns the path of the found file and an error if any occurs.
	Find(dir, pattern string) (*string, error)

	// Lookup searches for files matching the given pattern in the specified
	// path. It returns a slice of paths of the found files and an error if any occurs.
	Lookup(dir, pattern string) ([]string, error)

	// PathOf joins the provided path parts into a single normalized path,
	// applying the file system's prefix if applicable. It ensures the resulting
	// path is consistent with the file system's internal structure and prefixing rules.
	PathOf(parts ...string) string

	// Prefix returns the path prefix for embedded file system.
	Prefix() string

	// IsEmbedded returns true if the file system is an embedded file system.
	IsEmbedded() bool

	// FS returns the underlying fs.FS interface of the file system.
	FS() fs.FS

	// Http returns the http.FileSystem instance of the file system.
	Http() http.FileSystem
}

FlexibleFS represents a flexible file system that provides various methods for embed and local file operations.

func NewDir

func NewDir(path string) FlexibleFS

NewDir creates a FlexibleFS instance backed by the local file system at the specified directory path.

func NewEmbed

func NewEmbed(embeddedFS embed.FS, prefix string) FlexibleFS

NewEmbed creates a FlexibleFS instance backed by the provided embedded file system.

Jump to

Keyboard shortcuts

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