Added config file.

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

128
pm-task
View File

@@ -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() {
@@ -51,7 +116,7 @@ function subListProjects() {
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
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 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"
@@ -139,6 +202,17 @@ function subGotoWorkspace() {
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