ren

package module
v0.2.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

README

ren

Ren is a runtime environment and script packager for Risor scripting language. Ren can be used as a library to provide scripting capabilities to Go applications or as a utility to package/run/test Risor scripts.

Installation

go get github.com/foohq/ren

Usage

// The example code illustrates how to build a package and execute it.
// The code assumes some externalities, such as that specific directories must exist on the local filesystem.
// The packager creates a file cat.zip in the current working directory, if packaging was successful.
package main

import (
	"context"
	"os"

	"github.com/foohq/ren"
	"github.com/foohq/ren/filesystems/local"
	"github.com/foohq/ren/modules"
	"github.com/foohq/ren/packager"
	risoros "github.com/risor-io/risor/os"
)

func getArgs() []string {
	if len(os.Args) > 1 {
		return os.Args[1:]
	}
	return []string{}
}

func main() {
	outputName := "cat.zip"
	// Create a package from a script found in a local directory.
	// Builder recognizes Risor scripts by .risor, .rsr extension.
	// Packager parses, compiles Risor scripts and writes compiled bytecode to .json files.
	// Files are then zipped to create a package file.
	err := packager.Build("/home/user/scripts/cat", outputName)
	if err != nil {
		panic(err)
	}

	// Instantiate a local filesystem.
	// Other compatible synthetic filesystems can be found in standalone repositories.
	// Visit https://github.com/fooHQ?q=filesystem.
	localFS, err := local.NewFS()
	if err != nil {
		panic(err)
	}

	err = ren.RunFile(
		context.Background(),
		outputName,
		ren.WithStdin(os.Stdin),
		ren.WithStdout(os.Stdout),
		ren.WithArgs(getArgs()),
		ren.WithFilesystems(map[string]risoros.FS{
			"file": localFS,
		}),
	)
	if err != nil {
		panic(err)
	}
}

Command line utility

This repository provides a command line utility which can be used to build a package and execute it. The utility can either be built from the source, or downloaded as a pre-compiled binary. For pre-compiled binaries, please refer to repository's releases.

Installing from the source requires devbox:

$ git clone https://github.com/foohq/ren
$ cd ren
$ devbox run build
$ ./build/ren -h

Package format

The package is a .zip file containing compiled Risor scripts encoded as .json files, and other files that were copied from the source directory. A special file entrypoint.json must exist within the package. entrypoint.json file is used to bootstrap a packaged script. entrypoint.json is created by compiling entrypoint.risor (or entrypoint.rsr) which must exist in the source directory. The source file is expected to contain appropriate bootstrapping code tailored for the packaged script.

Please, see examples for more information.

License

This module is distributed under the Apache License Version 2.0 found in the LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFSNotFound is returned when a filesystem is not found for a given scheme.
	ErrFSNotFound = errors.New("filesystem not found")
	// ErrCrossingFSBoundaries is returned when an operation crosses filesystem boundaries.
	ErrCrossingFSBoundaries = errors.New("crossing filesystem boundaries")
)

Functions

func Run

func Run(ctx context.Context, reader io.ReaderAt, size int64, opt ...Option) error

Run executes a Ren script from an io.ReaderAt.

func RunBytes

func RunBytes(ctx context.Context, b []byte, opts ...Option) error

RunBytes executes a Ren script provided as a byte slice.

func RunFile

func RunFile(ctx context.Context, filename string, opts ...Option) error

RunFile executes a Ren script from a file.

func Version

func Version() string

Version returns the current version of Ren.

func WithOS

func WithOS(ctx context.Context, o OS) context.Context

WithOS returns a new context with the given OS implementation.

Types

type DirEntry

type DirEntry = fs.DirEntry

DirEntry is an entry read from a directory.

type Error

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

Error represents an error that occurred during script execution.

func (*Error) Error

func (e *Error) Error() string

Error returns the error message.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying error.

type ExitHandler

type ExitHandler func(int)

