Skip to content
Closed
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
17 changes: 14 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,23 @@ jobs:

- name: Build release binary
if: steps.bump.outputs.bump != 'skip'
# Static linux/amd64 binary. CGO off so the binary has no glibc
# dependency; -trimpath + -s -w shrinks size and strips local paths.
env:
TAG: ${{ steps.tag.outputs.next }}
run: |
set -euo pipefail
COMMIT="${HEAD_SHA:0:7}"
GO_VERSION="$(go version | cut -d' ' -f3)"
BUILD_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
LD="-s -w \
-X 'main.GitTag=${TAG}' \
-X 'main.GitCommit=${COMMIT}' \
-X 'main.GoVersion=${GO_VERSION}' \
-X 'main.BuildTimestamp=${BUILD_DATE}' \
-X 'main.BuildOS=linux' \
-X 'main.BuildArch=amd64' \
-X 'main.BuildTainted=false'"
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -trimpath -ldflags="-s -w" \
go build -trimpath -ldflags="${LD}" \
-o find-replace-linux-amd64 .
ls -l find-replace-linux-amd64

Expand Down
12 changes: 11 additions & 1 deletion find_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,18 @@ 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.

P2 Badge Document the new CLI flags

AGENTS.md says any added flag or subcommand must update the README in the same PR. This block adds -v/--version (and the -h/--help block below), but README.md still documents only find-replace FIND REPLACE, so users have no documented way to discover the new supported flags and the change violates the repo's CLI-surface rule.

Useful? React with 👍 / 👎.

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

if len(args) == 2 && (args[1] == "-h" || args[1] == "--help") {
printUsage(stderr)
return 0
}

if len(args) != 3 {
fmt.Fprintln(stderr, "Usage: find-replace FIND REPLACE")
printUsage(stderr)
return 1
}

Expand Down
39 changes: 39 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"io"
)

// 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,
)
}

func printUsage(w io.Writer) {
fmt.Fprintln(w, "Usage: find-replace FIND REPLACE")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "Recursively find and replace strings in file names and contents.")
fmt.Fprintln(w, "Skips .git/ directories and binary-looking files.")
}
46 changes: 46 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"bytes"
"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) {
var stderr bytes.Buffer
code := run([]string{"find-replace", "--version"}, &stderr)
if code != 0 {
t.Fatalf("run --version exit = %d; want 0", code)
}
if !strings.Contains(stderr.String(), "find-replace") {
t.Fatalf("version output = %q; want find-replace", stderr.String())
}
}

func TestRunHelpFlag(t *testing.T) {
var stderr bytes.Buffer
code := run([]string{"find-replace", "--help"}, &stderr)
if code != 0 {
t.Fatalf("run --help exit = %d; want 0", code)
}
if !strings.Contains(stderr.String(), "Usage: find-replace") {
t.Fatalf("help output = %q; want usage line", stderr.String())
}
}