fix: reject extra args and harden sshrm error handling
This commit is contained in:
@@ -11,7 +11,7 @@ description: SSHRM project conventions
|
||||
- Prefer minimal, focused changes that do not alter the intent of the existing scripts, unless the script behavior is intentionally updated.
|
||||
- Maintain `.continue/rules/project.md` whenever project conventions or script behavior change.
|
||||
- `sshrm` is implemented as a small Bash script with helper functions, while preserving host and line-number removal behavior.
|
||||
- `sshrm` should print a short usage line, support `-h`/`--help`, fail clearly on missing or invalid line-number input, and may support a dedicated `--no-backup` option with a `known_hosts.sshrm.bak` backup file.
|
||||
- `sshrm` should print a short usage line, support `-h`/`--help`, fail clearly on missing or invalid line-number input, reject extra arguments, and may support a dedicated `--no-backup` option with a `known_hosts.sshrm.bak` backup file.
|
||||
|
||||
# Project identity
|
||||
- Main script: `sshrm`
|
||||
|
||||
@@ -29,6 +29,7 @@ sshrm 12
|
||||
## Notes
|
||||
|
||||
- Line numbers must be positive integers.
|
||||
- Extra arguments are rejected.
|
||||
- The script edits `~/.ssh/known_hosts`.
|
||||
- Keep a backup if you need to recover entries.
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -u
|
||||
set -euo pipefail
|
||||
|
||||
readonly FILENAME="${HOME}/.ssh/known_hosts"
|
||||
readonly BACKUP_FILE="${FILENAME}.sshrm.bak"
|
||||
@@ -62,9 +62,19 @@ if [ "${1-}" = "" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "${2-}" != "" ]; then
|
||||
error "too many arguments."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
check_known_hosts
|
||||
|
||||
if is_number "$1"; then
|
||||
if [ "$1" -eq 0 ]; then
|
||||
error "invalid line number."
|
||||
exit 1
|
||||
fi
|
||||
HOST="$(get_host_from_line "$1")"
|
||||
if [ "${HOST}" = "" ]; then
|
||||
error "invalid line number."
|
||||
@@ -78,4 +88,7 @@ if [ "${backup}" = true ]; then
|
||||
create_backup
|
||||
fi
|
||||
|
||||
remove_host "$HOST"
|
||||
if ! remove_host "$HOST"; then
|
||||
error "removal failed."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user