Fix main() panicking on os.Args[1] when invoked with no arguments (closes #29) - #38
Merged
Conversation
Closes #29. `./ussher` (no arguments) used to panic with a Go runtime "index out of range" trace because main() accessed os.Args[1] on the very first line - the `--version` short-circuit - before the args-count check ten lines further down had a chance to fire. The clean "usage: ussher <username>" message that check was supposed to produce was unreachable for the no-args case. Extracts the arg-validation into validateArgs(args []string) (string, error), which returns the single argument or a typed usage error. main() calls it as the very first statement and log.Fatal()s on error, so bare invocation now prints "usage: ussher <username>" and exits 1 cleanly. The downstream args-count check is removed (now dead code). The --version short-circuit, security gates, initLog, and isValidUser sequencing are unchanged. The extraction is also a useful seam for the broader main() refactor proposed in #13 - parseArgs there would build on validateArgs here. Tests in a new ussher_test.go cover six cases: bare program name, empty args slice, single-arg-as-username, single-arg-as-version, too-many-args (with and without --version). Coverage 42.1% -> 42.7%. End-to-end repro: `./ussher` now prints "2026/04/27 20:11:26 usage: ussher <username>" to stderr and exits 1 instead of crashing. CHANGELOG bullet under [Unreleased] / Fixed. If the maintainer wants this fix in the (yet-untagged) v1.1.0, the bullet can be moved into [1.1.0] before the tag push; otherwise it ships in 1.1.1 / 1.2.0.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #29.
Summary
./ussher(no arguments) used to panic withruntime error: index out of range [1] with length 1becausemain()accessedos.Args[1]on its very first line — the--versionshort-circuit — before the args-count check ten lines further down. The intendedusage: ussher <username>message was unreachable for the no-args case.Fix
Extract
validateArgs(args []string) (string, error). It enforceslen(args) == 2and returns either the single arg (the username, or--version) or a typed usage error.main()calls it as its very first statement andlog.Fatals on error.The downstream
len(os.Args) != 2check is now dead code and removed. The--versionshort-circuit, the security gates,initLog, andisValidUsersequencing are unchanged. This extraction is also a useful seam for the broadermain()refactor proposed in #13 — that issue'sparseArgswould build on thisvalidateArgs.Tests
New
ussher_test.gowithTestValidateArgscovering six cases:--version→ returns--version--version) → errorCoverage 42.1% → 42.7%; tests pass under
-race.End-to-end repro
(Pre-fix: stack trace + exit 2.)
Releasing-this-thought
v1.1.0is queued inCHANGELOG.mdbut not yet tagged. The new Fixed bullet lives under[Unreleased]for safety — if you want this fix inv1.1.0, move the bullet into[1.1.0]before pushing the tag; otherwise it ships in1.1.1/1.2.0. Either is fine.Test plan
./build.shgreen; coverage 42.7%.go test -race ./...green../ussher(no args) printsusage: ussher <username>and exits 1 (no panic)../ussher --versionstill prints version info../ussher root(or any valid user) still proceeds normally../ussher alice extrastill rejected as wrong-arg-count.https://claude.ai/code/session_013HnepY8MhhxrJJjE5ysW47
Generated by Claude Code