ctl

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ctl is the control socket that lets a block talk back to the run that started it: mdsh ctl on one side, the run on the other.

Each call opens a connection, sends one line of JSON, reads one line of JSON back, and closes. The server handles calls one at a time, in the order they arrive.

Index

Constants

View Source
const (
	// SockVar holds the socket's path.
	SockVar = "MDSH_SOCK"
	// TokenVar holds the token for this run of this block.
	TokenVar = "MDSH_TOKEN"
)

The variables mdsh exports in every block's prelude.

View Source
const (
	// OK is done, or confirm was answered yes.
	OK = 0
	// No is confirm answered no.
	No = 1
	// Refused is bad arguments, an unknown task, a token that has
	// stopped working, or a name that an input binds.
	Refused = 2
	// NotRunning is $MDSH_SOCK unset, or nothing listening on it.
	NotRunning = 3
	// NoAnswer is ask or confirm with nobody to answer and no default.
	NoAnswer = 4
)

Exit codes, the same for every command.

View Source
const MaxRequest = 1 << 20

MaxRequest is the longest request line the server reads.

View Source
const Version = 1

Version is the protocol version this package speaks.

Variables

View Source
var ErrNotRunning = errors.New("not running under mdsh")

ErrNotRunning is Call's error when there is no run to call: the socket path is empty or nothing is listening on it.

Functions

This section is empty.

Types

type Handler

type Handler func(caller any, req Request) Response

Handler answers a call from caller, the value Issue was given for the call's token.

type NameArgs

type NameArgs struct {
	Name string `json:"name"`
}

NameArgs are the arguments to unset and get.

type Request

type Request struct {
	V     int             `json:"v"`
	Token string          `json:"token"`
	Cmd   string          `json:"cmd"`
	Args  json.RawMessage `json:"args,omitempty"`
}

Request is one call.

type Response

type Response struct {
	V     int    `json:"v"`
	OK    bool   `json:"ok"`
	Value string `json:"value,omitempty"`
	Error string `json:"error,omitempty"`
	Code  int    `json:"code,omitempty"`
}

Response is the answer to one call. Value is what the command prints, such as the value get read.

func Call

func Call(path, token, cmd string, args any) (Response, error)

Call sends cmd with args to the socket at path, as the block that has token, and returns the answer. It waits as long as the run takes to answer, which for a question can be a while.

func Done

func Done(value string) Response

Done is a response for a call that succeeded and prints value.

func Refuse

func Refuse(format string, a ...any) Response

Refuse is a response for a call that was refused, with code Refused.

type Server

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

Server listens on the control socket.

func Listen

func Listen(h Handler) (*Server, error)

Listen creates the socket in a new temp directory that only the current user can open, and starts serving calls with h.

func (*Server) Close

func (s *Server) Close() error

Close stops serving and removes the socket and its directory.

func (*Server) Issue

func (s *Server) Issue(caller any) string

Issue makes a new token for caller, for $MDSH_TOKEN. Calls made with it reach the handler with caller until Revoke.

func (*Server) Path

func (s *Server) Path() string

Path is the socket's path, for $MDSH_SOCK.

func (*Server) Revoke

func (s *Server) Revoke(token string)

Revoke stops token working.

type SetArgs

type SetArgs struct {
	Name   string `json:"name"`
	Value  string `json:"value"`
	Secret bool   `json:"secret,omitempty"`
}

SetArgs are the arguments to set.

type StatusArgs

type StatusArgs struct {
	Text string `json:"text"`
	Step int    `json:"step,omitempty"`
	Of   int    `json:"of,omitempty"`
}

StatusArgs are the arguments to status. Step and Of are 0 when the call gave no count.

Jump to

Keyboard shortcuts

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