Extract a scripts mixin

This commit is contained in:
Oliver Davies 2025-04-05 11:25:37 +01:00
parent 4bbca3493e
commit 1d284c9432
24 changed files with 3 additions and 32 deletions

View file

@ -1,10 +0,0 @@
{
home = {
sessionPath = [ "$HOME/.local/bin" ];
file.".local/bin" = {
source = ./scripts;
recursive = true;
};
};
}

View file

@ -1,21 +0,0 @@
#!/usr/bin/env bash
if [[ "$1" == "" ]]; then
echo "Usage: ${0##*/} <script-name>"; exit 2
fi
set -o errexit
set -o nounset
set -o pipefail
cat > "${1}" << EOF
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
echo "${1}"
EOF
chmod +x "${1}"

View file

@ -1,20 +0,0 @@
#!/usr/bin/env bash
# Abort a rebase, merge, `am`, a cherry-pick or a revert, depending on the situation.
if [[ -e .git/CHERRY_PICK_HEAD ]] ; then
exec git cherry-pick --abort "$@"
elif [[ -e .git/REVERT_HEAD ]] ; then
exec git revert --abort "$@"
elif [[ -e .git/rebase-apply/applying ]] ; then
exec git am --abort "$@"
elif [[ -e .git/rebase-apply ]] ; then
exec git rebase --abort "$@"
elif [[ -e .git/rebase-merge ]] ; then
exec git rebase --abort "$@"
elif [[ -e .git/MERGE_MODE ]] ; then
exec git merge --abort "$@"
else
echo git-abort: unknown state
exit 1
fi

View file

@ -1,34 +0,0 @@
#!/usr/bin/env bash
# Creates a new bare clone of a repository with the bare files within a `.bare`
# directory. It also sets the origin URL so push and pull work as expected.
if [[ "$1" == "" ]]; then
echo "Usage: ${0##*/} <repository> [<directory>]"
exit 2
fi
set -euo pipefail
repository_url="${1}"
directory="${2:-}"
location=".bare"
# If no destination directory is specified, get it from the repository URL - the same as "git clone".
if [ -z "${directory}" ]; then
directory="$(basename -s .git "${repository_url}")"
fi
# Create the parent directory if needed.
mkdir -pv "${directory}"
cd "${directory}"
git clone --bare "${repository_url}" "${location}"
# Adjust origin fetch locations.
cd "${location}"
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
# Set .git file contents.
cd ..
echo "gitdir: ./${location}" > .git

View file

@ -1,16 +0,0 @@
#!/usr/bin/env bash
# Continue a rebase or cherry-pick in the event of conflicts.
if [[ -e .git/CHERRY_PICK_HEAD ]] ; then
exec git cherry-pick --continue "$@"
elif [[ -e .git/rebase-apply/applying ]] ; then
exec git rebase --continue "$@"
elif [[ -e .git/rebase-apply ]] ; then
exec git rebase --continue "$@"
elif [[ -e .git/rebase-merge ]] ; then
exec git rebase --continue "$@"
else
echo git-abort: unknown state
exit 1
fi

View file

@ -1,8 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
git fetch --all
git stash
git pull --rebase
git stash pop

View file

