plug

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package plug provides the default runtime for a PlugKit-compatible plugin.

A SmartPlug is a minimal, one-shot command handler that receives a CBOR-encoded Envelope from the host via stdin, processes it using a registered handler, sends a response back to stdout, and terminates with a specific exit code.

This simple model allows for sandboxed, transactional plugin operations using structured messaging.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleSmartPlugMessage added in v0.1.0

func HandleSmartPlugMessage[In any](
	s *SmartPlug,
	messageType string,
	handler func(In) (*messages.Result, codes.PluginExitReason, error),
)

func WrapSmartPlugTypedHandler added in v0.1.0

func WrapSmartPlugTypedHandler[In any](
	fn func(In) (*messages.Result, codes.PluginExitReason, error),
) func([]byte) (*messages.Result, codes.PluginExitReason, error)

WrapSmartPlugTypedHandler adapts a strongly-typed plugin handler to the func([]byte) (*messages.Result, codes.PluginExitReason, error) form expected by SmartPlug.

It performs CBOR decoding of the input and passes the resulting value to the user-defined handler.

Types

type SmartPlug added in v0.1.0

type SmartPlug struct {
	Handlers map[string]func([]byte) (result *messages.Result, exitReason codes.PluginExitReason, e error)
	// contains filtered or unexported fields
}

SmartPlug is a most standard type of plug. It supports one-off comand. SmartPlug is the core runtime structure for a PlugKit plugin.

It supports registering handlers for specific message types and handles a single Envelope message per execution.

func New

func New() *SmartPlug

New creates a new SmartPlug instance wired to stdin and stdout.

A default "exit" message handler is registered, which allows the host to gracefully terminate the plugin if needed.

func (*SmartPlug) Finish added in v0.1.0

func (h *SmartPlug) Finish(message string, code codes.PluginExitReason)

Finish sends a PluginFinish message and terminates the plugin process.

The plugin will exit with the given PluginExitReason code.

func (*SmartPlug) HandleMessageType added in v0.1.0

func (h *SmartPlug) HandleMessageType(name string, handler func([]byte) (*messages.Result, codes.PluginExitReason, error))

HandleMessageType registers a function to handle a given message type.

The handler receives raw CBOR-encoded data from the Envelope and is responsible for decoding and processing it.

The handler must return a messages.Result (or nil), a PluginExitReason, and an error (or nil). message.Result must contain status code, and value of the response.

func (*SmartPlug) Main added in v0.1.0

func (h *SmartPlug) Main() error

Main runs the main routine of the plugin.

It waits for a single incoming message, dispatches it to the appropriate handler, sends back any response, and terminates with the declared PluginExitReason.

This function is designed for one-shot plugin invocations. It should be called from the plugin's main() function.

func (*SmartPlug) Respond added in v0.1.0

func (h *SmartPlug) Respond(r *messages.Result)

Respond sends a typed message to the host.

It wraps the payload into a CBOR-encoded Envelope and writes it to stdout. Panics if encoding fails.

Should only be used from within message handlers.

Jump to

Keyboard shortcuts

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