new_argument_parse #1

Merged
mraber merged 3 commits from new_argument_parse into master 2022-04-09 15:37:26 +02:00
Showing only changes of commit 9244181b83 - Show all commits

67
ne
View File

@ -18,48 +18,59 @@ skip_confirm=0
#sites-enabled="/etc/nginx/sites-enabled" #sites-enabled="/etc/nginx/sites-enabled"
pushd /etc/nginx/sites-enabled pushd /etc/nginx/sites-enabled
[[ $1 = "-h" ]] && usage
#load default settings
[[ -e ~/.local/share/ne/settings/auto_confirm ]] && skip_confirm=1 #se esiste ~/.local/share/ne/settings/auto_confirm
[[ -e ~/.local/share/ne/settings/program_privileges ]] && program_privileges=$(cat ~/.local/share/ne/settings/program_privileges) || program_privileges=sudo
#parse settings for this run
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
-y|--skip-confirm)
skip_confirm=1
shift # past argument
;;
-c|--choose-editor)
shift # past argument
text_editor=$1
shift # past value
;;
-*|--*)
echo "Unknown option $1"
# exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
#sbagliato l'if, se ho minore o uguale ad 1 argomento allora o è file o è flag, se file non esiste è un flag [[ ! $POSITIONAL_ARGS = "" ]] && file=$POSITIONAL_ARGS || file=../nginx.conf
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/settings/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 $(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: #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 [[ $? -eq 1 ]] && printf "${RED}File${NC} $file ${RED}not found, aborting${NC}\n" && exit 1
if [[ $1 = "-c" ]] #se flag è -c e ho ancora una parola dopo allora quello è l'editor
then
shift
if [[ $1 = "" ]]
then
read -p "Editor? " text_editor
else
text_editor=$1
shift
fi
fi
[[ -e $(which $text_editor) ]] || exit 3 [[ -e $(which $text_editor) ]] || exit 3
[[ -e ~/.local/share/ne/settings/program_privileges ]] && program_privileges=$(cat ~/.local/share/ne/settings/program_privileges) || program_privileges=sudo
$program_privileges $text_editor $file $program_privileges $text_editor $file
diff --color=always $file /tmp/copia_check_nginx_${USER} diff --color=always $file /tmp/copia_check_nginx_${USER}
edited=$? edited=$?
if [[ $edited = 1 ]] if [[ $edited = 1 ]]
then then
$program_privileges nginx -t || exit 1 $program_privileges nginx -t || exit 1 #se errori in conf, esci senza reload
[[ $skip_confirm = 1 ]] && $program_privileges nginx -s reload || confirm "Configuration OK, reload conf? " && $program_privileges nginx -s reload && echo "nginx should be updated" || echo "Aborted"
if [[ $skip_confirm = 1 ]]; #se continuo non ho errori e verifico se ho lo skipass
then
$program_privileges nginx -s reload
else
confirm "Configuration OK, reload conf?" && $program_privileges nginx -s reload
fi
else else
echo "No edits found, no need for nginx reload" echo "No edits found, no need for nginx reload"
exit 1 exit 1