43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package contracts
|
|
|
|
import (
|
|
"embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"path"
|
|
)
|
|
|
|
// Files contains the pinned upstream history and only the active local contract.
|
|
// Runtime code never reads a checkout or resolves schema refs online.
|
|
//
|
|
//go:embed upstream local/*.json local/examples
|
|
var Files embed.FS
|
|
|
|
const SourceCommit = "v1"
|
|
|
|
// ReadCurrent reads the sole active project-local contract, with no fallback
|
|
// to a historical bundle if a name is missing or invalid.
|
|
func ReadCurrent(name string) ([]byte, error) {
|
|
switch name {
|
|
case "config-read.schema.json", "task-discovery.schema.json", "mq.schema.json", "mq-topology.json", "manifest.json":
|
|
return Files.ReadFile(path.Join("local", name))
|
|
default:
|
|
return nil, fmt.Errorf("invalid current contract %q", name)
|
|
}
|
|
}
|
|
|
|
func Read(name string) ([]byte, error) {
|
|
return Files.ReadFile(path.Join("upstream", SourceCommit, name))
|
|
}
|
|
|
|
func ReadJSON(name string, dst any) error {
|
|
data, err := Read(name)
|
|
if err != nil {
|
|
return fmt.Errorf("read contract %s: %w", name, err)
|
|
}
|
|
if err := json.Unmarshal(data, dst); err != nil {
|
|
return fmt.Errorf("decode contract %s: %w", name, err)
|
|
}
|
|
return nil
|
|
}
|