package fs

import "codeberg.org/anaseto/goal/io/fs"

Package fs provides functionality for working with read-only file system values and files in Goal.

Index

Functions

func DirEntries

func DirEntries(des []fs.DirEntry) goal.V

DirEntries translates a slice of fs.DirEntry values into a Goal dict value.

func Error

func Error(err error) string

Error returns an error string corresponding to one of the portable errors from fs package if possible, or calls Error() otherwise. It is used in the err field of returned error dict values.

func NewFS

func NewFS(fsys fs.FS, progstr string) goal.V

NewFS returns a new Goal file system value using fsys as underlying file system implementation and progstr as program string representation.

func NewFSError

func NewFSError(err error) goal.V

NewFSError converts a file-system Go error to a Goal error dict value with fields as described in the FAQ.

func NewFileReader

func NewFileReader(f fs.File, name string, buffered bool) goal.V

NewFileReader returns a file reader handle for the given file. The name should usually be the name passed to Open. Buffering only works on non-dir files.

func ReadFileOrDir

func ReadFileOrDir(fsys fs.FS, name string) goal.V

ReadFileOrDir reads the name file or directory and returns its contents as a Goal value, like `fs read name` would in Goal.

If fsys implements ReadFileOrDirFS, the code uses its specialized method.

func VFSubFS

func VFSubFS(ctx *goal.Context, args []goal.V) goal.V

VFSubFS implements the subfs dyad.

fs subfs dir : return a derived file system value corresponding to the subtree rooted at dir.

Types

type FS

type FS struct {
	fs.FS // read-only file system value
	// contains filtered or unexported fields
}

FS is a simple wrapper around an fs.FS file system interface value that also satisfies the goal.BV interface. It is used by subfs for file system values that do not implement fs.SubFS (unlike dirfs ones).

It may be used by extensions to easily create Goal file system values for types that do not require any extra methods.

FS transmits calls from the various optional file system methods of package fs to the underlying fs.FS value.

Use NewFS to create Goal file system values based on this type.

func (*FS) Append

func (fsys *FS) Append(_ *goal.Context, dst []byte, _ bool) []byte

Append appends the program representation of the value to dst.

func (*FS) Glob

func (fsys *FS) Glob(pattern string) ([]string, error)

Glob implements fs.GlobFS.

func (*FS) Matches

func (fsys *FS) Matches(y goal.BV) bool

Matches reports whether fsys~y.

func (*FS) ReadDir

func (fsys *FS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir implements fs.ReadDirFS.

func (*FS) ReadFile

func (fsys *FS) ReadFile(name string) ([]byte, error)

ReadFile implements fs.ReadFileFS.

func (*FS) ReadFileOrDir

func (fsys *FS) ReadFileOrDir(name string) goal.V

ReadFileOrDir implements ReadFileOrDirFS.

func (*FS) Stat

func (fsys *FS) Stat(name string) (fs.FileInfo, error)

Stat implements fs.StatFS.

func (*FS) Sub

func (fsys *FS) Sub(dir string) (fs.FS, error)

Sub implements fs.SubFS.

func (*FS) Type

func (fsys *FS) Type() string

Type returns type "/" for handle types.

type FileReader

type FileReader struct {
	// contains filtered or unexported fields
}

FileReader represents an fs.File implementing the goal.BV interface. It can make use of buffering for non-directory files.

Use NewFileReader to create Goal handle values based on this type.

func (*FileReader) Append

func (fr *FileReader) Append(ctx *goal.Context, dst []byte, _ bool) []byte

Append appends a program representation of the value to dst.

func (*FileReader) Apply

func (fr *FileReader) Apply(_ *goal.Context, args []goal.V) goal.V

Apply implements goal.Callable for file reader handles.

func (*FileReader) Buffered

func (fr *FileReader) Buffered() bool

Buffered reports whether the file reader is buffered.

func (*FileReader) Close

func (fr *FileReader) Close() error

Close implements the fs.File interface.

func (*FileReader) File

func (fr *FileReader) File() fs.File

File returns the underlying file.

func (*FileReader) IsDir

func (fr *FileReader) IsDir() bool

IsDir reports whether the file is a directory.

func (*FileReader) Matches

func (fr *FileReader) Matches(y goal.BV) bool

Matches reports whether fr~y.

func (*FileReader) Name

func (fr *FileReader) Name() string

Name returns the file's name.

func (*FileReader) Read

func (fr *FileReader) Read(p []byte) (n int, err error)

Read implements the fs.File interface.

func (*FileReader) ReadDir

func (fr *FileReader) ReadDir(n int) ([]fs.DirEntry, error)

ReadDir implements the fs.ReadDirFile interface.

func (*FileReader) ReadString

func (fr *FileReader) ReadString(delim byte) (string, error)

ReadString reads until the first occurrence of delim in the input. This operation is only supported by buffered file handles.

func (*FileReader) Reader

func (fr *FileReader) Reader() *bufio.Reader

Reader returns the underlying buffered reader, or nil if the file is unbuffered or a directory.

func (*FileReader) Stat

func (fr *FileReader) Stat() (fs.FileInfo, error)

Stat implements the fs.File interface.

func (*FileReader) Type

func (fr *FileReader) Type() string

Type returns type "h" for handle types.

func (*FileReader) WriteTo

func (fr *FileReader) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements the io.WriterTo inteface.

type ReadFileOrDirFS

type ReadFileOrDirFS interface {
	fs.FS

	// ReadFileOrDir reads the named file or directory and returns a Goal
	// value with the results.
	ReadFileOrDir(name string) goal.V
}

ReadFileOrDirFS is the interface implemented by a file system that provides an optimized implementation for reading a file or a directory in Goal using the read builtin.

Source Files

file.go fs.go

Version
v1.1.0 (latest)
Published
Feb 17, 2025
Platform
linux/amd64
Imports
8 packages
Last checked
28 minutes ago

Tools for package owners.