From ef42a7089f18edc4afb0058ea26c055eedb2f1c4 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 4 Oct 2025 12:16:57 +0200 Subject: [PATCH] Make both tmux scripts use config files instead of being edited Because I may need to update them right? --- tmux-daemon.sh | 51 ++++++++++++++++++++++++---------- tmux-oneshot.sh | 74 ++++++++++++++++++++++++++++--------------------- 2 files changed, 80 insertions(+), 45 deletions(-) diff --git a/tmux-daemon.sh b/tmux-daemon.sh index 25f7d58..5ce1b72 100644 --- a/tmux-daemon.sh +++ b/tmux-daemon.sh @@ -1,22 +1,28 @@ #!/bin/bash -# tmux-daemonizer.sh -# Idempotent tmux daemon manager with optional per-command workdir +# tmux-daemon.sh +# Idempotent tmux daemon manager with config file support +# Load config file if it exists +SCRIPT_DIR=$(dirname "$0") +SCRIPT_NAME=$(basename "$0" .sh) +CONFIG_FILE="${SCRIPT_DIR}/${SCRIPT_NAME}.conf" +if [ -f "$CONFIG_FILE" ]; then + echo "Loading config from $CONFIG_FILE" + source "$CONFIG_FILE" +else + echo "Config file $CONFIG_FILE not found, generating template..." + cat > "$CONFIG_FILE" << 'EOF' +# tmux-daemon.conf +# Configuration file for tmux-daemon.sh + +# Session name SESSION="example" -echo "[main] SESSION: $SESSION" +# Whether to attach to session after setup (0 or 1) ATTACH_SESSION=0 -while getopts "a" opt; do - case "$opt" in - a) ATTACH_SESSION=1 ;; - \?) echo "Usage: $0 [-a]" >&2 - exit 1 ;; - esac -done -shift $((OPTIND-1)) - -# Define commands: "workdir:::command" OR just "command" +# Commands to run (array format) +# Format: "workdir:::command" OR just "command" CMDS=( "ping google.com" "ping google.com" @@ -26,6 +32,23 @@ CMDS=( "ping google.com" "ping google.com" ) +EOF + echo "Generated $CONFIG_FILE with default values. Please edit and run again." + exit 0 +fi + +# Validate required variables +if [ -z "$SESSION" ]; then + echo "Error: SESSION must be set in $CONFIG_FILE" >&2 + exit 1 +fi + +if [ ${#CMDS[@]} -eq 0 ]; then + echo "Error: CMDS array must be set in $CONFIG_FILE" >&2 + exit 1 +fi + +echo "[main] SESSION: $SESSION" echo "[main] Commands: ${CMDS[*]}" # Create session if missing @@ -115,4 +138,4 @@ echo "[main] Done." if [ $ATTACH_SESSION -eq 1 ]; then echo "[main] Attaching to tmux session: $SESSION" tmux attach -t $SESSION -fi +fi \ No newline at end of file diff --git a/tmux-oneshot.sh b/tmux-oneshot.sh index f3c0dfc..31ce89f 100644 --- a/tmux-oneshot.sh +++ b/tmux-oneshot.sh @@ -2,39 +2,51 @@ # tmux-oneshot.sh # Run a single command in a tmux session -SESSION="" -ATTACH_SESSION=0 - -# Hardcoded command and settings (uncomment and modify as needed) -# SESSION="mytest" -# COMMAND="ping google.com" -# ATTACH_SESSION=1 - - -while getopts "t:a" opt; do - case "$opt" in - t) SESSION="$OPTARG" ;; - a) ATTACH_SESSION=1 ;; - \?) echo "Usage: $0 [-t ] [-a] " >&2 - exit 1 ;; - esac -done -shift $((OPTIND-1)) - -# Use hardcoded command if no arguments provided -if [ $# -eq 0 ]; then - if [ -n "$COMMAND" ]; then - echo "Using hardcoded command: $COMMAND" - else - echo "Error: Command is required" >&2 - echo "Usage: $0 [-t ] [-a] " >&2 - exit 1 - fi +# Load config file if it exists +SCRIPT_DIR=$(dirname "$0") +SCRIPT_NAME=$(basename "$0" .sh) +CONFIG_FILE="${SCRIPT_DIR}/${SCRIPT_NAME}.conf" +if [ -f "$CONFIG_FILE" ]; then + echo "Loading config from $CONFIG_FILE" + source "$CONFIG_FILE" else - COMMAND="$*" + echo "Config file $CONFIG_FILE not found, generating template..." + cat > "$CONFIG_FILE" << 'EOF' +# tmux-oneshot.conf +# Configuration file for tmux-oneshot.sh + +# Session name (leave empty to use first word of command) +SESSION="" + +# Command to run (leave empty to use command line arguments) +COMMAND="" + +# Whether to attach to session after running command (0 or 1) +ATTACH_SESSION=0 +EOF + echo "Generated $CONFIG_FILE with default values. Please edit and run again." + exit 0 fi -# Use hardcoded session name, or first word of command if not provided +# Validate required variables +if [ -z "$SESSION" ] && [ -z "$COMMAND" ]; then + echo "Error: Either SESSION or COMMAND must be set in $CONFIG_FILE" >&2 + exit 1 +fi + +# Use command line arguments if provided, otherwise use config +if [ $# -gt 0 ]; then + COMMAND="$*" + echo "Using command line arguments: $COMMAND" +else + if [ -z "$COMMAND" ]; then + echo "Error: No command provided and COMMAND not set in config" >&2 + exit 1 + fi + echo "Using config command: $COMMAND" +fi + +# Use session name from config, or first word of command if not set if [ -z "$SESSION" ]; then SESSION=$(echo "$COMMAND" | awk '{print $1}') fi @@ -54,4 +66,4 @@ tmux send-keys -t $SESSION "$COMMAND" C-m if [ $ATTACH_SESSION -eq 1 ]; then echo "Attaching to tmux session: $SESSION" tmux attach -t $SESSION -fi +fi \ No newline at end of file