Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions find_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func run(args []string, stderr io.Writer) int {
// Remove date/time from logging output.
log.SetFlags(0)

if len(args) == 2 && (args[1] == "-v" || args[1] == "--version") {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Document the new version flag in the README

This adds a new CLI flag, but the README was not updated in the same change. The repo instructions in AGENTS.md state: “Adding a flag or subcommand requires updating the README in the same PR,” so users reading the documented CLI surface still only see find-replace FIND REPLACE and have no discoverable mention of -v/--version.

Useful? React with 👍 / 👎.

fmt.Fprintln(stderr, versionString())
return 0
}

if len(args) != 3 {
fmt.Fprintln(stderr, "Usage: find-replace FIND REPLACE")
return 1
Expand Down
29 changes: 29 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import "fmt"

// Build metadata injected by build.sh via -ldflags -X.
var (
GitTag string
GitCommit string
GoVersion string
BuildTimestamp string
BuildOS string
BuildArch string
BuildTainted string
)

func versionString() string {
tag := GitTag
if tag == "" {
tag = "dev"
}
commit := GitCommit
if commit == "" {
commit = "unknown"
}
return fmt.Sprintf(
"find-replace %s (%s) go=%s built=%s os=%s arch=%s tainted=%s",
tag, commit, GoVersion, BuildTimestamp, BuildOS, BuildArch, BuildTainted,
)
}
39 changes: 39 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"strings"
"testing"
)

func TestVersionString(t *testing.T) {
GitTag = "v1.2.3"
GitCommit = "abc1234"
GoVersion = "go1.22.0"
BuildTimestamp = "2026-01-01T00:00:00Z"
BuildOS = "linux"
BuildArch = "amd64"
BuildTainted = "false"

got := versionString()
for _, want := range []string{"v1.2.3", "abc1234", "go1.22.0", "linux", "amd64"} {
if !strings.Contains(got, want) {
t.Fatalf("versionString() = %q; want substring %q", got, want)
}
}
}

func TestRunVersionFlag(t *testing.T) {
code := run([]string{"find-replace", "--version"}, ioDiscard{t})
if code != 0 {
t.Fatalf("run --version exit = %d; want 0", code)
}
}

type ioDiscard struct{ t *testing.T }

func (d ioDiscard) Write(p []byte) (int, error) {
if !strings.Contains(string(p), "find-replace") {
d.t.Fatalf("version output = %q; want find-replace", p)
}
return len(p), nil
}