Documentation
¶
Overview ¶
Package vfs adapts Go-defined filesystems and environments into Monty OS handlers.
It includes an in-memory filesystem for tests, examples, and applications that want to keep Monty's file access fully inside Go-owned state.
Index ¶
- func DirStat(mode int64, modifiedAt time.Time) monty.StatResult
- func FileStat(size int64, mode int64, modifiedAt time.Time) monty.StatResult
- func Handler(fileSystem FileSystem, environment Environment) monty.OSHandler
- func SymlinkStat(mode int64, modifiedAt time.Time) monty.StatResult
- type Environment
- type FileSystem
- type MapEnvironment
- type MemoryFS
- func (fs *MemoryFS) Absolute(pathValue string) (string, error)
- func (fs *MemoryFS) AddBytes(pathValue string, content []byte)
- func (fs *MemoryFS) AddSymlink(pathValue string, target string)
- func (fs *MemoryFS) AddText(pathValue string, content string)
- func (fs *MemoryFS) Exists(pathValue string) (bool, error)
- func (fs *MemoryFS) IsDir(pathValue string) (bool, error)
- func (fs *MemoryFS) IsFile(pathValue string) (bool, error)
- func (fs *MemoryFS) IsSymlink(pathValue string) (bool, error)
- func (fs *MemoryFS) Iterdir(pathValue string) ([]string, error)
- func (fs *MemoryFS) Mkdir(pathValue string, parents bool, existOK bool) error
- func (fs *MemoryFS) ReadBytes(pathValue string) ([]byte, error)
- func (fs *MemoryFS) ReadText(pathValue string) (string, error)
- func (fs *MemoryFS) Rename(oldPath string, newPath string) error
- func (fs *MemoryFS) Resolve(pathValue string) (string, error)
- func (fs *MemoryFS) Rmdir(pathValue string) error
- func (fs *MemoryFS) Stat(pathValue string) (monty.StatResult, error)
- func (fs *MemoryFS) Unlink(pathValue string) error
- func (fs *MemoryFS) WriteBytes(pathValue string, data []byte) (int, error)
- func (fs *MemoryFS) WriteText(pathValue string, data string) (int, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handler ¶
func Handler(fileSystem FileSystem, environment Environment) monty.OSHandler
Handler converts a FileSystem and Environment into a Monty OS handler.
Example ¶
package main
import (
"context"
"fmt"
monty "github.com/ewhauser/gomonty"
"github.com/ewhauser/gomonty/vfs"
)
func main() {
fileSystem := vfs.NewMemoryFS()
fileSystem.AddText("/data/input.txt", "hello from memory")
runner, err := monty.New(`
from pathlib import Path
Path("/data/input.txt").read_text()
`, monty.CompileOptions{
ScriptName: "example.py",
})
if err != nil {
panic(err)
}
value, err := runner.Run(context.Background(), monty.RunOptions{
OS: vfs.Handler(fileSystem, vfs.MapEnvironment{
"HOME": "/sandbox",
}),
})
if err != nil {
panic(err)
}
fmt.Println(value.Raw())
}
Output: hello from memory
func SymlinkStat ¶
SymlinkStat builds a symlink stat_result helper.
Types ¶
type Environment ¶
Environment is the typed environment-variable surface expected by the bindings.
type FileSystem ¶
type FileSystem interface {
Exists(path string) (bool, error)
IsFile(path string) (bool, error)
IsDir(path string) (bool, error)
IsSymlink(path string) (bool, error)
ReadText(path string) (string, error)
ReadBytes(path string) ([]byte, error)
WriteText(path string, data string) (int, error)
WriteBytes(path string, data []byte) (int, error)
Mkdir(path string, parents bool, existOK bool) error
Unlink(path string) error
Rmdir(path string) error
Iterdir(path string) ([]string, error)
Stat(path string) (monty.StatResult, error)
Rename(oldPath string, newPath string) error
Resolve(path string) (string, error)
Absolute(path string) (string, error)
}
FileSystem is the typed OS surface expected by the Monty Go bindings.
type MapEnvironment ¶
MapEnvironment is a simple map-backed Environment.
func (MapEnvironment) All ¶
func (e MapEnvironment) All() map[string]string
All returns a cloned environment map.
type MemoryFS ¶
type MemoryFS struct {
// contains filtered or unexported fields
}
MemoryFS is a simple in-memory filesystem implementation for Monty.
func NewMemoryFS ¶
func NewMemoryFS() *MemoryFS
NewMemoryFS constructs an empty in-memory filesystem rooted at `/`.
func (*MemoryFS) AddSymlink ¶
AddSymlink preloads a symlink entry.
func (*MemoryFS) Resolve ¶
Resolve returns the absolute path, resolving symlink targets recursively.
func (*MemoryFS) WriteBytes ¶
WriteBytes writes a binary file.