From dc059db6db67abad249fa153593a89a45fa37486 Mon Sep 17 00:00:00 2001 From: srdusr <99972264+srdusr@users.noreply.github.com> Date: Mon, 25 May 2026 23:13:00 +0200 Subject: Version flag, Makefile, and honest cross-platform documentation Neither binary had a -version flag - a basic expectation for any CLI tool, and useful for anyone reporting a bug ("which build is this"). internal/version holds Version/Commit/Date, set via -ldflags "-X mitmux/internal/version.X=..." at build time and defaulting to "dev" for a plain `go build` with no ldflags, so -version is never blank or misleading about whether a given binary is a tagged release or a local build. Both mitmux and mitmuxd gained a -version flag that prints it and exits. Makefile: `make build` (both binaries for the current platform, version info from `git describe`), `make test` (the same build/vet/gofmt/test checks expected before every commit here), `make install` (a thin wrapper over `go install`, respecting GOBIN/GOPATH as usual - not reimplementing Go's own path resolution), `make release` (cross-compiles both binaries for linux/darwin/windows/freebsd, amd64+arm64 where it makes sense, into dist/). Every target is CGO_ENABLED=0: modernc.org/sqlite is pure Go, so no C toolchain is needed anywhere, cross-compiling included - this was already true before this commit, just not verified or made easy to use. Verified live, every target actually run rather than just written: `make build` produces working binaries with version info correctly picked up from git (confirmed against a real -version invocation, both the "dev" default and an ldflags-injected release-style version string); `make test` runs clean; `make release` was run for real and produced 6 platform/arch binaries, each confirmed with `file` to be a genuinely correctly-formatted executable for its target (Mach-O for both macOS architectures, PE32+ for Windows, ELF for both Linux architectures and FreeBSD) - not just "the command exited zero." `make install`'s correctness rests on `go install` itself, Go's own well-tested mechanism; deliberately not run for real here since it writes into the real GOPATH/bin outside this repo, unprompted. README gained an honest Platforms section: Linux is what's actually been run and verified throughout this project's development; macOS, Windows, and FreeBSD cross-compile cleanly and pass go vet, and the code has nothing Linux-specific in it (CA/history storage already used Go's own cross-platform os.UserConfigDir, not a hardcoded XDG path - also fixed the README's install-directory example, which had been Linux-only text), but they haven't run on real hardware, so they're documented as "should work, not yet verified" rather than a claim this session can't actually back up. Also flagged a concrete, real gotcha: macOS's shorter Unix domain socket path limit combined with the deeper ~/Library/Application Support default control-socket location could matter for a long username, with the existing -socket flag as the workaround. go build/vet/gofmt/test/mod tidy all clean. --- internal/version/version.go | 30 ++++++++++++++++++++++++++++++ internal/version/version_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 internal/version/version.go create mode 100644 internal/version/version_test.go (limited to 'internal') diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..030bd4e --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,30 @@ +// Package version holds build-time version information for both +// binaries, set via -ldflags "-X mitmux/internal/version.Version=..." +// (see the Makefile's release target). Defaults to "dev" for a plain +// `go build` with no ldflags, so `-version` is never blank or +// misleading about whether a given binary is a tagged release or a +// local build. +package version + +var ( + Version = "dev" + Commit = "" + Date = "" +) + +// String returns a one-line version summary for component ("mitmux" or +// "mitmuxd"): "mitmux dev" for an untagged build, or +// "mitmux 1.2.3 (abc1234, 2026-01-02)" once Commit/Date are set. +func String(component string) string { + s := component + " " + Version + if Commit != "" || Date != "" { + s += " (" + s += Commit + if Commit != "" && Date != "" { + s += ", " + } + s += Date + s += ")" + } + return s +} diff --git a/internal/version/version_test.go b/internal/version/version_test.go new file mode 100644 index 0000000..d2d501b --- /dev/null +++ b/internal/version/version_test.go @@ -0,0 +1,32 @@ +package version + +import "testing" + +func TestStringDefault(t *testing.T) { + orig := Version + Version, Commit, Date = "dev", "", "" + defer func() { Version = orig }() + if got := String("mitmux"); got != "mitmux dev" { + t.Errorf("String(%q) = %q, want %q", "mitmux", got, "mitmux dev") + } +} + +func TestStringWithCommitAndDate(t *testing.T) { + origV, origC, origD := Version, Commit, Date + Version, Commit, Date = "1.2.3", "abc1234", "2026-01-02" + defer func() { Version, Commit, Date = origV, origC, origD }() + want := "mitmuxd 1.2.3 (abc1234, 2026-01-02)" + if got := String("mitmuxd"); got != want { + t.Errorf("String(%q) = %q, want %q", "mitmuxd", got, want) + } +} + +func TestStringCommitOnly(t *testing.T) { + origV, origC, origD := Version, Commit, Date + Version, Commit, Date = "1.2.3", "abc1234", "" + defer func() { Version, Commit, Date = origV, origC, origD }() + want := "mitmux 1.2.3 (abc1234)" + if got := String("mitmux"); got != want { + t.Errorf("String(%q) = %q, want %q", "mitmux", got, want) + } +} -- cgit v1.2.3