From 410491070acc6819a06e6407d6c48af20fd7f03e Mon Sep 17 00:00:00 2001 From: Marco Raber Date: Thu, 31 Mar 2022 15:35:27 +0200 Subject: [PATCH] Preparato per dir diversa da /etc/nginx/sites-enabled --- ne | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 ne diff --git a/ne b/ne new file mode 100755 index 0000000..f286548 --- /dev/null +++ b/ne @@ -0,0 +1,53 @@ +#!/bin/bash +RED='\033[0;31m' +NC='\033[0m' # No Color + +usage() { + echo "ne - nginx edit" + echo "Usage: ne file_to_edit [-y] [-c]" + echo "Options:" + echo "-y -> skip confirm on reload" + echo "-c -> manually choose the text editor (default = vim)" + echo "Default behaviour: without arguments edits nginx.conf with confirmation" + exit 0 +} +text_editor=$EDITOR +skip_confirm=0 +sites-enabled=/etc/nginx/sites-enabled + +pushd $sites-enabled +[[ $1 = "-h" ]] && usage + +#[[ $# -le 0 ]] && file=../nginx.conf && printf "${RED}Editing nginx.conf${NC}\n" && sleep 0.50 || file=$1 && shift #programma senza argomenti edita configurazione globale di nginx + +#sbagliato l'if, se ho minore o uguale ad 1 argomento allora o è file o è flag, se file non esiste è un flag +if [[ $# -le 0 ]] +then + #lanciato senza argomenti: edita configurazione globale di nginx + file=../nginx.conf + printf "${RED}Editing nginx.conf${NC}\n" + sleep 0.5 +else + [[ -e $1 ]] && file=$1 && shift +fi +[[ $1 = "-y" ]] && shift && skip_confirm=1 +[[ -e ~/.local/share/ne_auto_confirm ]] && skip_confirm=1 #se flag -y in seconda posizione o esiste ~/.local/share/ne_auto_confirm + +$(which cp) $file /tmp/copia_check_nginx_${USER} #$(which cat) $file > /tmp/copia_check_nginx_${USER} #non so se usare cat > o cp +#se cat > o cp danno errore vuol dire che il file non esiste ed esco male: +[[ $? -eq 1 ]] && printf "${RED}File${NC} $file ${RED}not found, aborting${NC}\n" && exit 1 + +[[ $1 = "-c" ]] && read -p "Editor? " text_editor || text_editor=vim #se flag è -c +sudo $text_editor $file +diff --color=always $file /tmp/copia_check_nginx_${USER} + +edited=$? +if [[ $edited = 1 ]] +then + sudo nginx -t || exit 1 + [[ $skip_confirm = 1 ]] && sudo nginx -s reload || confirm "Configuration OK, reload conf? " && sudo nginx -s reload && echo "nginx should be updated" || echo "Aborted" +else + echo "No edits found, no need for nginx reload" + exit 1 +fi +pushd