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>
153 lines
4.2 KiB
Go
153 lines
4.2 KiB
Go
// 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
|
|
}
|