vfs

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 11 Imported by: 1

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirStat

func DirStat(mode int64, modifiedAt time.Time) monty.StatResult

DirStat builds a directory stat_result helper.

func FileStat

func FileStat(size int64, mode int64, modifiedAt time.Time) monty.StatResult

FileStat builds a file stat_result helper.

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

func SymlinkStat(mode int64, modifiedAt time.Time) monty.StatResult

SymlinkStat builds a symlink stat_result helper.

Types

type Environment

type Environment interface {
	Get(key string) (string, bool)
	All() map[string]string
}

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

type MapEnvironment map[string]string

MapEnvironment is a simple map-backed Environment.

func (MapEnvironment) All

func (e MapEnvironment) All() map[string]string

All returns a cloned environment map.

func (MapEnvironment) Get

func (e MapEnvironment) Get(key string) (string, bool)

Get returns a single environment variable.

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) Absolute

func (fs *MemoryFS) Absolute(pathValue string) (string, error)

Absolute normalizes the path into Monty's absolute POSIX form.

func (*MemoryFS) AddBytes

func (fs *MemoryFS) AddBytes(pathValue string, content []byte)

AddBytes preloads a binary file, creating parent directories as needed.

func (fs *MemoryFS) AddSymlink(pathValue string, target string)

AddSymlink preloads a symlink entry.

func (*MemoryFS) AddText

func (fs *MemoryFS) AddText(pathValue string, content string)

AddText preloads a text file, creating parent directories as needed.

func (*MemoryFS) Exists

func (fs *MemoryFS) Exists(pathValue string) (bool, error)

Exists reports whether the path exists.

func (*MemoryFS) IsDir

func (fs *MemoryFS) IsDir(pathValue string) (bool, error)

IsDir reports whether the path is a directory.

func (*MemoryFS) IsFile

func (fs *MemoryFS) IsFile(pathValue string) (bool, error)

IsFile reports whether the path is a file.

func (fs *MemoryFS) IsSymlink(pathValue string) (bool, error)

IsSymlink reports whether the path is a symlink.

func (*MemoryFS) Iterdir

func (fs *MemoryFS) Iterdir(pathValue string) ([]string, error)

Iterdir lists the direct children of a directory.

func (*MemoryFS) Mkdir

func (fs *MemoryFS) Mkdir(pathValue string, parents bool, existOK bool) error

Mkdir creates a directory.

func (*MemoryFS) ReadBytes

func (fs *MemoryFS) ReadBytes(pathValue string) ([]byte, error)

ReadBytes reads a file as raw bytes.

func (*MemoryFS) ReadText

func (fs *MemoryFS) ReadText(pathValue string) (string, error)

ReadText reads a UTF-8 file as text.

func (*MemoryFS) Rename

func (fs *MemoryFS) Rename(oldPath string, newPath string) error

Rename renames a file or directory.

func (*MemoryFS) Resolve

func (fs *MemoryFS) Resolve(pathValue string) (string, error)

Resolve returns the absolute path, resolving symlink targets recursively.

func (*MemoryFS) Rmdir

func (fs *MemoryFS) Rmdir(pathValue string) error

Rmdir removes an empty directory.

func (*MemoryFS) Stat

func (fs *MemoryFS) Stat(pathValue string) (monty.StatResult, error)

Stat returns stat metadata for a path.

func (fs *MemoryFS) Unlink(pathValue string) error

Unlink removes a file.

func (*MemoryFS) WriteBytes

func (fs *MemoryFS) WriteBytes(pathValue string, data []byte) (int, error)

WriteBytes writes a binary file.

func (*MemoryFS) WriteText

func (fs *MemoryFS) WriteText(pathValue string, data string) (int, error)

WriteText writes a UTF-8 text file.

Jump to

Keyboard shortcuts

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