configcmd and version: the config and version standard commands; darwin /var/run
core/configcmd: "<program> config create|validate|upgrade|path" from a Spec (name, application, minimal/full templates, Validate, Schema), taken over from yamase-core/pkg/configcmd and generalized: Filename, Optional (programs that run on defaults) and None (no configuration). core/version: "<program> version [--json]" over the values each program stamps into its internal/version; JSON keys match yasta's existing output (yavebu reads it), VCS fallback for unstamped builds. paths: macOS CLI/Service system-scope RuntimeDir is /var/run/<app> instead of /run/<app>, which macOS does not have. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ca0f06f978
commit
90132e2d76
13 files changed
+1824
-20
No files matched your search
@@ -0,0 +1,152 @@
|
||||
// Package version is the shared "<program> version [--json]" command of
|
||||
// the Yetano program standard (docs/program-conventions.md in the yetano
|
||||
// meta-repository).
|
||||
//
|
||||
// The values themselves stay in each program's internal/version package,
|
||||
// stamped at link time with -ldflags -X (see AGENTS.md "Versioning": the
|
||||
// version is "git describe --tags --always --dirty" without the tag's
|
||||
// "v"); the program hands them over:
|
||||
//
|
||||
// case "version":
|
||||
// os.Exit(coreversion.Info{
|
||||
// Program: "yasl",
|
||||
// Version: version.Version,
|
||||
// Commit: version.Commit,
|
||||
// BuildDate: version.BuildDate,
|
||||
// }.Run(args[1:], os.Stdout, os.Stderr))
|
||||
//
|
||||
// Output:
|
||||
//
|
||||
// yasl version 0.2.0
|
||||
// commit: abc1234
|
||||
// built: 2026-10-23T10:00:00Z
|
||||
// go: go1.26.3 darwin/arm64
|
||||
//
|
||||
// and with --json one object (contracts only for Verse programs, see
|
||||
// ADR-0012):
|
||||
//
|
||||
// {"program":"yasl","version":"0.2.0","commit":"abc1234",
|
||||
// "build_date":"2026-10-23T10:00:00Z","go":"go1.26.3","platform":"darwin/arm64"}
|
||||
package version
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Exit codes of Run, matching core/doctor's.
|
||||
const (
|
||||
ExitOK = 0
|
||||
ExitUsage = 2
|
||||
)
|
||||
|
||||
// Info is one program's build metadata. Empty or "unknown" Commit and
|
||||
// BuildDate fall back to the VCS data Go embeds (go build in a git
|
||||
// checkout, go install), so even an unstamped build says what it is.
|
||||
type Info struct {
|
||||
Program string `json:"program"`
|
||||
Version string `json:"version"`
|
||||
Commit string `json:"commit"`
|
||||
BuildDate string `json:"build_date"`
|
||||
Go string `json:"go"`
|
||||
Platform string `json:"platform"`
|
||||
// Contracts lists the wire contract majors a Verse program speaks,
|
||||
// by contract name (ADR-0012 §4, verse compatibility.md §4).
|
||||
Contracts map[string][]int `json:"contracts,omitempty"`
|
||||
}
|
||||
|
||||
// readBuildInfo is replaced in tests.
|
||||
var readBuildInfo = debug.ReadBuildInfo
|
||||
|
||||
// Complete fills in what the program did not stamp: Go version,
|
||||
// platform, the VCS revision and time, and "dev" for an empty Version.
|
||||
// A leading "v" on a stamped tag version is dropped (only git tags
|
||||
// carry one).
|
||||
func (i Info) Complete() Info {
|
||||
if i.Go == "" {
|
||||
i.Go = runtime.Version()
|
||||
}
|
||||
if i.Platform == "" {
|
||||
i.Platform = runtime.GOOS + "/" + runtime.GOARCH
|
||||
}
|
||||
if len(i.Version) > 1 && i.Version[0] == 'v' && i.Version[1] >= '0' && i.Version[1] <= '9' {
|
||||
i.Version = i.Version[1:]
|
||||
}
|
||||
if i.Version == "" {
|
||||
i.Version = "dev"
|
||||
}
|
||||
unknown := func(s string) bool { return s == "" || s == "unknown" }
|
||||
if unknown(i.Commit) || unknown(i.BuildDate) {
|
||||
if bi, ok := readBuildInfo(); ok {
|
||||
var rev, at, modified string
|
||||
for _, s := range bi.Settings {
|
||||
switch s.Key {
|
||||
case "vcs.revision":
|
||||
rev = s.Value
|
||||
case "vcs.time":
|
||||
at = s.Value
|
||||
case "vcs.modified":
|
||||
modified = s.Value
|
||||
}
|
||||
}
|
||||
if unknown(i.Commit) && rev != "" {
|
||||
i.Commit = rev[:min(len(rev), 7)]
|
||||
if modified == "true" {
|
||||
i.Commit += "-dirty"
|
||||
}
|
||||
}
|
||||
if unknown(i.BuildDate) && at != "" {
|
||||
i.BuildDate = at
|
||||
}
|
||||
}
|
||||
}
|
||||
if i.Commit == "" {
|
||||
i.Commit = "unknown"
|
||||
}
|
||||
if i.BuildDate == "" {
|
||||
i.BuildDate = "unknown"
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// String is the human-readable form "version" prints.
|
||||
func (i Info) String() string {
|
||||
i = i.Complete()
|
||||
return fmt.Sprintf("%s version %s\ncommit: %s\nbuilt: %s\ngo: %s %s", i.Program, i.Version, i.Commit, i.BuildDate, i.Go, i.Platform)
|
||||
}
|
||||
|
||||
// Short is the one-line form for log lines and web UI footers:
|
||||
// "0.2.0 (abc1234, built 2026-10-23T10:00:00Z)".
|
||||
func (i Info) Short() string {
|
||||
i = i.Complete()
|
||||
return fmt.Sprintf("%s (%s, built %s)", i.Version, i.Commit, i.BuildDate)
|
||||
}
|
||||
|
||||
// Run serves "<program> version [--json]" and returns the exit code.
|
||||
func (i Info) Run(args []string, stdout, stderr io.Writer) int {
|
||||
asJSON := false
|
||||
for _, a := range args {
|
||||
switch a {
|
||||
case "--json", "-json":
|
||||
asJSON = true
|
||||
case "-h", "--help", "-help", "help":
|
||||
fmt.Fprintf(stdout, "usage: %s version [--json]\n", i.Program)
|
||||
return ExitOK
|
||||
default:
|
||||
fmt.Fprintf(stderr, "%s version: unexpected argument %q\nusage: %s version [--json]\n", i.Program, a, i.Program)
|
||||
return ExitUsage
|
||||
}
|
||||
}
|
||||
if asJSON {
|
||||
enc := json.NewEncoder(stdout)
|
||||
enc.SetIndent("", " ")
|
||||
_ = enc.Encode(i.Complete())
|
||||
return ExitOK
|
||||
}
|
||||
fmt.Fprintln(stdout, strings.TrimRight(i.String(), "\n"))
|
||||
return ExitOK
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func run(t *testing.T, i Info, args ...string) (int, string, string) {
|
||||
t.Helper()
|
||||
var out, errOut bytes.Buffer
|
||||
code := i.Run(args, &out, &errOut)
|
||||
return code, out.String(), errOut.String()
|
||||
}
|
||||
|
||||
func noBuildInfo(t *testing.T) {
|
||||
t.Helper()
|
||||
old := readBuildInfo
|
||||
readBuildInfo = func() (*debug.BuildInfo, bool) { return nil, false }
|
||||
t.Cleanup(func() { readBuildInfo = old })
|
||||
}
|
||||
|
||||
func TestText(t *testing.T) {
|
||||
noBuildInfo(t)
|
||||
code, out, _ := run(t, Info{Program: "yatool", Version: "0.2.0", Commit: "abc1234", BuildDate: "2026-10-23T10:00:00Z"})
|
||||
if code != ExitOK {
|
||||
t.Fatalf("code %d", code)
|
||||
}
|
||||
for _, want := range []string{"yatool version 0.2.0\n", "commit: abc1234\n", "built: 2026-10-23T10:00:00Z\n", "go: go"} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Errorf("output %q lacks %q", out, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSON(t *testing.T) {
|
||||
noBuildInfo(t)
|
||||
i := Info{Program: "yasta", Version: "v0.11.1", Commit: "eb3675a", BuildDate: "x",
|
||||
Contracts: map[string][]int{"envelope": {1}, "session": {1, 2}}}
|
||||
code, out, _ := run(t, i, "--json")
|
||||
if code != ExitOK {
|
||||
t.Fatalf("code %d", code)
|
||||
}
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal([]byte(out), &got); err != nil {
|
||||
t.Fatalf("%v: %s", err, out)
|
||||
}
|
||||
// The keys yavebu and others already read from yasta's output.
|
||||
for _, k := range []string{"program", "version", "commit", "build_date", "go", "platform", "contracts"} {
|
||||
if _, ok := got[k]; !ok {
|
||||
t.Errorf("missing key %q in %s", k, out)
|
||||
}
|
||||
}
|
||||
if got["version"] != "0.11.1" {
|
||||
t.Errorf("version %v, want the leading v dropped", got["version"])
|
||||
}
|
||||
if code, out, _ = run(t, Info{Program: "x"}, "--json"); code != ExitOK || strings.Contains(out, "contracts") {
|
||||
t.Errorf("contracts must be omitted when empty: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultsAndBuildInfoFallback(t *testing.T) {
|
||||
old := readBuildInfo
|
||||
readBuildInfo = func() (*debug.BuildInfo, bool) {
|
||||
return &debug.BuildInfo{Settings: []debug.BuildSetting{
|
||||
{Key: "vcs.revision", Value: "0123456789abcdef"},
|
||||
{Key: "vcs.time", Value: "2026-10-01T00:00:00Z"},
|
||||
{Key: "vcs.modified", Value: "true"},
|
||||
}}, true
|
||||
}
|
||||
t.Cleanup(func() { readBuildInfo = old })
|
||||
i := Info{Program: "x", Commit: "unknown"}.Complete()
|
||||
if i.Version != "dev" || i.Commit != "0123456-dirty" || i.BuildDate != "2026-10-01T00:00:00Z" {
|
||||
t.Errorf("got %+v", i)
|
||||
}
|
||||
if v := (Info{Version: "version-x"}).Complete().Version; v != "version-x" {
|
||||
t.Errorf("a non-tag v must stay: %q", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsage(t *testing.T) {
|
||||
noBuildInfo(t)
|
||||
if code, _, errOut := run(t, Info{Program: "x"}, "--yaml"); code != ExitUsage || errOut == "" {
|
||||
t.Errorf("unknown flag: code %d, err %q", code, errOut)
|
||||
}
|
||||
if code, out, _ := run(t, Info{Program: "x"}, "--help"); code != ExitOK || !strings.Contains(out, "usage") {
|
||||
t.Errorf("--help: code %d, out %q", code, out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShort(t *testing.T) {
|
||||
noBuildInfo(t)
|
||||
if s := (Info{Version: "0.1.0", Commit: "c", BuildDate: "d"}).Short(); s != "0.1.0 (c, built d)" {
|
||||
t.Errorf("Short %q", s)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user