starbox

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 14 Imported by: 0

README ¶

🥡 starbox

godoc codecov codacy codeclimate go report

Starlark in a box -- a simple Starlark REPL and script runner.

It provides three main features:

  • Execute a Starlark script file or REPL
  • Data exchange between Starlark and Go
  • Function call from Starlark to Go, or vice versa

Documentation ¶

Overview ¶

Package starbox provides a set of utilities for building Starlark virtual machine.

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func SetLog ¶

func SetLog(l *zap.SugaredLogger)

SetLog sets the logger from outside the package.

Types ¶

type InspectCondFunc ¶ added in v0.0.2

type InspectCondFunc func(starlet.StringAnyMap, error) bool

InspectCondFunc is a function type for inspecting the converted output of Run*() and decide whether to continue.

type ModuleSetName ¶

type ModuleSetName string

ModuleSetName defines the name of a module set.

const (
	// EmptyModuleSet represents the predefined module set for empty scripts, it contains no modules.
	EmptyModuleSet ModuleSetName = "none"
	// SafeModuleSet represents the predefined module set for safe scripts, it contains only safe modules that do not have side effects with outside world.
	SafeModuleSet ModuleSetName = "safe"
	// NetworkModuleSet represents the predefined module set for network scripts, it's based on SafeModuleSet with additional network modules.
	NetworkModuleSet ModuleSetName = "network"
	// FullModuleSet represents the predefined module set for full scripts, it includes all available modules.
	FullModuleSet ModuleSetName = "full"
)

type Starbox ¶

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

Starbox is a wrapper of starlet.Machine with additional features.

func New ¶

func New(name string) *Starbox

New creates a new Starbox instance with default settings.

func (*Starbox) AddBuiltin ¶

func (s *Starbox) AddBuiltin(name string, starFunc StarlarkFunc)

AddBuiltin adds a builtin function with name to the global environment before running. If the name already exists, it will be overwritten. It panics if called after running.

func (*Starbox) AddKeyValue ¶

func (s *Starbox) AddKeyValue(key string, value interface{})

AddKeyValue adds a key-value pair to the global environment before running. If the key already exists, it will be overwritten. It panics if called after running.

func (*Starbox) AddKeyValues ¶

func (s *Starbox) AddKeyValues(keyValues starlet.StringAnyMap)

AddKeyValues adds key-value pairs to the global environment before running. Usually for output of Run()*. For each key-value pair, if the key already exists, it will be overwritten. It panics if called after running.

func (*Starbox) AddModuleData ¶

func (s *Starbox) AddModuleData(moduleName string, moduleData starlark.StringDict)

AddModuleData creates a module for the given module data along with a module loader, and adds it to the preload and lazyload registry. The given module data can be accessed in script via load("module_name", "key1") or module_name.key1. It panics if called after running.

func (*Starbox) AddModuleLoader ¶

func (s *Starbox) AddModuleLoader(moduleName string, moduleLoader starlet.ModuleLoader)

AddModuleLoader adds a custom module loader to the preload and lazyload registry. It will not load the module until the first run, and load result can be accessed in script via load("module_name", "key1") or key1 directly. It panics if called after running.

func (*Starbox) AddModuleScript ¶

func (s *Starbox) AddModuleScript(moduleName, moduleScript string)

AddModuleScript creates a module with given module script in virtual filesystem, and adds it to the preload and lazyload registry. The given module script can be accessed in script via load("module_name", "key1") or load("module_name.star", "key1") if module name has no ".star" suffix. It panics if called after running.

func (*Starbox) AddNamedModules ¶

func (s *Starbox) AddNamedModules(moduleNames ...string)

AddNamedModules adds builtin modules by name to the preload and lazyload registry. It will not load the modules until the first run. It panics if called after running.

func (*Starbox) AddStarlarkValues ¶ added in v0.0.2

func (s *Starbox) AddStarlarkValues(keyValues starlark.StringDict)

AddStarlarkValues adds key-value pairs to the global environment before running, the values are already converted to Starlark values. For each key-value pair, if the key already exists, it will be overwritten. It panics if called after running.

func (*Starbox) GetMachine ¶

func (s *Starbox) GetMachine() *starlet.Machine

GetMachine returns the underlying starlet.Machine instance.

func (*Starbox) REPL ¶

func (s *Starbox) REPL() error

REPL starts a REPL session.

func (*Starbox) Reset ¶

func (s *Starbox) Reset()

Reset creates an new Starlet machine and keeps the settings.

func (*Starbox) Run ¶

func (s *Starbox) Run(script string) (starlet.StringAnyMap, error)

Run executes a script and returns the converted output.

func (*Starbox) RunInspect ¶

func (s *Starbox) RunInspect(script string) (starlet.StringAnyMap, error)

RunInspect executes a script and then REPL with result and returns the converted output.

func (*Starbox) RunInspectIf ¶ added in v0.0.2

func (s *Starbox) RunInspectIf(script string, cond InspectCondFunc) (starlet.StringAnyMap, error)

RunInspectIf executes a script and then REPL with result and returns the converted output, if the condition is met. The condition function is called with the converted output and the error from Run*(), and returns true if REPL is needed.

func (*Starbox) RunTimeout ¶

func (s *Starbox) RunTimeout(script string, timeout time.Duration) (starlet.StringAnyMap, error)

RunTimeout executes a script and returns the converted output.

func (*Starbox) SetModuleSet ¶

func (s *Starbox) SetModuleSet(modSet ModuleSetName)

SetModuleSet sets the module set to be loaded before running. It panics if called after running.

func (*Starbox) SetPrintFunc ¶

func (s *Starbox) SetPrintFunc(printFunc starlet.PrintFunc)

SetPrintFunc sets the print function for Starlark. It panics if called after running.

func (*Starbox) SetStructTag ¶

func (s *Starbox) SetStructTag(tag string)

SetStructTag sets the custom tag of Go struct fields for Starlark. It panics if called after running.

func (*Starbox) String ¶

func (s *Starbox) String() string

String returns the name of the Starbox instance.

type StarlarkFunc ¶

type StarlarkFunc func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

StarlarkFunc is a function that can be called from Starlark.

Directories ¶

Path Synopsis
module
runtime
Package runtime implements the Starlark module for runtime information.
Package runtime implements the Starlark module for runtime information.

Jump to

Keyboard shortcuts

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