Craggy
← All repos

Craggy subsystem SDK for Go

The Go SDK for Craggy Harness subsystems (mirrored from craggy-harness sdk/go)

View on GitHub
git clone https://github.com/craggy-io/subsystem-sdk-go.git

The Go SDK for writing a Craggy Harness subsystem: the wire protocol's types, and servekit, which serves a subsystem's capabilities over it. Its source is the sdk/go folder of Craggy Harness, mirrored here so anyone can build against it:

go get github.com/craggy-io/subsystem-sdk-go

craggy-subsystem init -lang go starts a bundle that uses it. Changes are made in Craggy Harness and mirrored here; pull requests here are not merged.

Go SDK

Module github.com/craggy-io/subsystem-sdk-go. Three things:

Package What it is
subsystem (the module root) The wire types of protocol 1, held to ../spec by the conformance suite
servekit The provider kit: serve registry.Capability values as a subsystem, call back through the host, and the helpers below
cmd/example-subsystem, cmd/conformance-fixture A raw wire reference provider, and the fixture the suite drives

Calling what you require: the client is the host's to supply

A Go subsystem does not write, copy or download a client for the capabilities it lists under requires. The host generates one when it builds the subsystem, from what the providers of those capabilities describe, as the package internal/hostclient of the bundle's own Go module:

import "<your module>/internal/hostclient"

out, err := hostclient.WorkersDo(ctx, host, hostclient.WorkersDoIn{Worker: "craggy.gitleaks"})

host is the *servekit.Host: it is the hostclient.Caller. The package holds exactly what subsystem.yaml requires, so calling something that is not listed there does not compile. Names, types and the paged readers (…Pages, …All) are capability-services' clientgen's (docs/clientgen.md there). The folder is never part of the bundle: the loader leaves it out, and craggy-subsystem generate <bundle> -with <providers> writes it into your checkout for the compiler, the tests and the editor (../cli/README.md; the rules are in docs/plan.md, 2.2).

The Python SDK needs none of this: call.host(capability, input) takes and returns plain values.

Helpers over the client

Generation gives shapes. What is behaviour, and is not about any one subsystem, is here:

Helper What it does
servekit.NewSettings(host, keys...), .Keep(d), .Read(ctx) Reads the subsystem's own settings with core.settings.values@1 (list it under requires), in one call, kept for d when asked. Values.String, Int, IntIn and Bool read one with a default or a range
servekit.ReadPages(pages, data, limit) Joins the bytes of any generated …Pages iterator, never more than limit
servekit.PagesReader(pages, data) The same as a streaming io.ReadCloser: a page is asked for when the one before is read
(*Host).Read(ctx, ref) Reads a reference's whole content through its owner, checking its digest
data, err := servekit.ReadPages(
	hostclient.ArtifactsContentPages(ctx, host, hostclient.ArtifactsContentIn{ArtifactId: id, Limit: 65536}),
	func(p hostclient.ArtifactsContentOut) []byte { return p.Data }, 64<<20)

Nothing here names a subsystem's capability: a helper that is about one provider's contract (recording a run in an artifact store, running a worker and taking its report) is a few lines over the generated functions, in the bundle that needs it.

Testing code that calls through the host

servekit.Calls is the one method both the helpers and a generated client call through. A fake gets the ref and the input as the generated struct:

type fakeHost struct{}

func (fakeHost) Call(_ context.Context, ref string, in any) (json.RawMessage, error) {
	switch in := in.(type) {
	case hostclient.WorkersDoIn:
		return json.Marshal(hostclient.WorkersDoOut{Run: "run-1", Worker: in.Worker})
	}
	return nil, registry.Errorf(registry.NotFound, "%s", ref)
}