feat: Enhance run.sh with improved app listing and editor selection, adding error handling and user-friendly prompts

This commit is contained in:
Christopher
2024-08-14 11:56:20 -05:00
parent e470f0e829
commit c7d482b757

View File

@@ -5,8 +5,16 @@ APPS_DIR="/var/lib/casaos/apps"
# Function to list all apps
list_apps() {
echo "Available apps:"
ls "$APPS_DIR"
if [ -d "$APPS_DIR" ] && [ "$(ls -A "$APPS_DIR")" ]; then
apps=($(ls "$APPS_DIR"))
echo "Available apps:"
for i in "${!apps[@]}"; do
echo "$((i+1)). ${apps[i]}"
done
else
echo "No apps found in $APPS_DIR"
exit 1
fi
}
# Function to edit the docker-compose file with the chosen editor
@@ -16,56 +24,79 @@ edit_docker_compose() {
local docker_compose_path="${APPS_DIR}/${service_name}/docker-compose.yml"
if [[ ! -f "$docker_compose_path" ]]; then
echo "The docker-compose.yml file does not exist for the service: $service_name"
echo "Error: The docker-compose.yml file does not exist for the service: $service_name"
exit 1
fi
# Check if the chosen editor is installed
if ! command -v "$editor_choice" &> /dev/null; then
echo "Error: $editor_choice is not installed. Please install it or choose another editor."
exit 1
fi
# Open the editor
if [[ "$editor_choice" == "nano" ]]; then
nano "$docker_compose_path"
elif [[ "$editor_choice" == "vim" ]]; then
vim "$docker_compose_path"
else
echo "Invalid editor choice. Please select 'nano' or 'vim'."
exit 1
fi
"$editor_choice" "$docker_compose_path"
# Apply the changes using casaos-cli
casaos-cli app-management apply "$service_name" --file="$docker_compose_path"
if casaos-cli app-management apply "$service_name" --file="$docker_compose_path"; then
echo "Changes applied successfully."
else
echo "Error: Failed to apply changes. Please check the docker-compose file for errors."
exit 1
fi
}
# Main script starts here
echo "Welcome to the CasaOS App Editor."
# Display Welcome message with links and support information
echo "-------------------"
echo "BigBearCasaOS App Editor V2.0"
echo "-------------------"
echo "Here are some links"
echo "https://community.bigbeartechworld.com"
echo "https://github.com/BigBearTechWorld"
echo "-------------------"
echo "If you would like to support me, please consider buying me a tea"
echo "https://ko-fi.com/bigbeartechworld"
echo ""
# List all apps for the user to choose from
list_apps
# Prompt the user for the app to edit
read -p "Enter the name of the app you want to edit: " SERVICE_NAME
# Get the list of apps
apps=($(ls "$APPS_DIR"))
# Check if the service directory exists
if [[ ! -d "${APPS_DIR}/${SERVICE_NAME}" ]]; then
echo "The selected app does not exist. Please try again."
exit 1
fi
# Prompt the user for the app to edit
while true; do
echo "Enter the number of the app you want to edit, or 'q' to quit:"
read -p "> " choice
if [[ "$choice" == "q" ]]; then
echo "Exiting the script."
exit 0
elif [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le "${#apps[@]}" ]; then
SERVICE_NAME="${apps[$((choice-1))]}"
break
else
echo "Invalid selection. Please try again."
fi
done
# Ask the user to choose an editor
echo "Please select an editor:"
echo "1. nano"
echo "2. vim"
read -p "Enter your choice (1 or 2): " EDITOR_CHOICE
case $EDITOR_CHOICE in
1)
edit_docker_compose "$SERVICE_NAME" "nano"
;;
2)
edit_docker_compose "$SERVICE_NAME" "vim"
;;
*)
echo "Invalid choice. Please enter 1 for nano or 2 for vim."
exit 1
;;
esac
PS3="Select an editor (enter the number): "
options=("nano" "vim" "quit")
select EDITOR_CHOICE in "${options[@]}"
do
case $EDITOR_CHOICE in
"nano"|"vim")
edit_docker_compose "$SERVICE_NAME" "$EDITOR_CHOICE"
break
;;
"quit")
echo "Exiting the script."
exit 0
;;
*)
echo "Invalid option. Please try again."
;;
esac
done
echo "Editing complete."