nix fmt and fixes
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
{
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
{ lib, ... }:
|
||||
let
|
||||
inherit (builtins) stringLength substring;
|
||||
# aka startsWith but hopefully clear from the name what order the arguments go
|
||||
@@ -30,23 +27,23 @@ let
|
||||
}:
|
||||
lines:
|
||||
let
|
||||
pipeline = [
|
||||
(lib.splitString "\n")
|
||||
(list:
|
||||
if (builtins.length list) == 0 then list
|
||||
else if (lib.last list) == "" then lib.dropEnd 1 list
|
||||
else list
|
||||
)
|
||||
]
|
||||
++ lib.optional inlineComments (map (s:
|
||||
builtins.head (lib.splitString "#" s)
|
||||
))
|
||||
++ lib.optional trim (map lib.trim)
|
||||
++ lib.optional comments (builtins.filter (s:
|
||||
(builtins.substring 0 1 s) != "#"
|
||||
))
|
||||
++ lib.optional removeEmpty (builtins.filter (s: s != ""))
|
||||
;
|
||||
pipeline =
|
||||
[
|
||||
(lib.splitString "\n")
|
||||
(
|
||||
list:
|
||||
if (builtins.length list) == 0 then
|
||||
list
|
||||
else if (lib.last list) == "" then
|
||||
lib.dropEnd 1 list
|
||||
else
|
||||
list
|
||||
)
|
||||
]
|
||||
++ lib.optional inlineComments (map (s: builtins.head (lib.splitString "#" s)))
|
||||
++ lib.optional trim (map lib.trim)
|
||||
++ lib.optional comments (builtins.filter (s: (builtins.substring 0 1 s) != "#"))
|
||||
++ lib.optional removeEmpty (builtins.filter (s: s != ""));
|
||||
in
|
||||
lib.pipe lines pipeline;
|
||||
in
|
||||
|
@@ -75,7 +75,7 @@ let
|
||||
stringToPackageSet =
|
||||
from:
|
||||
lib.pipe from [
|
||||
(vaculib.listOfLines {})
|
||||
(vaculib.listOfLines { })
|
||||
(map nameToPackageSet)
|
||||
builtins.listToAttrs
|
||||
];
|
||||
|
@@ -1,3 +1,5 @@
|
||||
# the entire point is to be sh-compatible
|
||||
# shellcheck disable=SC2292
|
||||
if [ -z "${BASH_VERSINFO-}" ]; then
|
||||
echo "shellvaculib.bash: This script only works with bash" >&2
|
||||
exit 1
|
||||
@@ -10,7 +12,7 @@ if ! (return 0 2>/dev/null); then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "${SHELLVACULIB_COMPAT-0}" == 1 ]]; then
|
||||
if [[ ${SHELLVACULIB_COMPAT-0} == 1 ]]; then
|
||||
: # nothing
|
||||
else
|
||||
set -euo pipefail
|
||||
@@ -24,15 +26,17 @@ svl_eprintln() {
|
||||
}
|
||||
|
||||
_shellvaculib_debug_enabled() {
|
||||
[[ "${SHELLVACULIB_DEBUG-0}" == 1 ]]
|
||||
[[ ${SHELLVACULIB_DEBUG-0} == 1 ]]
|
||||
}
|
||||
|
||||
_shellvaculib_debug_print() {
|
||||
# shellcheck disable=SC2310
|
||||
if _shellvaculib_debug_enabled; then
|
||||
svl_eprintln "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
# shellcheck disable=SC2310
|
||||
if _shellvaculib_debug_enabled; then
|
||||
svl_eprintln "shellvaculib.bash sourced."
|
||||
declare dollar_zero dollar_underscore
|
||||
@@ -125,7 +129,7 @@ if _shellvaculib_debug_enabled; then
|
||||
"${cmd[@]}" || true
|
||||
fi
|
||||
|
||||
if [[ -z "${_shellvaculib_arg0_canonicalized-}" ]]; then
|
||||
if [[ -z ${_shellvaculib_arg0_canonicalized-} ]]; then
|
||||
if ! _shellvaculib_arg0_canonicalized="$(realpath -- "$0")"; then
|
||||
svl_eprintln "warn: could not get realpath of \$0: $0"
|
||||
fi
|
||||
@@ -154,9 +158,9 @@ svl_throw_skip() {
|
||||
shift
|
||||
declare -a args=("$@")
|
||||
#always skip svl_throw_skip itself
|
||||
skip=$(( skip + 1 ))
|
||||
for (( i=${#FUNCNAME[@]}-1; i >= skip; i-- )); do
|
||||
svl_err "in ${FUNCNAME[i]}[${BASH_LINENO[i-1]}]:"
|
||||
skip=$((skip + 1))
|
||||
for ((i = ${#FUNCNAME[@]} - 1; i >= skip; i--)); do
|
||||
svl_err "in ${FUNCNAME[i]}[${BASH_LINENO[i - 1]}]:"
|
||||
done
|
||||
svl_die "${args[@]}"
|
||||
}
|
||||
@@ -169,26 +173,26 @@ declare -gi _shellvaculib_max_args=1000000
|
||||
|
||||
_shellvaculib_min_andor_max_args_impl() {
|
||||
declare -i actual_count="$1" minimum_count="$2" maximum_count="$3"
|
||||
if (( minimum_count <= actual_count )) && (( actual_count <= maximum_count )); then
|
||||
if ((minimum_count <= actual_count)) && ((actual_count <= maximum_count)); then
|
||||
return 0
|
||||
fi
|
||||
if (( actual_count < 0 )) || (( minimum_count < 0 )) || (( maximum_count < 0 )); then
|
||||
if ((actual_count < 0)) || ((minimum_count < 0)) || ((maximum_count < 0)); then
|
||||
svl_die "this shouldn't happen (one of the counts negative in ${FUNCNAME[0]})"
|
||||
fi
|
||||
local expect_message
|
||||
if (( minimum_count == maximum_count )); then
|
||||
if ((minimum_count == maximum_count)); then
|
||||
expect_message="expected exactly $minimum_count argument(s)"
|
||||
elif (( minimum_count == 0 )) && (( maximum_count != _shellvaculib_max_args )); then
|
||||
elif ((minimum_count == 0)) && ((maximum_count != _shellvaculib_max_args)); then
|
||||
expect_message="expected at most $maximum_count argument(s)"
|
||||
elif (( minimum_count != 0 )) && (( maximum_count == _shellvaculib_max_args )); then
|
||||
elif ((minimum_count != 0)) && ((maximum_count == _shellvaculib_max_args)); then
|
||||
expect_message="expected at least $minimum_count argument(s)"
|
||||
elif (( minimum_count != 0 )) && (( maximum_count != _shellvaculib_max_args )); then
|
||||
elif ((minimum_count != 0)) && ((maximum_count != _shellvaculib_max_args)); then
|
||||
expect_message="expected between $minimum_count and $maximum_count arguments"
|
||||
else
|
||||
svl_die "this shouldnt be possible really"
|
||||
fi
|
||||
local error_message="Wrong number of arguments, $expect_message, got $actual_count"
|
||||
if [[ "${#FUNCNAME[@]}" == 1 ]]; then
|
||||
if [[ ${#FUNCNAME[@]} == 1 ]]; then
|
||||
#we are being called from the top-level
|
||||
svl_die "$error_message"
|
||||
else
|
||||
@@ -229,7 +233,7 @@ svl_idempotent_add_prompt_command() {
|
||||
PROMPT_COMMAND[0]=''${PROMPT_COMMAND[0]:-}
|
||||
declare to_add="$1" cmd
|
||||
for cmd in "${PROMPT_COMMAND[@]}"; do
|
||||
if [[ "$to_add" == "$cmd" ]]; then
|
||||
if [[ $to_add == "$cmd" ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
@@ -240,22 +244,23 @@ svl_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!
|
||||
svl_probably_in_script_dir() {
|
||||
declare script_dir canon_pwd
|
||||
if [[ -z "$_shellvaculib_arg0_canonicalized" ]]; then
|
||||
if [[ -z $_shellvaculib_arg0_canonicalized ]]; then
|
||||
_shellvaculib_debug_print "svl_probably_in_script_dir called when _shellvaculib_arg0_canonicalized is unset or blank, always returning false"
|
||||
return 1
|
||||
fi
|
||||
if ! script_dir="$(dirname -- "$_shellvaculib_arg0_canonicalized")"; then
|
||||
_shellvaculib_debug_print "svl_probably_in_script_dir failed to call dirname \$_shellvaculib_arg0_canonicalized, returning 1"
|
||||
_shellvaculib_debug_print 'svl_probably_in_script_dir failed to call $(dirname -- $_shellvaculib_arg0_canonicalized), returning 1'
|
||||
return 1
|
||||
fi
|
||||
if ! canon_pwd="$(realpath -- "$PWD")"; then
|
||||
_shellvaculib_debug_print "svl_probably_in_script_dir failed to call realpath \$PWD, returning 1"
|
||||
_shellvaculib_debug_print 'svl_probably_in_script_dir failed to call $(realpath -- $PWD), returning 1'
|
||||
return 1
|
||||
fi
|
||||
[[ "$script_dir" == "$canon_pwd" ]]
|
||||
[[ $script_dir == "$canon_pwd" ]]
|
||||
}
|
||||
|
||||
svl_assert_probably_in_script_dir() {
|
||||
# shellcheck disable=SC2310
|
||||
if ! svl_probably_in_script_dir; then
|
||||
svl_die "This script must be run in its directory"
|
||||
fi
|
||||
@@ -263,10 +268,10 @@ svl_assert_probably_in_script_dir() {
|
||||
}
|
||||
|
||||
svl_assert_root() {
|
||||
if [[ "${EUID:-}" ]]; then
|
||||
svl_throw "\$EUID unset!?"
|
||||
if [[ -n ${EUID:-} ]]; then
|
||||
svl_throw '$EUID unset!?'
|
||||
fi
|
||||
if [[ "$EUID" != 0 ]]; then
|
||||
if [[ $EUID != 0 ]]; then
|
||||
svl_die "must be root to run this"
|
||||
fi
|
||||
return 0
|
||||
@@ -280,7 +285,7 @@ svl_in_array() {
|
||||
shift 1
|
||||
|
||||
for value; do
|
||||
if [[ "$value" == "$needle" ]]; then
|
||||
if [[ $value == "$needle" ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
@@ -299,7 +304,7 @@ svl_confirm_or_die() {
|
||||
fi
|
||||
full_prompt="$prompt [yes/N]: "
|
||||
read -r -p "$full_prompt" response || true
|
||||
if [[ "$response" == "yes" ]]; then
|
||||
if [[ $response == "yes" ]]; then
|
||||
return 0
|
||||
else
|
||||
svl_die "exiting"
|
||||
@@ -314,6 +319,8 @@ svl_count() {
|
||||
return 0
|
||||
}
|
||||
|
||||
# svl_count_matches 'foo*bar'
|
||||
# counts the number of matches (correctly, even if nullglob is not set) and print to stdout
|
||||
svl_count_matches() {
|
||||
svl_exact_args $# 1
|
||||
declare nullglob_before
|
||||
@@ -323,8 +330,10 @@ svl_count_matches() {
|
||||
nullglob_before=disabled
|
||||
fi
|
||||
shopt -s nullglob
|
||||
# intentional expansion of arg, so that *s and such will expand
|
||||
# shellcheck disable=SC2086
|
||||
svl_count $1
|
||||
if [[ "$nullglob_before" == "disabled" ]]; then
|
||||
if [[ $nullglob_before == "disabled" ]]; then
|
||||
shopt -u nullglob
|
||||
fi
|
||||
return 0
|
||||
|
@@ -6,12 +6,13 @@
|
||||
}:
|
||||
let
|
||||
k = 1000;
|
||||
m = k*1000;
|
||||
g = m*1000;
|
||||
t = g*1000;
|
||||
m = k * 1000;
|
||||
g = m * 1000;
|
||||
t = g * 1000;
|
||||
ki = 1024;
|
||||
_8ki = 8*ki;
|
||||
vdev_size = lib.pipe (14*t) [ # 14TB
|
||||
_8ki = 8 * ki;
|
||||
vdev_size = lib.pipe (14 * t) [
|
||||
# 14TB
|
||||
(n: n * 0.99) # leave 1% unused
|
||||
builtins.floor # convert back to int
|
||||
(n: (n / _8ki) * _8ki) # round to multiple of 8KiB
|
||||
@@ -39,29 +40,26 @@ let
|
||||
easystore_2
|
||||
];
|
||||
fs_type_raid = "fd00"; # Linux RAID
|
||||
fs_type_zfs = "a504"; # FreeBSD ZFS
|
||||
split_config =
|
||||
md_name:
|
||||
{
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions.mdadm = {
|
||||
size = "${builtins.toString md_partition_size_ki}K";
|
||||
type = fs_type_raid;
|
||||
content = {
|
||||
type = "mdraid";
|
||||
name = md_name;
|
||||
};
|
||||
fs_type_zfs = "a504"; # FreeBSD ZFS
|
||||
split_config = md_name: {
|
||||
type = "disk";
|
||||
content = {
|
||||
type = "gpt";
|
||||
partitions.mdadm = {
|
||||
size = "${builtins.toString md_partition_size_ki}K";
|
||||
type = fs_type_raid;
|
||||
content = {
|
||||
type = "mdraid";
|
||||
name = md_name;
|
||||
};
|
||||
};
|
||||
}
|
||||
;
|
||||
};
|
||||
};
|
||||
md_name_seagate = "propdata-seagates-combiner";
|
||||
md_name_easystore = "propdata-easystores-combiner";
|
||||
poolname = "propdata";
|
||||
# each 14TB
|
||||
easystores_14 = vaculib.listOfLines {} ''
|
||||
easystores_14 = vaculib.listOfLines { } ''
|
||||
ata-WDC_WD140EDFZ-11A0VA0_Y5J3929C
|
||||
ata-WDC_WD140EDGZ-11B2DA2_2BHRSN3F
|
||||
ata-WDC_WD140EDGZ-11B2DA2_2CG19ERP
|
||||
@@ -112,7 +110,7 @@ in
|
||||
easystores_10
|
||||
easystores_14
|
||||
seagate_gaming
|
||||
;
|
||||
;
|
||||
};
|
||||
config.disko.enableConfig = false;
|
||||
config.disko.checkScripts = true;
|
||||
|
@@ -7,12 +7,16 @@ svl_assert_root
|
||||
|
||||
declare top_dataset="trip" snapshot_name snapshot_prefix
|
||||
declare -a previous_snapshots
|
||||
mapfile -t previous_snapshots < <(zfs list -t snapshot --json "$top_dataset" | jq -r '.datasets|values[]|.snapshot_name')
|
||||
declare snapshot_lines
|
||||
snapshot_lines="$(zfs list -t snapshot --json "$top_dataset" | jq -r '.datasets|values[]|.snapshot_name')"
|
||||
mapfile -t previous_snapshots <<<"$snapshot_lines"
|
||||
|
||||
snapshot_prefix="semiauto--$(date '+%Y-%m-%d')--"
|
||||
declare -i idx=1
|
||||
while true; do
|
||||
snapshot_name="${snapshot_prefix}${idx}"
|
||||
# the only purpose of svl_in_array is to be used in a condition
|
||||
# shellcheck disable=SC2310
|
||||
if ! svl_in_array "$snapshot_name" "${previous_snapshots[@]}"; then
|
||||
break
|
||||
fi
|
||||
@@ -20,17 +24,18 @@ while true; do
|
||||
done
|
||||
|
||||
declare -a all_datasets
|
||||
|
||||
mapfile -t all_datasets < <(zfs list -r --json "$top_dataset" | jq -r '.datasets|keys[]')
|
||||
declare all_datasets_lines
|
||||
all_datasets_lines="$(zfs list -r --json "$top_dataset" | jq -r '.datasets|keys[]')"
|
||||
mapfile -t all_datasets <<<"$all_datasets_lines"
|
||||
declare -a should_snap_datasets excluded
|
||||
for dataset in "${all_datasets[@]}"; do
|
||||
case "$dataset" in
|
||||
trip/fw-backup|trip/fw-backup/*|trip/fw-backup-2|trip/fw-backup-2/*)
|
||||
excluded+=("$dataset")
|
||||
;;
|
||||
*)
|
||||
should_snap_datasets+=("$dataset")
|
||||
;;
|
||||
trip/fw-backup | trip/fw-backup/* | trip/fw-backup-2 | trip/fw-backup-2/*)
|
||||
excluded+=("$dataset")
|
||||
;;
|
||||
*)
|
||||
should_snap_datasets+=("$dataset")
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
@@ -41,7 +46,7 @@ done
|
||||
echo
|
||||
echo
|
||||
read -r -p "Type y to continue: " confirmation
|
||||
if [[ "$confirmation" != [yY] ]]; then
|
||||
if [[ $confirmation != [yY] ]]; then
|
||||
echo "abort"
|
||||
exit 1
|
||||
fi
|
||||
|
@@ -2,6 +2,7 @@
|
||||
let
|
||||
shellFiles = [
|
||||
"*.sh"
|
||||
"*.bash"
|
||||
"dcd"
|
||||
"dliam"
|
||||
"dmmm"
|
||||
@@ -24,7 +25,7 @@ in
|
||||
"--norc"
|
||||
"--source-path=${pkgs.shellvaculib}/bin"
|
||||
"--enable=all"
|
||||
"--exclude=SC2250"
|
||||
"--exclude=SC2250,SC2016"
|
||||
];
|
||||
programs.shfmt.enable = true;
|
||||
programs.shfmt.includes = shellFiles;
|
||||
|
Reference in New Issue
Block a user