From 439580c5cfd2cfa83b14d429fe79226ef4a4f40c Mon Sep 17 00:00:00 2001 From: rowan Date: Fri, 7 Mar 2025 12:10:00 -0600 Subject: [PATCH] add new opts for service files --- configctl | 83 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 13 deletions(-) diff --git a/configctl b/configctl index 7fb5223..fbdc891 100755 --- a/configctl +++ b/configctl @@ -10,13 +10,47 @@ MOUNT_POINT="$XDG_CONFIG_HOME" usage() { SCRIPT_NAME=$(basename $0) - echo "Usage: $SCRIPT_NAME [unmount]" - exit 1 + echo "Usage: $SCRIPT_NAME [OPTIONS] [unmount]" + echo + echo 'options' + echo ' -a, --automount automatically enable the changeset service file' + echo ' -f, --force ovewrite existing service file' + echo ' -s, --service-path the path to write the service file' } -if [ $# -lt 1 ]; then - usage -fi +subpath() { + parent_path="$1" + sub_path="$2" + + # Normalize paths (remove trailing slashes, handle relative paths, etc.) + parent_path=$(cd "$parent_path" && pwd) + sub_path=$(cd "$sub_path" && pwd) + + # Add trailing slash to parent_path if it doesn't have one + [[ "$parent_path" != */ ]] && parent_path+="/" + + # Check if sub_path starts with parent_path + if [[ "$sub_path" == "$parent_path"* ]]; then + return 0 # is a subpath + else + return 1 # is not a subpath + fi +} + +AUTOMOUNT=0 +FORCE=0 +SVC_PATH="$XDG_CONFIG_HOME/systemd/user" +while true; do + case "$1" in + -h | --help | -\? ) usage; exit ;; + -a | --automount ) AUTOMOUNT=1; shift ;; + -f | --force ) FORCE=1; shift ;; + -s | --service-path ) SVC_PATH="$2"; shift 2 ;; + \? | -\? ) echo "Unknown option: $1"; exit 1 ;; + -- ) shift; break ;; + * ) break ;; + esac +done SELECTED="$1" CHANGESET="$CHANGESET_DIR/$SELECTED" @@ -43,31 +77,55 @@ if mountpoint -q "$MOUNT_POINT"; then fusermount -u "$MOUNT_POINT" fi -SYSD_DIR="$XDG_CONFIG_HOME/systemd/user" SVC_NAME="$SELECTED-automount.service" +SVC_FILE="$SVC_PATH/$SVC_NAME" TMP=$(mktemp) -mkdir -p "$SYSD_DIR" +mkdir -p "$SVC_PATH" cat << EOF > "$TMP" [Unit] -Description=Mount fuse-overlayfs config +Description=Mount fuse-overlayfs config $SELECTED [Service] Type=oneshot RemainAfterExit=yes ExecStart=fuse-overlayfs -o lowerdir="$BASE_CONFIG:$CHANGESET",upperdir="$UPPER_DIR",workdir="$WORKDIR" "$MOUNT_POINT" -ExecStop=/usr/bin/fusermount -u $MOUNT_POINT +ExecStop=/usr/bin/fusermount -zu $MOUNT_POINT [Install] WantedBy=default.target EOF -diff "$TMP" "$SYSD_DIR/$SVC_NAME" > /dev/null 2>&1 +diff "$TMP" "$SVC_FILE" > /dev/null 2>&1 HAS_DIFF=$? PROMPT=0 +IS_HIDDEN=0 if [ "$HAS_DIFF" -gt 0 ]; then - mv "$TMP" "$SYSD_DIR/$SVC_NAME" - PROMPT=1 + if subpath "$MOUNT_POINT" "$SVC_PATH"; then + IS_HIDDEN=1 + echo "WARN: writing a service file to target mountpoint" + echo "'$SVC_PATH' is a subpath of '$MOUNT_POINT'" + echo "this won't break anything, but it will be hidden once '$SELECTED' is mounted" + echo "this means you won't be able to enable it. you may run this command with -a" + echo "to automatically mount it instead" + echo + fi + + if [ "$HAS_DIFF" -eq 2 ]; then + mv "$TMP" "$SVC_FILE" + elif [ "$HAS_DIFF" -eq 1 ] && [ "$FORCE" -eq 1 ]; then + mv "$TMP" "$SVC_FILE" + elif [ "$HAS_DIFF" -eq 1 ] && [ "$FORCE" -eq 0 ]; then + echo "INFO: Changes detected in existing service file but refusing to ovewrite." + echo "rerun with -f or --force to overwrite" + echo + fi + + if [ "$AUTOMOUNT" -eq 1 ]; then + systemctl --user enable --now "$SVC_FILE" + else + PROMPT=1 + fi fi mkdir -p "$WORKDIR" @@ -80,4 +138,3 @@ if [ "$PROMPT" -eq 1 ]; then echo "Run 'systemctl --user enable --now $SVC_NAME' to automount this config" fi -