diff --git a/nginx-sm.sh b/nginx-sm.sh index b89c3df..bd2c846 100644 --- a/nginx-sm.sh +++ b/nginx-sm.sh @@ -1,27 +1,32 @@ #!/bin/bash -#!/bin/bash - ################################################################################ # NGINX - Site Manager # # This script manages NGINX sites, providing functionality for enabling/disabling, -# listing, and editing site configurations. It simplifies the process of managing -# NGINX sites by providing a command-line interface for common operations. +# listing, editing, creating, and removing site configurations. It simplifies the +# process of managing NGINX sites by providing a command-line interface for common +# operations. # # Usage: -# ./nginx-sm.sh [options] +# ./nginx-sm.sh [options] # # Options: -# -e, --enable Enable a site by enabling its configuration file. -# -d, --disable Disable a site by disabling its configuration file. -# -l, --list List all available sites and their status. -# -ed, --edit Edit the configuration file of a site. +# -e, --enable Enable a site by enabling its configuration file. +# -d, --disable Disable a site by disabling its configuration file. +# -l, --list List all available sites and their status. +# -ed, --edit Edit the configuration file of a site. +# --editor Set editor for editing configurations. +# -c, --create Create a new site configuration. +# -rm, --remove Remove an existing site configuration. +# -h, --help Display help information. # # Examples: -# ./nginx-sm.sh --enable example.com # Enable the site example.com -# ./nginx-sm.sh --list # List all available sites -# ./nginx-sm.sh --edit example.com # Edit the configuration of example.com +# ./nginx-sm.sh --enable example.com # Enable the site example.com +# ./nginx-sm.sh --list # List all available sites +# ./nginx-sm.sh --edit example.com # Edit the configuration of example.com +# ./nginx-sm.sh --create example.com # Create a new configuration for example.com +# ./nginx-sm.sh --remove example.com # Remove the configuration for example.com # # Dependencies: # - NGINX web server must be installed and running. @@ -58,6 +63,7 @@ declare -g site_to_disable="" declare -g site_to_edit="" declare -g list_sites=false declare -g display_help=false +declare -g create="" declare -g editor="nano" # Fetch NGINX configuration directories @@ -85,6 +91,7 @@ initialize_variables() { site_to_edit="" list_sites=false display_help=false + create="" editor="nano" } @@ -101,12 +108,14 @@ show_help() { echo -e "\nnginx Site Manager" echo -e "Usage: $(basename "$0") [options] \n" echo -e "Options:" - echo -e "\t-e, --enable : Enable site" - echo -e "\t-d, --disable : Disable site" - echo -e "\t-l, --list: List sites" - echo -e "\t-ed, --edit : Edit site" - echo -e "\t--editor : Set editor" - echo -e "\t-h, --help: Display help" + echo -e "\t-e, --enable Enable site" + echo -e "\t-d, --disable Disable site" + echo -e "\t-l, --list List sites" + echo -e "\t-ed, --edit Edit site configuration" + echo -e "\t--editor Set editor for editing configurations" + echo -e "\t-c, --create Create a new site configuration" + echo -e "\t-rm, --remove Remove an existing site configuration" + echo -e "\t-h, --help Display this help message" echo -e "\n" } @@ -169,6 +178,77 @@ edit_site() { fi } +create_site() { + # Assign the first argument to the variable 'site' + local site=$1 + + # Check if the site configuration already exists + if [ -e "$sitesAvail/$site" ]; then + echo "Site already exists." + else + # Ensure the user has administrative privileges + check_sudo + + # Create a new site configuration file + touch "$sitesAvail/$site" + echo "Site created." + + # Prompt the user to edit the new site configuration + echo -n "Do you want to edit the new site? [y/n]: " + + # Save current stty configuration and set stty for raw input + local old_stty_cfg=$(stty -g) + stty raw -echo + + # Read a single character from the user + answer=$(head -c 1) + + # Restore previous stty configuration + stty $old_stty_cfg + + # Check if the user answered 'yes' (case insensitive) + if echo "$answer" | grep -iq "^y" ; then + # Open the site configuration file in the default editor + $editor "$sitesAvail/$site" + fi + fi +} + +remove_site() { + local site=$1 + + # Check if the site configuration file exists + if [[ ! -e "$sitesAvail/$site" ]]; then + echo "Site does not appear to exist." + return 1 + fi + + # Ask the user if they are sure to remove the site with a default answer of 'no' + read -r -p "Are you sure you want to remove $site? [yes/no]: " -i "no" answer + + # Convert the answer to lowercase and compare to 'yes' + if [[ ${answer,,} == "yes" ]]; then + # Ensure the user has administrative privileges + check_sudo + + # Remove the site configuration file + rm "$sitesAvail/$site" + echo "Site removed." + else + echo "Site removal canceled." + fi +} + +# Check if the string contains only spaces +is_all_spaces() { + local input_string="$1" + if [[ "$input_string" =~ ^[[:space:]]+$ ]]; then + echo "The string consists only of spaces. Exiting the script." + exit 1 + fi +} + +# Parse command line arguments parse_arguments() { while [ "$#" -gt 0 ]; do local key="$1" @@ -178,6 +258,7 @@ parse_arguments() { echo "No site specified to enable. Use -h for help." exit 1 fi + is_all_spaces "$2" check_sudo site_to_enable="$2" shift @@ -187,6 +268,7 @@ parse_arguments() { echo "No site specified to disable. Use -h for help." exit 1 fi + is_all_spaces "$2" check_sudo site_to_disable="$2" shift @@ -196,6 +278,7 @@ parse_arguments() { echo "No site specified to edit. Use -h for help." exit 1 fi + is_all_spaces "$2" check_sudo site_to_edit="$2" shift @@ -205,9 +288,29 @@ parse_arguments() { echo "No editor specified. Use -h for help." exit 1 fi + is_all_spaces "$2" editor="$2" shift ;; + -c|--create) + if [ -z "$2" ]; then + echo "No site specified. Use -h for help." + exit 1 + fi + is_all_spaces "$2" + create="$2" + shift + ;; + -rm|--remove) + if [ -z "$2" ]; then + echo "No site specified. Use -h for help." + exit 1 + fi + is_all_spaces "$2" + check_sudo + remove="$2" + shift + ;; -l|--list) list_sites=true ;; @@ -241,18 +344,29 @@ main() { exit 0 fi + if [ -n "$site_to_enable" ]; then + enable_site "$site_to_enable" + fi + + if [ -n "$site_to_disable" ]; then + disable_site "$site_to_disable" + fi + + if $list_sites; then + list_sites + fi + if check_editor "$editor"; then - if [ -n "$site_to_enable" ]; then - enable_site "$site_to_enable" - fi - if [ -n "$site_to_disable" ]; then - disable_site "$site_to_disable" - fi if [ -n "$site_to_edit" ]; then edit_site "$site_to_edit" fi - if $list_sites; then - list_sites + + if [ -n "$create" ]; then + create_site "$create" + fi + + if [ -n "$remove" ]; then + remove_site "$remove" fi fi }