ExitHandler is a function that handles os.exit calls.

type FS

type FS interface {
	Mkdir(name string, perm FileMode) error
	MkdirAll(path string, perm FileMode) error
	MkdirTemp(dir, pattern string) (string, error)
	OpenFile(name string, flag int, perm FileMode) (File, error)
	ReadFile(name string) ([]byte, error)
	Remove(name string) error
	RemoveAll(path string) error
	Rename(oldpath, newpath string) error
	Stat(name string) (FileInfo, error)
	Symlink(oldname, newname string) error
	WriteFile(name string, data []byte, perm FileMode) error
	ReadDir(name string) ([]DirEntry, error)
}

FS is an interface for a filesystem.

type File

type File interface {
	fs.File
	io.Writer
}

File represents an open file.

type FileInfo

type FileInfo = fs.FileInfo

FileInfo describes a file and is returned by Stat.

type FileMode

type FileMode = fs.FileMode

FileMode represents a file's mode and permission bits.

type Group

type Group interface {
	Gid() string
	Name() string
}

Group represents a system group.

type OS

type OS interface {
	FS
	Args() []string
	Chdir(dir string) error
	Environ() []string
	Exit(code int)
	Getpid() int
	Getuid() int
	Getwd() (dir string, err error)
	Hostname() (name string, err error)
	Setenv(key, value string) error
	Getenv(key string) string
	Unsetenv(key string) error
	LookupEnv(key string) (string, bool)
	TempDir() string
	UserCacheDir() (string, error)
	UserConfigDir() (string, error)
	UserHomeDir() (string, error)
	Stdin() File
	Stdout() File
	PathSeparator() rune
	PathListSeparator() rune
	CurrentUser() (User, error)
	LookupUser(name string) (User, error)
	LookupUid(uid string) (User, error)
	LookupGroup(name string) (Group, error)
	LookupGid(gid string) (Group, error)
}

OS provides an interface for interacting with the operating system.

func GetOS

func GetOS(ctx context.Context) OS

GetOS returns the OS implementation from the context.

type Option

type Option func(*options)

Option is a function that configures the execution of a script.

func WithArgs added in v0.1.0

func WithArgs(args []string) Option

WithArgs sets the command line arguments for the script.

func WithBuiltin

func WithBuiltin(builtin *object.Builtin) Option

WithBuiltin adds a custom builtin function to the script execution environment.

func WithExitHandler added in v0.1.0

func WithExitHandler(handler ExitHandler) Option

WithExitHandler sets the handler for os.exit calls in the script.

func WithFilesystem

func WithFilesystem(scheme string, fs FS) Option

WithFilesystem registers a filesystem for a specific URI scheme.

func WithModule added in v0.1.0

func WithModule(module *object.Module) Option

WithModule adds a custom module to the script execution environment.

func WithStdin added in v0.1.0

func WithStdin(f File) Option

WithStdin sets the standard input file for the script.

func WithStdout added in v0.1.0

func WithStdout(f File) Option

WithStdout sets the standard output file for the script.

type Pipe added in v0.1.0

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

Pipe implements ren's os.File interface and allows concurrent reads and writes.

func NewPipe added in v0.1.0

func NewPipe() *Pipe

NewPipe returns a new Pipe.

func (*Pipe) Close added in v0.1.0

func (f *Pipe) Close() error

func (*Pipe) Read added in v0.1.0

func (f *Pipe) Read(p []byte) (int, error)

func (*Pipe) Stat added in v0.1.0

func (f *Pipe) Stat() (FileInfo, error)

func (*Pipe) Write added in v0.1.0

func (f *Pipe) Write(p []byte) (int, error)

type User

type User interface {
	Uid() string
	Gid() string
	Username() string
	Name() string
	HomeDir() string
}

User represents a system user.

Directories

Path Synopsis
cmd
ren command
internal
ren
fs
os

Jump to

Keyboard shortcuts

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