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 docker compose down && docker compose up -d
fi fi
# read -e -p "Restart nginx service in container? " confirm # read -e -p "Restart nginx service in container? " confirm
# if [[ $confirm =~ ^[Yy]$ ]]; then # if [[ $confirm =~ ^[Yy]$ ]]; then
# docker exec -d nginx service nginx reload # docker exec -d nginx service nginx reload
# fi # fi
# Assume nginx is always in place. # Assume nginx is always in place.
docker exec -d nginx service nginx reload docker exec -d nginx service nginx reload
else else
@@ -37,6 +37,71 @@ function subBuild() {
fi 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. # List available projects in a workspace and let user choose one.
# Sets PROJECT_LIST (newline-separated) and PROJECT_COUNT globals # Sets PROJECT_LIST (newline-separated) and PROJECT_COUNT globals
function subListProjects() { function subListProjects() {
@@ -44,14 +109,14 @@ function subListProjects() {
PROJECT_LIST="" PROJECT_LIST=""
PROJECT_COUNT=0 PROJECT_COUNT=0
# Get list of subdirectories (excluding special dirs) # Get list of subdirectories (excluding special dirs)
if [ -d "$workspace" ]; then if [ -d "$workspace" ]; then
for dir in "$workspace"/*/; do for dir in "$workspace"/*/; do
[ -d "$dir" ] || continue [ -d "$dir" ] || continue
bname=$(basename "$dir") bname=$(basename "$dir")
# Skip hidden dirs and README* dirs # Skip hidden dirs and README* dirs
case "$bname" in case "$bname" in
.*) continue ;; '.'|'..'|'#'*|''|' '*) continue ;;
README*) continue ;; README*) continue ;;
esac esac
PROJECT_COUNT=$((PROJECT_COUNT + 1)) PROJECT_COUNT=$((PROJECT_COUNT + 1))
@@ -68,34 +133,32 @@ $bname"
function subGotoWorkspace() { function subGotoWorkspace() {
workspace="" workspace=""
# Determine workspace path based on hostname # If no argument provided, list projects and let user choose
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 [ -z "$1" ]; then 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_LIST=""
PROJECT_COUNT=0 PROJECT_COUNT=0
subListProjects "$workspace" subListProjects "$workspace"
@@ -109,18 +172,18 @@ function subGotoWorkspace() {
echo "Available projects in $workspace:" echo "Available projects in $workspace:"
count=1 count=1
echo "$PROJECT_LIST" | while read -r project; do echo "$PROJECT_LIST" | while read -r project; do
echo " [$count] $project" echo " [$count] $project"
count=$((count + 1)) count=$((count + 1))
done done
echo " [0] Cancel" echo " [0] Cancel"
read -r choice read -r choice
case "$choice" in case "$choice" in
''|*[!0-9]*) ''|*[!0-9]*)
echo "Invalid choice. Canceling." echo "Invalid choice. Canceling."
cd "$workspace" cd "$workspace"
return 0 return 0
;; ;;
esac esac
if [ "$choice" -eq 0 ]; then if [ "$choice" -eq 0 ]; then
@@ -135,10 +198,21 @@ function subGotoWorkspace() {
return 0 return 0
fi fi
# Get the selected project # Get the selected project
selected=$(echo "$PROJECT_LIST" | sed -n "${choice}p") selected=$(echo "$PROJECT_LIST" | sed -n "${choice}p")
workspace="$workspace/$selected" workspace="$workspace/$selected"
else 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" workspace="$workspace/$1"
fi fi
@@ -148,20 +222,20 @@ function subGotoWorkspace() {
# Application logs streaming (single log). # Application logs streaming (single log).
function subLogs() { function subLogs() {
docker logs -f $POD docker logs -f $POD
# # $1 is $2 of the script. # # $1 is $2 of the script.
# if [ $1 ]; then # if [ $1 ]; then
# # ELABORATE # # ELABORATE
# docker exec -it $1 tail -s 5 -f /var/www/app/storage/logs/laravel.log # docker exec -it $1 tail -s 5 -f /var/www/app/storage/logs/laravel.log
# else # else
# docker exec -it $POD tail -s 5 -f /var/www/app/storage/logs/laravel.log # docker exec -it $POD tail -s 5 -f /var/www/app/storage/logs/laravel.log
# fi # fi
} }
# Application logs streaming (daily log). # Application logs streaming (daily log).
function subLogsDaily() { function subLogsDaily() {
# $1 is $2 of the script. # $1 is $2 of the script.
if [ $1 ]; then if [ $1 ]; then
# ELABORATE # ELABORATE
docker exec -it $1 tail -s 5 -f /var/www/app/storage/logs/laravel-$TODAY.log docker exec -it $1 tail -s 5 -f /var/www/app/storage/logs/laravel-$TODAY.log
else else
docker exec -it $POD tail -s 5 -f /var/www/app/storage/logs/laravel-$TODAY.log 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 case $POD in
nginx) nginx)
docker exec -it $POD service nginx reload 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 docker exec -it $POD service nginx reload
;; ;;
esac esac
} }
@@ -187,7 +261,7 @@ function subRestart() {
# Container shell access. # Container shell access.
function subShell() { function subShell() {
# $1 is $2 of the script. # $1 is $2 of the script.
if [ $1 ]; then if [ $1 ]; then
docker exec -it $1 bash docker exec -it $1 bash
else else
@@ -209,18 +283,18 @@ function subHelp() {
echo " stats Display container resource usage statistics." echo " stats Display container resource usage statistics."
echo " up, start Start the Docker Compose project." echo " up, start Start the Docker Compose project."
echo " bash|rsh|sh Access a shell in the running container." 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 # Task
if [ $1 ]; then 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 if [ $(type -t docker) == "alias" ]; then
COMMAND='podman' COMMAND='podman'
fi fi
# Assumed container is named after the directory name. # Assumed container is named after the directory name.
TASK=$1 TASK=$1
TODAY=$(date +%Y-%m-%d) TODAY=$(date +%Y-%m-%d)
POD=${PWD##*/} POD=${PWD##*/}
@@ -228,45 +302,45 @@ if [ $1 ]; then
case $TASK in case $TASK in
bash|rsh|sh) bash|rsh|sh)
subShell $2 subShell $2
;; ;;
build|rebuild) build|rebuild)
subBuild subBuild
;; ;;
daily|today) daily|today)
subLogsDaily $2 subLogsDaily $2
;; ;;
down|stop) down|stop)
docker compose down docker compose down
;; ;;
logs|log) logs|log)
subLogs $2 subLogs $2
;; ;;
ps) ps)
# Because `watch` has no idea of the alias. # Because `watch` has no idea of the alias.
eval "watch -n 10 $COMMAND ps" eval "watch -n 10 $COMMAND ps"
;; ;;
reload) reload)
subReload subReload
;; ;;
restart) restart)
if [ $2 ]; then if [ $2 ]; then
subGotoWorkspace $2 subGotoWorkspace $2
fi fi
subRestart $2 subRestart $2
;; ;;
stats) stats)
docker stats docker stats
;; ;;
up|start) up|start)
docker compose up -d docker compose up -d
;; ;;
upgrade) upgrade)
docker compose pull && docker compose up -d --remove-orphans --force-recreate docker compose pull && docker compose up -d --remove-orphans --force-recreate
;; ;;
--help) --help)
subHelp subHelp
;; ;;
*) *)
subGotoWorkspace $TASK subGotoWorkspace $TASK
esac esac