@ -1,21 +0,0 @@
#!/usr/bin/env bash
# Usage: git up {branch} {remote}
set -e
if [[ $# < 1 ]]; then
echo "You must specify a branch name to update"
exit 1
fi
BRANCH=$1
REMOTE=${2:-origin}
git checkout ${BRANCH} && \
git fetch ${REMOTE} && \
echo && \
git sl ${BRANCH}..${REMOTE}/${BRANCH} && \
echo && \
git pull --quiet && \
git checkout -

View file

@ -1,17 +0,0 @@
#!/usr/bin/env bash
if [[ "$1" == "" ]]; then
echo "Usage: ${0##*/} <filename>"; exit 2
fi
set -euo pipefail
filename="${1}"
shift 1
pv "${filename}" \
| zcat \
| docker compose exec -T "${SERVICE_NAME:-database}" mysql \
-p"${DB_PASSWORD:-app}" \
-u"${DB_USER:-app}" \
"${DB_NAME:-app}"

View file

@ -1,10 +0,0 @@
#!/bin/zsh
# Check if we should use the `main` or `master` branch for this repo.
# Prefer `main` to `master`.
if git show-ref --quiet origin/main || git rev-parse main &>/dev/null; then
echo main
else
echo master
fi

View file

@ -1,36 +0,0 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
lsblk_output="$(lsblk -rpo "uuid,name,type,size,label,mountpoint,mountpoints,fstype" | grep "part" | grep -Ev "/(boot|nix/store)")"
partition_names="$(echo "$lsblk_output" | awk '{ printf "%s (%s)\n", $2, $4 }' )";
selected=$(echo "$partition_names" | dmenu -p "Select drive:" | awk '{ print $1 }')
test -n "$selected"
mount_device="$selected"
if sudo cryptsetup isLuks "$selected"; then
luks_name="usb"
${TERMINAL:-st} -n floatterm -g 60x1 -e sudo cryptsetup open "$selected" "$luks_name"
mount_device="/dev/mapper/$luks_name"
test -b "/dev/mapper/$luks_name"
fi
mount_point=$(echo -e "/mnt\n/media\n/run/media/$USER" | dmenu -p "Select mount point:")
test -n "$mount_point"
sudo mkdir -p "$mount_point"
if sudo mount "$mount_device" "$mount_point"; then
notify-send "Device mounted" "Mounted $selected at $mount_point."
else
notify-send "Mount failed" "Failed to mount $selected."
exit 1
fi

View file

@ -1,11 +0,0 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
original_directory="${HOME}/Downloads"
new_directory="${HOME}/Pictures/Screenshots"
find "${original_directory}" -mindepth 1 -maxdepth 1 -type f -name "Screenshot *" \
-exec mv {} "${new_directory}" \;

View file

@ -1,19 +0,0 @@
#!/usr/bin/env bash
if [[ "$1" == "" || "$2" == "" ]]; then
echo "Usage: ${0##*/} <machine-name> <module-name>"; exit 2
fi
set -euo pipefail
machine_name=$1
module_name=$2
git clone https://github.com/opdavies/drupal-module-template \
--depth=1 \
"${machine_name}"
pushd "${machine_name}"
just rename "${module_name}"
rm -fr .git .github justfile phpcs.xml.dist
popd

View file

@ -1,3 +0,0 @@
#!/usr/bin/env bash
xwallpaper --zoom ~/${XDG_REPOS_DIR}/nixos-config/wallpaper/wallpaper.jpg

View file

@ -1,9 +0,0 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
cd "${XDG_REPOS_DIR}/traefik-development"
docker compose up --detach
docker container ps

View file

@ -1,19 +0,0 @@
#!/usr/bin/env bash
# Stops all running Docker containers except for the global Traefik proxy.
set -euo pipefail
# Show the running containers.
docker container ps
echo ""
# Stop the running containers and show any that are still running.
docker container ls |
tail -n +2 |
grep -v traefik-development-reverse-proxy-1 |
awk '{print $1}' |
xargs docker container stop
echo ""
docker container ps

View file

@ -1,7 +0,0 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
docker container stop traefik-development-reverse-proxy-1
docker container ps

View file

@ -1,13 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
commit_sha="${1:-HEAD}"
tag="$(date '+%Y-%m-%d-%H.%M.%S')"
echo "Tagging commit $(git rev-parse "${commit_sha}") as ${tag}."
echo ""
# Tag the appropriate commit and push to the remote.
git tag "${tag}" "${commit_sha}"
git push origin "refs/tags/${tag}" --no-verify

View file

@ -1,26 +0,0 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
lsblk_output="$(lsblk -nrpo "name,type,size,mountpoint")"
mounted_drives="$(echo "$lsblk_output" | awk '($2=="part"||$2="crypt")&&$4!~/\/boot|\/home$|SWAP/&&length($4)>1{printf "%s (%s)\n",$4,$3}')"
all_unmountable="$(echo "$mounted_drives" | sed "/^$/d;s/ *$//")"
test -n "$all_unmountable"
selected="$(echo "$all_unmountable" | dmenu -i -p "Unmount which drive?")"
selected="${selected%% *}"
test -n "$selected"
sudo -A umount -l "/${selected#*/}"
notify-send "Device unmounted" "$selected has been unmounted."
# Close the selected drive if decrypted.
cryptid="$(echo "$lsblk_output" | grep "/${selected#*/}$")"
cryptid="${cryptid%% *}"
test -b /dev/mapper/"${cryptid##*/}"
sudo -A cryptsetup close "$cryptid"
notify-send "Device dencryption closed" "Drive is now securely locked again."

View file

@ -1,25 +0,0 @@
#!/usr/bin/env bash
# Update all top-level Git repository clones within my Code directories to their
# latest version.
set -o errexit
set -o nounset
dirs=$(find "$XDG_REPOS_DIR" -mindepth 1 -maxdepth 2 -type d -name .git -not -path '*/*.old/*')
for dir in $dirs; do
repo_path="${dir%/.git}"
cd "${repo_path}"
# If the repo is a bare clone, the commands need to be run within the
# worktree directory.
[[ -f .bare/worktrees/main/gitdir && -d main && -f main/.git ]] && cd main
echo "Updating $(pwd)"
# Update the repository.
git fetch --all --jobs=4 --progress --prune || true
git pull --rebase || true
done

View file

@ -1,7 +0,0 @@
{ ... }:
{
imports = [
./bin
];
}