refactor code and add fallback prompt to zsh

This commit is contained in:
2026-02-13 10:19:45 +01:00
parent 1cdc70eb70
commit 25a4a6076c
19 changed files with 150 additions and 189 deletions

View File

@@ -1,13 +1,9 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
readonly SCRIPT_NAME="${0##*/}"
readonly SCRIPT_NAME=${0##*/}
check_deps() {
command -v "curl" >/dev/null 2>&1 || die "curl: command not found"
command -v curl >/dev/null 2>&1 || die "curl: command not found"
}
usage() {
@@ -26,18 +22,18 @@ EOF
}
shell_quote() {
local -r string="$1"
local -r string=$1
printf "'%s'" "${string//'/'\\''}"
}
die() {
local -r message="$1"
local -r message=$1
printf "%s: %b\n" "${SCRIPT_NAME}" "${message}" >&2
exit 1
}
invalid_option() {
local -r option="$1"
local -r option=$1
die "invalid option $(shell_quote "${option}")\nTry '${SCRIPT_NAME} --help' for usage."
}
@@ -48,11 +44,11 @@ parse_args() {
-h|--help) usage; exit 0 ;;
--family)
( (( $# < 2 )) || [[ -z "$2" ]] ) && die "--family requires an argument (auto, 4 or 6)"
family="$2"
family=$2
shift 2
;;
--family=*)
family="${1#*=}"
family=${1#*=}
[[ -z "${family}" ]] && die "--family requires an argument (auto, 4 or 6)"
shift
;;
@@ -63,7 +59,7 @@ parse_args() {
}
validate_args() {
case "${family}" in
case ${family} in
auto|4|6) ;;
*) die "invalid value for family: '${family}' (expected auto, 4 or 6)"
esac
@@ -71,9 +67,9 @@ validate_args() {
show_ip() {
local -a curl_args=()
[[ ${family} == "4" ]] && curl_args=( "-4" )
[[ ${family} == "6" ]] && curl_args=( "-6" )
curl "${curl_args[@]}" "zx2c4.com/ip"
[[ ${family} == "4" ]] && curl_args=("-4")
[[ ${family} == "6" ]] && curl_args=("-6")
curl "${curl_args[@]}" zx2c4.com/ip
}
main() {