Documentation ¶
Overview ¶
Starkit is a toolkit for implementing Starlark interpreters, with support for:
(1) reusable sets of builtins (2) collecting state on a starlark thread (3) instrumenting builtins with analytics
So that builtins from different packages can be composed.
This package is designed to eventually be a separate repo.
Index ¶
- Variables
- func AbsPath(t *starlark.Thread, path string) string
- func AbsWorkingDir(t *starlark.Thread) string
- func ContextFromThread(t *starlark.Thread) (context.Context, error)
- func CurrentExecPath(t *starlark.Thread) string
- func SetState(t *starlark.Thread, valOrFn interface{}) error
- func StartTiltfileFromThread(t *starlark.Thread) (*v1alpha1.Tiltfile, error)
- func UnpackArgs(t *starlark.Thread, fnName string, args starlark.Tuple, ...) error
- func UnpackBacktrace(err error) error
- type ArgUnpacker
- type BuiltinCall
- type Environment
- func (e *Environment) AddBuiltin(name string, f Function) error
- func (e *Environment) AddLoadInterceptor(i LoadInterceptor)
- func (e *Environment) AddValue(name string, val starlark.Value) error
- func (e *Environment) SetArgUnpacker(unpackArgs ArgUnpacker)
- func (e *Environment) SetContext(ctx context.Context)
- func (e *Environment) SetFakeFileSystem(files map[string]string)
- func (e *Environment) SetPrint(print func(thread *starlark.Thread, msg string))
- func (e *Environment) StartTiltfile() *v1alpha1.Tiltfile
- type Fixture
- func (f *Fixture) ExecFile(name string) (Model, error)
- func (f *Fixture) File(name, contents string)
- func (f *Fixture) JoinPath(elem ...string) string
- func (f *Fixture) OnStart(e *Environment) error
- func (f *Fixture) Path() string
- func (f *Fixture) PrintOutput() string
- func (f *Fixture) SetContext(ctx context.Context)
- func (f *Fixture) SetLoadInterceptor(i LoadInterceptor)
- func (f *Fixture) SetOutput(out *bytes.Buffer)
- func (f *Fixture) Symlink(old, new string)
- func (f *Fixture) Tiltfile() *v1alpha1.Tiltfile
- func (f *Fixture) UseRealFS()
- type Function
- type LoadInterceptor
- type Model
- type Module
- type OnBuiltinCallPlugin
- type OnExecPlugin
- type Plugin
- type StatefulPlugin
Constants ¶
This section is empty.
Variables ¶
var ErrStopExecution = errors.New("stop execution")
ErrStopExecution is a sentinel value to stop Starlark execution but will not be propagated back to callers.
It is used by the custom exit() built-in to allow halting Tiltfile execution in a non-fatal manner.
Functions ¶
func AbsPath ¶
We want to resolve paths relative to the dir where the currently executing file lives, not relative to the working directory.
func AbsWorkingDir ¶
func ContextFromThread ¶ added in v0.14.0
func CurrentExecPath ¶
Path to the file that's currently executing
func SetState ¶
SetState works like SetState in React. It can take a value or a function.
That function should transform old state into new state. It can have the signature `func(T) T` or `func(T) (T, error)`.
For example, an plugin that accumulated strings might use SetState() like this:
err := starkit.SetState(t, func(strings []string) { return append([]string{newString}, strings...) })
This would be so much easier with generics :grimace:
SetState will return an error if it can't match the type of anything in the state store.
func StartTiltfileFromThread ¶ added in v0.22.10
func UnpackArgs ¶
func UnpackArgs(t *starlark.Thread, fnName string, args starlark.Tuple, kwargs []starlark.Tuple, pairs ...interface{}) error
Unpacks args, using the arg unpacker on the current thread.
func UnpackBacktrace ¶ added in v0.10.16
Keep unwrapping errors until we find an error with a backtrace.
Types ¶
type ArgUnpacker ¶
type BuiltinCall ¶ added in v0.17.3
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
A starlark execution environment.
func (*Environment) AddBuiltin ¶
func (e *Environment) AddBuiltin(name string, f Function) error
Add a builtin to the environment.
All builtins will be wrapped to invoke OnBuiltinCall on every plugin.
All builtins should use starkit.UnpackArgs to get instrumentation.
func (*Environment) AddLoadInterceptor ¶ added in v0.14.0
func (e *Environment) AddLoadInterceptor(i LoadInterceptor)
func (*Environment) AddValue ¶
func (e *Environment) AddValue(name string, val starlark.Value) error
func (*Environment) SetArgUnpacker ¶
func (e *Environment) SetArgUnpacker(unpackArgs ArgUnpacker)
func (*Environment) SetContext ¶ added in v0.14.0
func (e *Environment) SetContext(ctx context.Context)
func (*Environment) SetFakeFileSystem ¶
func (e *Environment) SetFakeFileSystem(files map[string]string)
Set a fake file system so that we can write tests that don't touch the file system. Expressed as a map from paths to contents.
func (*Environment) SetPrint ¶
func (e *Environment) SetPrint(print func(thread *starlark.Thread, msg string))
func (*Environment) StartTiltfile ¶ added in v0.22.10
func (e *Environment) StartTiltfile() *v1alpha1.Tiltfile
The tiltfile model driving this environment.
type Fixture ¶
type Fixture struct {
// contains filtered or unexported fields
}
A fixture for test setup/teardown
func (*Fixture) OnStart ¶
func (f *Fixture) OnStart(e *Environment) error
func (*Fixture) PrintOutput ¶
func (*Fixture) SetContext ¶ added in v0.18.3
func (*Fixture) SetLoadInterceptor ¶ added in v0.14.0
func (f *Fixture) SetLoadInterceptor(i LoadInterceptor)
type LoadInterceptor ¶ added in v0.14.0
type LoadInterceptor interface { // LocalPath returns the path that the Tiltfile code should be read from. // Must be stable, because it's used as a cache key // Ensure the content is present in the path returned // Returns "" if this interceptor doesn't act on this path LocalPath(t *starlark.Thread, path string) (string, error) }
LoadInterceptor allows an Plugin to intercept a load to set the contents based on the requested path.
type Model ¶
type Model struct { BuiltinCalls []BuiltinCall // contains filtered or unexported fields }
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
A frozen starlark module object. Can only be changed from Go.
type OnBuiltinCallPlugin ¶ added in v0.22.4
type OnExecPlugin ¶ added in v0.22.4
type Plugin ¶ added in v0.22.4
type Plugin interface { // Called when execution begins. OnStart(e *Environment) error }
An plugin to a starlark execution environment.
type StatefulPlugin ¶ added in v0.22.4
type StatefulPlugin interface { Plugin NewState() interface{} }
Starkit plugins are not allowed to have mutable state.
Starlark has different ideas about mutable state than most programming languages. In particular, state is mutable during file execution, but becomes immutable when it's imported into other files.
This allows Starlark to execute files in parallel without locks.
To play more nicely with Starlark, Starkit plugins manage state with an init/reduce pattern. That means each plugin should define:
1) An initial state (created with NewModel()) 2) Make subsequent mutations to state with starkit.SetState
At the end of execution, Starkit will return a data model with the accumulated state from all plugins.
See: https://github.com/google/starlark-go/blob/master/doc/spec.md#identity-and-mutation