Go types for the OpenCLI spec.
This project implements a subset of a variation of the OpenCLI specification. The current version is 0.1-block.1, which differs from the upstream spec in two ways:
- The Document object extends the Command object — see spectreconsole/open-cli#70
- Command supports the field
defaultCommand— see spectreconsole/open-cli#77
go get github.com/block/opencli-goimport "github.com/block/opencli-go"Document.MarshalJSON always writes the current version constant:
doc := opencli.Document{
Info: opencli.CliInfo{Version: "1.0.0"},
Command: opencli.Command{
Name: "myapp",
Commands: []opencli.Command{
{Name: "serve"},
},
},
}
data, _ := json.Marshal(doc)
// {"opencli":"0.1-block.1","info":{"version":"1.0.0"},"name":"myapp",...}Document.UnmarshalJSON validates the "opencli" field and rejects missing or unrecognized versions:
var doc opencli.Document
err := json.Unmarshal(data, &doc)
// Returns an error if "opencli" is missing or unsupportedThis module can unmarshal any OpenCLI version it supports and always marshals to the latest version (opencli.Version).
When a future version of this module introduces breaking type changes, it will use Go's semantic import versioning (e.g. github.com/block/opencli-go/v2). A program can import multiple major versions simultaneously:
import (
opencli "github.com/block/opencli-go"
opencliv2 "github.com/block/opencli-go/v2"
)Types are code-generated from JSON Schema files in schema/. To add support for a new version:
- Add the schema file (e.g.
schema/0.2-block.1.json). - Run
just generate-typesto createinternal/v0_2_block_1/types_gen.go. - Add
internal/v0_2_block_1/unmarshal.gowith any custom unmarshal logic (e.g. Arity defaults). - Add a
case "0.2-block.1":branch toDocument.UnmarshalJSONinopencli.go. - If this becomes the new current version, update the
Versionconstant and re-point the public type aliases to the new internal package.
This project uses Hermit for tool management. Activate the environment:
. ./bin/activate-hermitGenerate types from schemas:
just generate-typesRun tests:
go test ./...| Resource | Description |
|---|---|
| CODEOWNERS | Outlines the project lead(s) |
| GOVERNANCE.md | Project governance |
| LICENSE | Apache License, Version 2.0 |