Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d1f2b441c0 | |||
| 6026d7514b | |||
| c5a52e2d41 | |||
| e3f142bbb6 | |||
| 967b246f4c | |||
| 82aeed5882 | |||
| 8a0ddde506 |
@@ -0,0 +1,18 @@
|
||||
---
|
||||
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.
|
||||
- Keep user-facing messages short, clear, and in English.
|
||||
- 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, reject extra arguments, and may support a dedicated `--no-backup` option with a `known_hosts.sshrm.bak` backup file.
|
||||
|
||||
# Project identity
|
||||
- Main script: `sshrm`
|
||||
- License: GNU GPL v3
|
||||
@@ -1,2 +1,35 @@
|
||||
# sshrm
|
||||
|
||||
Remove an entry from `~/.ssh/known_hosts` by host name or by line number.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
sshrm [OPTION]... HOST|LINE
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
- `-h`, `--help` Show help and exit.
|
||||
|
||||
## Examples
|
||||
|
||||
Remove by host:
|
||||
|
||||
```bash
|
||||
sshrm example.com
|
||||
```
|
||||
|
||||
Remove by line number:
|
||||
|
||||
```bash
|
||||
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.
|
||||
|
||||
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
declare -r VERSION=${1}
|
||||
declare -r MESSAGE=${2}
|
||||
declare -r TAGBRANCH=main
|
||||
declare CURRENTBRANCH=""
|
||||
|
||||
showHelp() {
|
||||
echo git-release version
|
||||
}
|
||||
|
||||
if [ "${VERSION}" == "" ]; then
|
||||
showHelp
|
||||
echo ""
|
||||
echo "no version provided!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
CURRENTBRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
|
||||
if [ ! "${CURRENTBRANCH}" == "dev" ]; then
|
||||
echo "You are not in dev branch!"
|
||||
echo "Use dev branch to make a release!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
git checkout "${TAGBRANCH}"
|
||||
git merge "${CURRENTBRANCH}"
|
||||
git push
|
||||
git tag -a "${VERSION}" -m "${MESSAGE}"
|
||||
git push --tags
|
||||
git checkout "${CURRENTBRANCH}"
|
||||
Executable
+135
@@ -0,0 +1,135 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
declare -r TAGBRANCH=main
|
||||
declare CURRENTBRANCH=""
|
||||
declare ORIGBRANCH=""
|
||||
declare -r ARG1="${1:-}"
|
||||
declare -r ARG2="${2:-}"
|
||||
declare is_dry_run=false
|
||||
declare release_input=""
|
||||
|
||||
showHelp() {
|
||||
cat <<'EOF'
|
||||
Usage:
|
||||
makerelease.sh VERSION
|
||||
makerelease.sh +0.0.1
|
||||
makerelease.sh +0.1
|
||||
makerelease.sh +1
|
||||
makerelease.sh --dry-run VERSION
|
||||
makerelease.sh --dry-run +0.0.1
|
||||
makerelease.sh --dry-run +0.1
|
||||
makerelease.sh --dry-run +1
|
||||
|
||||
Creates an annotated Git tag from the current dev branch.
|
||||
|
||||
If VERSION starts with +, it is treated as an increment:
|
||||
- +0.0.1 increments patch
|
||||
- +0.1 increments minor
|
||||
- +1 increments major
|
||||
|
||||
Use --dry-run to show the computed release version without running Git actions.
|
||||
|
||||
Requirements:
|
||||
- run from a clean Git working tree
|
||||
- current branch must be dev for real releases
|
||||
- main branch must exist locally
|
||||
EOF
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
if [ -n "${ORIGBRANCH}" ] && [ "${CURRENTBRANCH}" != "${ORIGBRANCH}" ]; then
|
||||
git checkout "${ORIGBRANCH}" >/dev/null 2>&1 || true
|
||||
fi
|
||||
}
|
||||
|
||||
trap cleanup EXIT
|
||||
|
||||
if [ "${ARG1}" = "--dry-run" ] || [ "${ARG1}" = "-n" ]; then
|
||||
is_dry_run=true
|
||||
release_input="${ARG2:-}"
|
||||
else
|
||||
release_input="${ARG1}"
|
||||
fi
|
||||
|
||||
if [ -z "${release_input}" ]; then
|
||||
showHelp
|
||||
echo ""
|
||||
echo "no version provided!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
echo "You are not inside a Git repository!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -n "$(git status --porcelain)" ]; then
|
||||
echo "Working tree is not clean!"
|
||||
echo "Commit or stash your changes before creating a release."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
CURRENTBRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
ORIGBRANCH="${CURRENTBRANCH}"
|
||||
|
||||
if [ "${is_dry_run}" = false ] && [ "${CURRENTBRANCH}" != "dev" ]; then
|
||||
echo "You are not in dev branch!"
|
||||
echo "Use dev branch to make a release!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
release_tag="${release_input}"
|
||||
if [ "${release_input}" != "${release_input#+}" ]; then
|
||||
current_tag="$(git describe --tags --abbrev=0 2>/dev/null || true)"
|
||||
if [ -z "${current_tag}" ]; then
|
||||
echo "No existing tag found to increment from!"
|
||||
exit 1
|
||||
fi
|
||||
case "${release_input}" in
|
||||
+1)
|
||||
release_tag="$(printf '%s' "${current_tag}" | awk -F. 'BEGIN{OFS="."} { $1+=1; $2=0; $3=0; print }')"
|
||||
;;
|
||||
+0.1)
|
||||
release_tag="$(printf '%s' "${current_tag}" | awk -F. 'BEGIN{OFS="."} { $2+=1; $3=0; print }')"
|
||||
;;
|
||||
+0.0.1)
|
||||
release_tag="$(printf '%s' "${current_tag}" | awk -F. 'BEGIN{OFS="."} { $3+=1; print }')"
|
||||
;;
|
||||
*)
|
||||
echo "Unsupported increment syntax: ${release_input}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
if git rev-parse -q --verify "refs/tags/${release_tag}" >/dev/null; then
|
||||
echo "Tag ${release_tag} already exists!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "${is_dry_run}" = true ]; then
|
||||
echo "Dry run: computed release tag ${release_tag}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "Release tag selected: ${release_tag}"
|
||||
read -r -p "Proceed with release? [y/N] " confirm
|
||||
case "${confirm}" in
|
||||
y|Y)
|
||||
;;
|
||||
*)
|
||||
echo "Release cancelled."
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
git checkout "${TAGBRANCH}"
|
||||
CURRENTBRANCH="${TAGBRANCH}"
|
||||
git merge "${ORIGBRANCH}"
|
||||
git push
|
||||
git tag -a "${release_tag}" -m "Release ${release_tag}"
|
||||
git push --tags
|
||||
|
||||
echo "Created release tag ${release_tag}"
|
||||
@@ -1,15 +1,94 @@
|
||||
#!/bin/bash
|
||||
|
||||
declare -r FILENAME="${HOME}/.ssh/known_hosts"
|
||||
declare HOST=""
|
||||
set -euo pipefail
|
||||
|
||||
if [ ! "${1}" == "" ]; then
|
||||
case "${1}" in
|
||||
*[!0-9]*) HOST=${1} ;;
|
||||
*) HOST=$(sed -n -e ${1}p "${FILENAME}" | awk '{print $1}') ;;
|
||||
esac
|
||||
ssh-keygen -R "${HOST}"
|
||||
else
|
||||
echo "Error: No host or line number provided !"
|
||||
echo "sshrm host|line_number"
|
||||
readonly FILENAME="${HOME}/.ssh/known_hosts"
|
||||
readonly BACKUP_FILE="${FILENAME}.sshrm.bak"
|
||||
|
||||
check_known_hosts() {
|
||||
if [ ! -f "${FILENAME}" ]; then
|
||||
error "known_hosts not found."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
usage() {
|
||||
echo "Usage: sshrm [-h|--help] [--no-backup] host|line_number"
|
||||
}
|
||||
|
||||
error() {
|
||||
echo "Error: $1"
|
||||
}
|
||||
|
||||
is_number() {
|
||||
case "$1" in
|
||||
''|*[!0-9]*) return 1 ;;
|
||||
*) return 0 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
get_host_from_line() {
|
||||
awk -v line="$1" 'NR == line {print $1; exit}' "${FILENAME}"
|
||||
}
|
||||
|
||||
create_backup() {
|
||||
cp "${FILENAME}" "${BACKUP_FILE}"
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
backup=true
|
||||
if [ "$1" = "--no-backup" ]; then
|
||||
backup=false
|
||||
shift
|
||||
fi
|
||||
|
||||
if [ "${1-}" = "" ]; then
|
||||
error "missing argument."
|
||||
usage
|
||||
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."
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
HOST="$1"
|
||||
fi
|
||||
|
||||
if [ "${backup}" = true ]; then
|
||||
create_backup
|
||||
fi
|
||||
|
||||
if ! remove_host "$HOST"; then
|
||||
error "removal failed."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user