Added config file.

This commit is contained in:
Ali
2026-04-09 13:31:03 +08:00
parent 4034114d4d
commit 05457bca08

220
pm-task
View File

@@ -24,12 +24,12 @@ function subBuild() {
docker compose down && docker compose up -d
fi
# read -e -p "Restart nginx service in container? " confirm
# if [[ $confirm =~ ^[Yy]$ ]]; then
# docker exec -d nginx service nginx reload
# fi
# read -e -p "Restart nginx service in container? " confirm
# if [[ $confirm =~ ^[Yy]$ ]]; then
# docker exec -d nginx service nginx reload
# fi
# Assume nginx is always in place.
# Assume nginx is always in place.
docker exec -d nginx service nginx reload
else
@@ -37,6 +37,71 @@ function subBuild() {
fi
}
# Get config file path
function getConfigFile() {
echo "$HOME/.config/pmtask.conf"
}
# Read docker-workspace from config file, or return empty string
function getWorkspaceFromConfig() {
config_file=$(getConfigFile)
if [ -f "$config_file" ]; then
# Parse INI file for docker-workspace option (skip comments and sections)
while IFS= read -r line || [ -n "$line" ]; do
# Skip empty lines, comments, and section headers
case "$line" in
''|'#'*|'['*) continue ;;
esac
# Parse key=value
key=$(echo "$line" | cut -d'=' -f1 | tr -d ' ')
value=$(echo "$line" | cut -d'=' -f2- | tr -d ' ')
if [ "$key" = "docker-workspace" ]; then
echo "$value"
return 0
fi
done < "$config_file"
fi
return 1
}
# Save workspace to config file
function saveWorkspaceToConfig() {
config_file=$(getConfigFile)
config_dir=$(dirname "$config_file")
# Create config directory if it doesn't exist
if [ ! -d "$config_dir" ]; then
mkdir -p "$config_dir"
fi
# Check if config file exists and has docker-workspace
if [ -f "$config_file" ]; then
# Update existing entry
temp_file=$(mktemp)
found=0
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
docker-workspace=*)
echo "docker-workspace=$1"
found=1
;;
*)
echo "$line"
;;
esac
done < "$config_file" > "$temp_file"
# Append if not found
if [ "$found" -eq 0 ]; then
echo "docker-workspace=$1" >> "$temp_file"
fi
mv "$temp_file" "$config_file"
else
echo "docker-workspace=$1" > "$config_file"
fi
}
# List available projects in a workspace and let user choose one.
# Sets PROJECT_LIST (newline-separated) and PROJECT_COUNT globals
function subListProjects() {
@@ -44,14 +109,14 @@ function subListProjects() {
PROJECT_LIST=""
PROJECT_COUNT=0
# Get list of subdirectories (excluding special dirs)
# Get list of subdirectories (excluding special dirs)
if [ -d "$workspace" ]; then
for dir in "$workspace"/*/; do
[ -d "$dir" ] || continue
[ -d "$dir" ] || continue
bname=$(basename "$dir")
# Skip hidden dirs and README* dirs
# Skip hidden dirs and README* dirs
case "$bname" in
.*) continue ;;
'.'|'..'|'#'*|''|' '*) continue ;;
README*) continue ;;
esac
PROJECT_COUNT=$((PROJECT_COUNT + 1))
@@ -68,34 +133,32 @@ $bname"
function subGotoWorkspace() {
workspace=""
# Determine workspace path based on hostname
case $HOSTNAME in
Eighty)
workspace="$HOME/docker"
;;
HiveDC)
workspace="/mnt/workspace/docker"
;;
hivegcp)
workspace="$HOME/docker"
;;
Podman)
workspace="/mnt/workspace/podman"
;;
HivePM)
workspace="/mnt/workspace/podman"
;;
libpodman)
workspace="/podman"
;;
*)
echo "Unrecognized host: $HOSTNAME"
return 1
;;
esac
# If no argument provided, list projects and let user choose
# If no argument provided, list projects and let user choose
if [ -z "$1" ]; then
# Get workspace from config or prompt user
workspace=$(getWorkspaceFromConfig)
if [ -z "$workspace" ]; then
# No config, prompt user for workspace
echo "No workspace configured."
echo "Enter workspace path (e.g., $HOME/docker or /mnt/workspace/podman): "
read -r workspace
if [ -z "$workspace" ]; then
echo "Workspace path cannot be empty."
exit 1
fi
# Save to config
saveWorkspaceToConfig "$workspace"
fi
# Validate workspace exists
if [ ! -d "$workspace" ]; then
echo "Workspace directory does not exist: $workspace"
exit 1
fi
PROJECT_LIST=""
PROJECT_COUNT=0
subListProjects "$workspace"
@@ -109,18 +172,18 @@ function subGotoWorkspace() {
echo "Available projects in $workspace:"
count=1
echo "$PROJECT_LIST" | while read -r project; do
echo " [$count] $project"
echo " [$count] $project"
count=$((count + 1))
done
echo " [0] Cancel"
echo " [0] Cancel"
read -r choice
case "$choice" in
''|*[!0-9]*)
''|*[!0-9]*)
echo "Invalid choice. Canceling."
cd "$workspace"
return 0
;;
;;
esac
if [ "$choice" -eq 0 ]; then
@@ -135,10 +198,21 @@ function subGotoWorkspace() {
return 0
fi
# Get the selected project
# Get the selected project
selected=$(echo "$PROJECT_LIST" | sed -n "${choice}p")
workspace="$workspace/$selected"
else
# Workspace provided via config, append project name
workspace=$(getWorkspaceFromConfig)
if [ -z "$workspace" ]; then
# No config, prompt user
echo "No workspace configured."
echo "Enter workspace path: "
read -r workspace
saveWorkspaceToConfig "$workspace"
fi
workspace="$workspace/$1"
fi
@@ -148,20 +222,20 @@ function subGotoWorkspace() {
# Application logs streaming (single log).
function subLogs() {
docker logs -f $POD
# # $1 is $2 of the script.
# if [ $1 ]; then
# # ELABORATE
# docker exec -it $1 tail -s 5 -f /var/www/app/storage/logs/laravel.log
# else
# docker exec -it $POD tail -s 5 -f /var/www/app/storage/logs/laravel.log
# fi
# # $1 is $2 of the script.
# if [ $1 ]; then
# # ELABORATE
# docker exec -it $1 tail -s 5 -f /var/www/app/storage/logs/laravel.log
# else
# docker exec -it $POD tail -s 5 -f /var/www/app/storage/logs/laravel.log
# fi
}
# Application logs streaming (daily log).
function subLogsDaily() {
# $1 is $2 of the script.
# $1 is $2 of the script.
if [ $1 ]; then
# ELABORATE
# ELABORATE
docker exec -it $1 tail -s 5 -f /var/www/app/storage/logs/laravel-$TODAY.log
else
docker exec -it $POD tail -s 5 -f /var/www/app/storage/logs/laravel-$TODAY.log
@@ -173,11 +247,11 @@ function subReload() {
case $POD in
nginx)
docker exec -it $POD service nginx reload
;;
*)
# docker exec -it $POD service apache2 reload # No longer using Apache.
;;
*)
# docker exec -it $POD service apache2 reload # No longer using Apache.
docker exec -it $POD service nginx reload
;;
;;
esac
}
@@ -187,7 +261,7 @@ function subRestart() {
# Container shell access.
function subShell() {
# $1 is $2 of the script.
# $1 is $2 of the script.
if [ $1 ]; then
docker exec -it $1 bash
else
@@ -209,18 +283,18 @@ function subHelp() {
echo " stats Display container resource usage statistics."
echo " up, start Start the Docker Compose project."
echo " bash|rsh|sh Access a shell in the running container."
echo " (no command) List available projects and choose one to navigate to."
echo " (no command) List available projects and choose one to navigate to."
}
# Task
if [ $1 ]; then
# Assume the host is running podman when alias docket='podman' exists.
# Assume the host is running podman when alias docket='podman' exists.
if [ $(type -t docker) == "alias" ]; then
COMMAND='podman'
fi
# Assumed container is named after the directory name.
# Assumed container is named after the directory name.
TASK=$1
TODAY=$(date +%Y-%m-%d)
POD=${PWD##*/}
@@ -228,45 +302,45 @@ if [ $1 ]; then
case $TASK in
bash|rsh|sh)
subShell $2
;;
;;
build|rebuild)
subBuild
;;
;;
daily|today)
subLogsDaily $2
;;
;;
down|stop)
docker compose down
;;
;;
logs|log)
subLogs $2
;;
;;
ps)
# Because `watch` has no idea of the alias.
# Because `watch` has no idea of the alias.
eval "watch -n 10 $COMMAND ps"
;;
;;
reload)
subReload
;;
;;
restart)
if [ $2 ]; then
subGotoWorkspace $2
fi
subRestart $2
;;
;;
stats)
docker stats
;;
;;
up|start)
docker compose up -d
;;
;;
upgrade)
docker compose pull && docker compose up -d --remove-orphans --force-recreate
;;
--help)
;;
--help)
subHelp
;;
*)
;;
*)
subGotoWorkspace $TASK
esac