Skip to content

Commit 211ab43

Browse files
committed
chore: fix reviews
1 parent c3db69e commit 211ab43

4 files changed

Lines changed: 25 additions & 22 deletions

File tree

cmd/anfra/main.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ type exitCodeError struct{ code int }
1616
func (e *exitCodeError) Error() string { return fmt.Sprintf("exit code %d", e.code) }
1717

1818
func main() {
19-
err := newRootCmd().Execute()
19+
// ExecuteC returns the command that actually ran, so we get the real
20+
// subcommand name (handles flags/args/aliases) rather than parsing os.Args.
21+
executed, err := newRootCmd().ExecuteC()
2022
// After the command runs, surface a cached "update available" notice (and, if
2123
// opted in, kick a background update). Best-effort; never affects exit status.
22-
maybeNotifyUpdate(invokedCommand())
24+
if executed != nil {
25+
maybeNotifyUpdate(executed.Name())
26+
}
2327

2428
if err == nil {
2529
return
@@ -32,17 +36,6 @@ func main() {
3236
os.Exit(1)
3337
}
3438

35-
// invokedCommand returns the first non-flag CLI arg (the subcommand name), or ""
36-
// for a bare `anfra`. Used to suppress the notice on update/serve commands.
37-
func invokedCommand() string {
38-
for _, a := range os.Args[1:] {
39-
if len(a) > 0 && a[0] != '-' {
40-
return a
41-
}
42-
}
43-
return ""
44-
}
45-
4639
func newRootCmd() *cobra.Command {
4740
root := &cobra.Command{
4841
Use: "anfra",

install.sh

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# ANFRA_INSTALL_DIR install location (default: $HOME/.anfra/bin)
1313
# ANFRA_VERSION pin a version, e.g. 0.1.0 (default: latest)
1414
# ANFRA_NO_MODIFY_PATH if set, don't touch shell rc files; just print the hint
15-
# GITHUB_TOKEN optional; sent as a bearer token (for pre-public access)
1615
#
17-
# Trust model: downloads over HTTPS from GitHub Releases (TOFU). Signature
16+
# Downloads from public GitHub Releases (no auth). Trust model: downloads over
17+
# HTTPS from GitHub Releases (TOFU). Signature
1818
# verification is not done here yet — `anfra update` is where verification will
1919
# live (see .agents/projects/anfra/signing.md).
2020

@@ -53,16 +53,11 @@ else
5353
url="https://github.com/${REPO}/releases/latest/download/${asset}"
5454
fi
5555

56-
auth=()
57-
if [ -n "${GITHUB_TOKEN:-}" ]; then
58-
auth=(-H "Authorization: Bearer ${GITHUB_TOKEN}")
59-
fi
60-
6156
# --- download ---
6257
tmp="$(mktemp -d)"
6358
trap 'rm -rf "$tmp"' EXIT
6459
echo "Downloading ${asset} (~250 MB) for ${os}/${arch}..."
65-
if ! curl -fL ${auth[@]+"${auth[@]}"} -o "${tmp}/${BIN_NAME}" "$url"; then
60+
if ! curl -fL -o "${tmp}/${BIN_NAME}" "$url"; then
6661
err "download failed from ${url} (is the release published for this platform?)"
6762
fi
6863

internal/update/update.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,17 @@ func (r *Release) IsNewer() bool {
154154
if cur == "dev" || cur == "" {
155155
return true
156156
}
157-
return semver.Compare("v"+r.Version, "v"+cur) > 0
157+
// Normalize to canonical vX.Y.Z so a stray "v"/"anfra-v" prefix on either side
158+
// can't produce an invalid string (e.g. "vv0.2.0"), which Compare treats as 0.
159+
return semver.Compare(canonicalVersion(r.Version), canonicalVersion(cur)) > 0
160+
}
161+
162+
// canonicalVersion strips any anfra-v / v prefix and re-adds a single "v", so
163+
// semver.Compare always sees a valid version regardless of the input format.
164+
func canonicalVersion(v string) string {
165+
v = strings.TrimPrefix(v, tagPrefix)
166+
v = strings.TrimPrefix(v, "v")
167+
return "v" + v
158168
}
159169

160170
// Apply downloads the platform asset and atomically replaces the running binary.

internal/update/update_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ func TestIsNewer(t *testing.T) {
2525
{"dev", "0.1.0", true}, // local build is always outdated
2626
{"", "0.1.0", true}, // unset behaves like dev
2727
{"1.0.0", "0.9.9", false},
28+
// Mixed prefixes must normalize, not degrade to a 0/invalid compare.
29+
{"v0.1.0", "0.2.0", true},
30+
{"0.1.0", "v0.2.0", true},
31+
{"v1.0.0", "v1.0.0", false},
32+
{"anfra-v0.1.0", "0.2.0", true},
2833
}
2934
for _, c := range cases {
3035
meta.Version = c.current

0 commit comments

Comments
 (0)