diff --git a/.continue/rules/project.md b/.continue/rules/project.md new file mode 100644 index 0000000..88eb337 --- /dev/null +++ b/.continue/rules/project.md @@ -0,0 +1,20 @@ +--- +description: SSHRM project conventions +--- + +# Project conventions +- Use English throughout the project. +- Keep shell scripts Bash-based when Bash is already used by the project. +- Preserve the current behavior of the main scripts: + - `sshrm`: remove an entry from `~/.ssh/known_hosts` by host name or by line number. + - `git-release`: handle releases from `dev` to `main`, create an annotated tag, and push commits and tags. +- Keep user-facing messages short, clear, and in English. +- Prefer minimal, focused changes that do not alter the intent of the existing scripts. +- 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`, and fail clearly on missing or invalid line-number input. + +# Project identity +- Main script: `sshrm` +- Release helper: `git-release` +- License: GNU GPL v3 diff --git a/sshrm b/sshrm index 523bb8b..4f326ba 100755 --- a/sshrm +++ b/sshrm @@ -1,15 +1,60 @@ #!/bin/bash -declare -r FILENAME="${HOME}/.ssh/known_hosts" -declare HOST="" +set -u -if [ ! "${1}" == "" ]; then - case "${1}" in - *[!0-9]*) HOST=${1} ;; - *) HOST=$(sed -n -e ${1}p "${FILENAME}" | awk '{print $1}') ;; +readonly FILENAME="${HOME}/.ssh/known_hosts" + +check_known_hosts() { + if [ ! -f "${FILENAME}" ]; then + error "known_hosts not found." + exit 1 + fi +} + +usage() { + echo "Usage: sshrm [-h|--help] host|line_number" +} + +error() { + echo "Error: $1" +} + +is_number() { + case "$1" in + ''|*[!0-9]*) return 1 ;; + *) return 0 ;; esac - ssh-keygen -R "${HOST}" -else - echo "Error: No host or line number provided !" - echo "sshrm host|line_number" +} + +get_host_from_line() { + awk -v line="$1" 'NR == line {print $1; exit}' "${FILENAME}" +} + +remove_host() { + ssh-keygen -R "$1" +} + +if [ "${1-}" = "" ]; then + error "missing argument." + usage + exit 1 fi + +if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + usage + exit 0 +fi + +check_known_hosts + +if is_number "$1"; then + HOST="$(get_host_from_line "$1")" + if [ "${HOST}" = "" ]; then + error "invalid line number." + exit 1 + fi +else + HOST="$1" +fi + +remove_host "$HOST"