diff --git a/Makefile b/Makefile index 4340b5f..2a84fdf 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ LDFLAGS = "" # If VERSION is set, inject into binary ifdef VERSION - LDFLAGS = -ldflags "-X '$(MODULE_PATH).version=$(VERSION)'" + LDFLAGS = -ldflags "-X '$(MODULE_PATH).Version=$(VERSION)'" endif .PHONY: build install agent-dev test coverage diff --git a/cmd/core-agent/commands_test.go b/cmd/core-agent/commands_test.go index a8497c1..2075f70 100644 --- a/cmd/core-agent/commands_test.go +++ b/cmd/core-agent/commands_test.go @@ -7,6 +7,7 @@ import ( "os" "testing" + agentpkg "dappco.re/go/agent" "dappco.re/go/core" "github.com/stretchr/testify/assert" ) @@ -85,9 +86,9 @@ func TestCommands_RegisterAppCommands_Good(t *testing.T) { func TestCommands_Version_Good(t *testing.T) { c := newTestCore(t) - version = "0.8.0" + agentpkg.Version = "0.8.0" t.Cleanup(func() { - version = "" + agentpkg.Version = "" }) r := c.Cli().Run("version") @@ -96,7 +97,7 @@ func TestCommands_Version_Good(t *testing.T) { func TestCommands_VersionDev_Bad(t *testing.T) { c := newTestCore(t) - version = "" + agentpkg.Version = "" c.App().Version = "dev" r := c.Cli().Run("version") diff --git a/cmd/core-agent/main.go b/cmd/core-agent/main.go index d3bb324..f1b6638 100644 --- a/cmd/core-agent/main.go +++ b/cmd/core-agent/main.go @@ -6,6 +6,7 @@ import ( "context" "syscall" + agentpkg "dappco.re/go/agent" "dappco.re/go/core" "dappco.re/go/agent/pkg/agentic" @@ -49,11 +50,11 @@ func newCoreAgent() *core.Core { // appVersion resolves the build version injected at link time. // -// version = "0.15.0" +// agentpkg.Version = "0.15.0" // appVersion() // "0.15.0" func appVersion() string { - if version != "" { - return version + if agentpkg.Version != "" { + return agentpkg.Version } return "dev" } diff --git a/cmd/core-agent/main_example_test.go b/cmd/core-agent/main_example_test.go index 6c2d9d9..72a7f41 100644 --- a/cmd/core-agent/main_example_test.go +++ b/cmd/core-agent/main_example_test.go @@ -2,12 +2,15 @@ package main -import core "dappco.re/go/core" +import ( + agentpkg "dappco.re/go/agent" + core "dappco.re/go/core" +) func Example_newCoreAgent() { - oldVersion := version - version = "0.15.0" - defer func() { version = oldVersion }() + oldVersion := agentpkg.Version + agentpkg.Version = "0.15.0" + defer func() { agentpkg.Version = oldVersion }() c := newCoreAgent() core.Println(c.App().Name) @@ -20,9 +23,9 @@ func Example_newCoreAgent() { } func Example_appVersion() { - oldVersion := version - version = "0.15.0" - defer func() { version = oldVersion }() + oldVersion := agentpkg.Version + agentpkg.Version = "0.15.0" + defer func() { agentpkg.Version = oldVersion }() core.Println(appVersion()) // Output: 0.15.0 diff --git a/cmd/core-agent/main_test.go b/cmd/core-agent/main_test.go index 7900f5c..9e400c6 100644 --- a/cmd/core-agent/main_test.go +++ b/cmd/core-agent/main_test.go @@ -5,6 +5,7 @@ package main import ( "testing" + agentpkg "dappco.re/go/agent" "dappco.re/go/agent/pkg/agentic" "dappco.re/go/agent/pkg/brain" "dappco.re/go/agent/pkg/monitor" @@ -16,9 +17,9 @@ import ( func withVersion(t *testing.T, value string) { t.Helper() - oldVersion := version - version = value - t.Cleanup(func() { version = oldVersion }) + oldVersion := agentpkg.Version + agentpkg.Version = value + t.Cleanup(func() { agentpkg.Version = oldVersion }) } func TestMain_NewCoreAgent_Good(t *testing.T) { diff --git a/cmd/core-agent/update.go b/cmd/core-agent/update.go index 68890d4..33a9655 100644 --- a/cmd/core-agent/update.go +++ b/cmd/core-agent/update.go @@ -2,26 +2,19 @@ package main -// version is set at build time via ldflags: -// -// go build -ldflags "-X 'dappco.re/go/agent.version=0.15.0'" ./cmd/core-agent/ -var version string +import agentpkg "dappco.re/go/agent" // updateChannel maps the build version to the release channel. // -// version = "0.15.0" +// agentpkg.Version = "0.15.0" // updateChannel() // "stable" func updateChannel() string { switch { - case version == "" || version == "dev": + case agentpkg.Version == "" || agentpkg.Version == "dev": return "dev" - case len(version) > 0 && (version[len(version)-1] >= 'a'): + case len(agentpkg.Version) > 0 && (agentpkg.Version[len(agentpkg.Version)-1] >= 'a'): return "prerelease" default: return "stable" } } - -// TODO: wire go-update UpdateService for self-update command -// Channels: stable → GitHub releases, prerelease → GitHub dev, dev → Forge main -// Parked until version var moves to module root package (dappco.re/go/agent.Version) diff --git a/cmd/core-agent/update_example_test.go b/cmd/core-agent/update_example_test.go index 1e99b38..8062a1b 100644 --- a/cmd/core-agent/update_example_test.go +++ b/cmd/core-agent/update_example_test.go @@ -2,12 +2,15 @@ package main -import core "dappco.re/go/core" +import ( + agentpkg "dappco.re/go/agent" + core "dappco.re/go/core" +) func Example_updateChannel() { - oldVersion := version - version = "0.15.0-alpha" - defer func() { version = oldVersion }() + oldVersion := agentpkg.Version + agentpkg.Version = "0.15.0-alpha" + defer func() { agentpkg.Version = oldVersion }() core.Println(updateChannel()) // Output: prerelease diff --git a/cmd/core-agent/update_test.go b/cmd/core-agent/update_test.go index 3200da8..36fe48f 100644 --- a/cmd/core-agent/update_test.go +++ b/cmd/core-agent/update_test.go @@ -5,56 +5,57 @@ package main import ( "testing" + agentpkg "dappco.re/go/agent" "github.com/stretchr/testify/assert" ) func TestUpdate_UpdateChannel_Good(t *testing.T) { - version = "1.0.0" + agentpkg.Version = "1.0.0" t.Cleanup(func() { - version = "" + agentpkg.Version = "" }) assert.Equal(t, "stable", updateChannel()) } func TestUpdate_UpdateChannelDev_Good(t *testing.T) { - version = "dev" + agentpkg.Version = "dev" t.Cleanup(func() { - version = "" + agentpkg.Version = "" }) assert.Equal(t, "dev", updateChannel()) } func TestUpdate_UpdateChannelEmpty_Bad(t *testing.T) { - version = "" + agentpkg.Version = "" assert.Equal(t, "dev", updateChannel()) } func TestUpdate_UpdateChannelPrerelease_Ugly(t *testing.T) { - version = "0.8.0-alpha" + agentpkg.Version = "0.8.0-alpha" t.Cleanup(func() { - version = "" + agentpkg.Version = "" }) assert.Equal(t, "prerelease", updateChannel()) } func TestUpdate_UpdateChannelNumericSuffix_Ugly(t *testing.T) { - version = "0.8.0-beta.1" + agentpkg.Version = "0.8.0-beta.1" t.Cleanup(func() { - version = "" + agentpkg.Version = "" }) // Ends in '1' which is < 'a', so stable assert.Equal(t, "stable", updateChannel()) } func TestUpdate_AppVersion_Good(t *testing.T) { - version = "1.2.3" + agentpkg.Version = "1.2.3" t.Cleanup(func() { - version = "" + agentpkg.Version = "" }) assert.Equal(t, "1.2.3", appVersion()) } func TestUpdate_AppVersion_Bad(t *testing.T) { - version = "" + agentpkg.Version = "" assert.Equal(t, "dev", appVersion()) } diff --git a/version.go b/version.go new file mode 100644 index 0000000..f81b8c3 --- /dev/null +++ b/version.go @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: EUPL-1.2 + +package agent + +// Version is injected at build time via ldflags. +// +// go build -ldflags "-X 'dappco.re/go/agent.Version=0.15.0'" ./cmd/core-agent/ +var Version string