This commit is contained in:
Shelvacu
2025-05-23 17:08:37 -07:00
committed by Shelvacu on fw
parent e7f4fa8638
commit 6ad831f85d
4 changed files with 42 additions and 40 deletions

View File

@@ -1,6 +1,8 @@
#!/usr/bin/env bash
source shellvaculib.bash
svl_assert_probably_in_script_dir
declare -a nix_eval=(
nix eval
--show-trace

View File

@@ -12,7 +12,7 @@ in
config = lib.mkIf config.vacu.shell.containerAliases {
vacu.packages = [
(writeScriptBin "ncrun" ''
min_args $# 2
svl_min_args $# 2
if [[ $UID != 0 ]]; then
exec /run/wrappers/bin/sudo "$0" "$@"
fi
@@ -21,7 +21,7 @@ in
exec ${lib.getExe pkgs.nixos-container} run "$1" -- "$@"
'')
(writeScriptBin "ncrl" ''
exact_args $# 1
svl_exact_args $# 1
if [[ $UID != 0 ]]; then
exec /run/wrappers/bin/sudo "$0" "$@"
fi

View File

@@ -56,7 +56,7 @@ let
echo "complete -F $completion_function_name ${name}" >> $completion_file
'';
ms_text = with_sudo: ''
minmax_args $# 1 3
svl_minmax_args $# 1 3
host="$1"
session_name="''${2:-main}"
set -x
@@ -70,7 +70,7 @@ in
(script "ms" (ms_text false))
(script "mss" (ms_text true))
(script "msl" ''
exact_args $# 1
svl_exact_args $# 1
host="$1"
echo 'echo "user:"; screen -ls; echo; echo "root:"; sudo screen -ls' | ssh -T "$host"
'')
@@ -87,7 +87,7 @@ in
'')
(script "nr" ''
# nix run nixpkgs#<thing> -- <args>
min_args $# 1
svl_min_args $# 1
installable="$1"
shift
if [[ "$installable" != *'#'* ]]; then
@@ -97,7 +97,7 @@ in
'')
(script "nb" ''
# nix build nixpkgs#<thing> <args>
min_args $# 1
svl_min_args $# 1
installable="$1"
shift
if [[ "$installable" != *'#'* ]]; then
@@ -107,7 +107,7 @@ in
'')
(script "ns" ''
# nix shell nixpkgs#<thing>
min_args $# 1
svl_min_args $# 1
new_args=( )
for arg in "$@"; do
if [[ "$arg" != *'#'* ]] && [[ "$arg" != -* ]]; then
@@ -125,7 +125,7 @@ in
(simple "jcu" [journalctl "--pager-end" "-u"])
(script "list-auto-roots" ''
auto_roots="/nix/var/nix/gcroots/auto"
exact_args $# 0
svl_exact_args $# 0
echo "List of auto-added nix gcroots, excluding system profiles:"
echo
for fn in "$auto_roots/"*; do
@@ -145,7 +145,7 @@ in
];
vacu.shell.functions = {
nd = ''
min_args $# 1
svl_min_args $# 1
declare -a args=("$@")
lastarg="''${args[-1]}"
if [[ "$lastarg" == "-"* ]]; then

View File

@@ -21,13 +21,13 @@ fi
# __shellvaculib_pwd_at_begin="$PWD"
__shellvaculib_arg0_canonicalized="$(realpath "$0")"
function eprintln() {
svl_eprintln() {
echo -n "$@" >&2
echo
}
if [[ "${SHELLVACULIB_DEBUG-0}" == 1 ]]; then
eprintln "shellvaculib.bash sourced."
svl_eprintln "shellvaculib.bash sourced."
dollar_zero="$0"
all_args=("$@")
dollar_underscore="$_"
@@ -116,7 +116,7 @@ if [[ "${SHELLVACULIB_DEBUG-0}" == 1 ]]; then
"${cmd[@]}" || true
fi
function err() {
svl_err() {
if [[ $# == 0 ]]; then
echo "$0: unspecified error" >&2
else
@@ -124,28 +124,28 @@ function err() {
fi
}
function die() {
err "$@"
svl_die() {
svl_err "$@"
exit 1
}
throw_skip() {
svl_throw_skip() {
if [[ $# == 0 ]]; then
die "throw_skip expects at least one arg"
svl_die "svl_throw_skip expects at least one arg"
fi
declare -i skip="$1" i
shift
local args=("$@")
#always skip throw_skip itself
#always skip svl_throw_skip itself
skip=$(( skip + 1 ))
for (( i=${#FUNCNAME[@]}-1; i >= skip; i-- )); do
err "in ${FUNCNAME[i]}[${BASH_LINENO[i-1]}]:"
svl_err "in ${FUNCNAME[i]}[${BASH_LINENO[i-1]}]:"
done
die "${args[@]}"
svl_die "${args[@]}"
}
throw() {
throw_skip 1 "$@"
svl_throw() {
svl_throw_skip 1 "$@"
}
declare -gi _shellvaculib_max_args=1000000
@@ -156,7 +156,7 @@ _shellvaculib_min_andor_max_args_impl() {
return 0
fi
if (( actual_count < 0 )) || (( minimum_count < 0 )) || (( maximum_count < 0 )); then
die "this shouldn't happen (one of the counts negative in ${FUNCNAME[0]})"
svl_die "this shouldn't happen (one of the counts negative in ${FUNCNAME[0]})"
fi
local expect_message
if (( minimum_count == maximum_count )); then
@@ -168,47 +168,47 @@ _shellvaculib_min_andor_max_args_impl() {
elif (( minimum_count != 0 )) && (( maximum_count != _shellvaculib_max_args )); then
expect_message="expected between $minimum_count and $maximum_count arguments"
else
die "this shouldnt be possible really"
svl_die "this shouldnt be possible really"
fi
local error_message="Wrong number of arguments, $expect_message, got $actual_count"
if [[ "${#FUNCNAME[@]}" == 1 ]]; then
#we are being called from the top-level
die "$error_message"
svl_die "$error_message"
else
throw_skip 2 "$error_message"
svl_throw_skip 2 "$error_message"
fi
}
minmax_args() {
svl_minmax_args() {
if [[ $# != 3 ]]; then
throw_skip 1 "expected 3 args to minmax_args, got $#"
svl_throw_skip 1 "expected 3 args to svl_minmax_args, got $#"
fi
_shellvaculib_min_andor_max_args_impl "$1" "$2" "$3"
}
min_args() {
svl_min_args() {
if [[ $# != 2 ]]; then
throw_skip 1 "expected 2 args to min_args, got $#"
svl_throw_skip 1 "expected 2 args to svl_min_args, got $#"
fi
_shellvaculib_min_andor_max_args_impl "$1" "$2" "$_shellvaculib_max_args"
}
max_args() {
svl_max_args() {
if [[ $# != 2 ]]; then
throw_skip 1 "expected 2 args to max_args, got $#"
svl_throw_skip 1 "expected 2 args to svl_max_args, got $#"
fi
_shellvaculib_min_andor_max_args_impl "$1" 0 "$2"
}
exact_args() {
svl_exact_args() {
if [[ $# != 2 ]]; then
throw_skip 1 "expected 2 args to exact_args, got $#"
svl_throw_skip 1 "expected 2 args to svl_exact_args, got $#"
fi
_shellvaculib_min_andor_max_args_impl "$1" "$2" "$2"
}
idempotent_add_prompt_command() {
exact_args $# 1
svl_idempotent_add_prompt_command() {
svl_exact_args $# 1
PROMPT_COMMAND[0]=''${PROMPT_COMMAND[0]:-}
local to_add="$1"
for cmd in "${PROMPT_COMMAND[@]}"; do
@@ -221,16 +221,16 @@ idempotent_add_prompt_command() {
}
# because the folder containing the script as well as PWD can be deleted while we're using it (or its parents), it's impossible to know for sure. Woohoo!
probably_in_script_dir() {
svl_probably_in_script_dir() {
declare script_dir canon_pwd
script_dir="$(dirname "$(get_script_full_path)")"
script_dir="$(dirname "$__shellvaculib_arg0_canonicalized")"
canon_pwd="$(realpath "$PWD")"
[[ "$script_dir" == "$canon_pwd" ]]
}
assert_probably_in_script_dir() {
if ! probably_in_script_dir; then
die "This script must be run in its directory"
svl_assert_probably_in_script_dir() {
if ! svl_probably_in_script_dir; then
svl_die "This script must be run in its directory"
fi
return 0
}