fix: Support for absolute paths in rp

This was not a very functional way of removing trailing slashes.

Fixes: #3
This commit is contained in:
Karolin Varner
2023-02-23 22:47:48 +01:00
parent 22c238764a
commit 55e4fc7e9a

47
rp
View File

@@ -123,7 +123,7 @@ fatal() {
genkey() { genkey() {
usagestack+=("PRIVATE_KEYS_DIR") usagestack+=("PRIVATE_KEYS_DIR")
local skdir local skdir
skdir="${1/\//}"; shift || fatal "Required positional argument: PRIVATE_KEYS_DIR" skdir="${1%/}"; shift || fatal "Required positional argument: PRIVATE_KEYS_DIR"
while (( $# > 0 )); do while (( $# > 0 )); do
local arg; arg="$1"; shift local arg; arg="$1"; shift
@@ -149,8 +149,8 @@ genkey() {
pubkey() { pubkey() {
usagestack+=("PRIVATE_KEYS_DIR" "PUBLIC_KEYS_DIR") usagestack+=("PRIVATE_KEYS_DIR" "PUBLIC_KEYS_DIR")
local skdir pkdir local skdir pkdir
skdir="${1/\//}"; shift || fatal "Required positional argument: PRIVATE_KEYS_DIR" skdir="${1%/}"; shift || fatal "Required positional argument: PRIVATE_KEYS_DIR"
pkdir="${1/\//}"; shift || fatal "Required positional argument: PUBLIC_KEYS_DIR" pkdir="${1%/}"; shift || fatal "Required positional argument: PUBLIC_KEYS_DIR"
while (( $# > 0 )); do while (( $# > 0 )); do
local arg; arg="$1"; shift local arg; arg="$1"; shift
@@ -174,7 +174,7 @@ exchange() {
usagestack+=("PRIVATE_KEYS_DIR" "[dev <device>]" "[listen <ip>:<port>]" "[peer PUBLIC_KEYS_DIR [endpoint <ip>:<port>] [persistent-keepalive <interval>] [allowed-ips <ip1>/<cidr1>[,<ip2>/<cidr2>]...]]...") usagestack+=("PRIVATE_KEYS_DIR" "[dev <device>]" "[listen <ip>:<port>]" "[peer PUBLIC_KEYS_DIR [endpoint <ip>:<port>] [persistent-keepalive <interval>] [allowed-ips <ip1>/<cidr1>[,<ip2>/<cidr2>]...]]...")
local skdir dev lport local skdir dev lport
dev="${project_name}0" dev="${project_name}0"
skdir="${1/\//}"; shift || fatal "Required positional argument: PRIVATE_KEYS_DIR" skdir="${1%/}"; shift || fatal "Required positional argument: PRIVATE_KEYS_DIR"
while (( $# > 0 )); do while (( $# > 0 )); do
local arg; arg="$1"; shift local arg; arg="$1"; shift
@@ -238,7 +238,7 @@ exchange() {
shift; # Skip "peer" argument shift; # Skip "peer" argument
local peerdir ip port keepalive allowedips local peerdir ip port keepalive allowedips
peerdir="${1/\//}"; shift || fatal "Required peer argument: PUBLIC_KEYS_DIR" peerdir="${1%/}"; shift || fatal "Required peer argument: PUBLIC_KEYS_DIR"
while (( $# > 0 )); do while (( $# > 0 )); do
local arg; arg="$1"; shift local arg; arg="$1"; shift
@@ -282,6 +282,29 @@ exchange() {
done done
} }
find_rosenpass_binary() {
local binary; binary=""
if [[ -n "${gitdir}" ]]; then
# If rp is run from the git repo, use the newest build artifact
binary=$(
find "${gitdir}/result/bin/${project_name}" \
"${gitdir}"/target/{release,debug}/"${project_name}" \
-printf "%T@ %p\n" 2>/dev/null \
| sort -nr \
| awk 'NR==1 { print($2) }'
)
elif [[ -n "${nixdir}" ]]; then
# If rp is run from nix, use the nix-installed rosenpass version
binary="${nixdir}/bin/${project_name}"
fi
if [[ -z "${binary}" ]]; then
binary="${project_name}"
fi
echo "${binary}"
}
main() { main() {
formatting_init formatting_init
cleanup_init cleanup_init
@@ -289,17 +312,11 @@ main() {
frag_init frag_init
project_name="rosenpass" project_name="rosenpass"
scriptdir="$(dirname "${script}")"
verbose=0 verbose=0
binary="$( scriptdir="$(dirname "${script}")"
find "result/bin/rosenpass" \ gitdir="$(git -C "${scriptdir}" rev-parse --show-toplevel 2>/dev/null)" || true
"${scriptdir}"/target/{release,debug}/"${project_name}" \ nixdir="$(readlink -f result/bin/rp | grep -Pio '^/nix/store/[^/]+(?=/bin/[^/]+)')" || true
"${PWD}/rosenpass" -printf "%T@ %p\n" 2>/dev/null \ binary="$(find_rosenpass_binary)"
| sort -nr \
| awk -v fallback="${project_name}" '
NR == 1 { print($2) }
END { if (NR == 0) print(fallback) }'
)"
# Parse command # Parse command