diff --git a/Makefile b/Makefile index c177793..e26285f 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ -VERSION ?= dev +VERSION ?= $(shell python3 -c "import json; print(json.load(open('.release-please-manifest.json'))['.'])" 2>/dev/null || echo dev) LDFLAGS := -s -w -X github.com/keeperhub/cli/internal/version.Version=$(VERSION) -.PHONY: build test lint clean install +.PHONY: build test lint clean install sync-version -build: +sync-version: + @cp .release-please-manifest.json internal/version/manifest.json + +build: sync-version CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o bin/kh ./cmd/kh test: @@ -15,5 +18,5 @@ lint: clean: rm -rf bin/ dist/ -install: +install: sync-version CGO_ENABLED=0 go install -ldflags="$(LDFLAGS)" ./cmd/kh diff --git a/cmd/update/update.go b/cmd/update/update.go index ab8c838..13abe41 100644 --- a/cmd/update/update.go +++ b/cmd/update/update.go @@ -92,7 +92,13 @@ func runUpdate(f *cmdutil.Factory) error { return fmt.Errorf("checking for updates: %w", err) } - if !found || latest.LessOrEqual(version.Version) { + if !found { + fmt.Fprintf(f.IOStreams.Out, "No release found.\n") + return nil + } + + // "dev" means no version was injected via ldflags — always update. + if version.Version != "dev" && latest.LessOrEqual(version.Version) { fmt.Fprintf(f.IOStreams.Out, "Already on latest version: %s\n", version.Version) return nil } diff --git a/cmd/update/update_test.go b/cmd/update/update_test.go index dd45db0..b8f5c16 100644 --- a/cmd/update/update_test.go +++ b/cmd/update/update_test.go @@ -100,7 +100,7 @@ func TestUpdateCmd_AlreadyOnLatest(t *testing.T) { } out := buf.String() - if !strings.Contains(out, "latest") { - t.Errorf("expected 'latest' in output, got: %q", out) + if !strings.Contains(out, "No release found") { + t.Errorf("expected 'No release found' in output, got: %q", out) } } diff --git a/internal/version/manifest.json b/internal/version/manifest.json new file mode 100644 index 0000000..0ee8c01 --- /dev/null +++ b/internal/version/manifest.json @@ -0,0 +1,3 @@ +{ + ".": "0.3.0" +} diff --git a/internal/version/version.go b/internal/version/version.go index b2d615c..cba0ebf 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -1,5 +1,29 @@ package version +//go:generate cp ../../.release-please-manifest.json manifest.json + +import ( + _ "embed" + "encoding/json" +) + +//go:embed manifest.json +var manifestBytes []byte + // Version is the current build version. Overridden at build time via ldflags: // go build -ldflags "-X github.com/keeperhub/cli/internal/version.Version=1.0.0" -var Version = "dev" +var Version = "" + +func init() { + if Version != "" { + return + } + var manifest map[string]string + if err := json.Unmarshal(manifestBytes, &manifest); err == nil { + if v, ok := manifest["."]; ok { + Version = v + return + } + } + Version = "dev" +} diff --git a/release-please-config.json b/release-please-config.json index 9899349..9328866 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -3,6 +3,10 @@ "release-type": "go", "changelog-path": "CHANGELOG.md", "packages": { - ".": {} + ".": { + "extra-files": [ + "internal/version/manifest.json" + ] + } } }