Make both tmux scripts use config files instead of being edited

Because I may need to update them right?
This commit is contained in:
2025-10-04 12:16:57 +02:00
parent e46aba65be
commit ef42a7089f
2 changed files with 80 additions and 45 deletions

View File

@@ -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

View File

@@ -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 <session_name>] [-a] <command>" >&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 <session_name>] [-a] <command>" >&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