diff --git a/ne b/ne index 7ee330c..1d29b51 100755 --- a/ne +++ b/ne @@ -1,6 +1,9 @@ #!/bin/bash -RED='\033[0;31m' -NC='\033[0m' # No Color + +#exit table +#exit 1 - legato a file +#exit 3 - legato a editor +#exit 4 - legato a sudo usage() { echo "ne - nginx edit" @@ -13,53 +16,71 @@ usage() { 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 /etc/nginx/sites-enabled -[[ $1 = "-h" ]] && usage + +#load default settings +text_editor=${EDITOR:=vim} +[[ -e ~/.local/share/ne/settings/auto_confirm ]] && skip_confirm=1 || skip_confirm=0 #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 + [[ $1 = "" ]] && echo "No specified editor, exiting, run -h for help" && exit 3 + text_editor=$1 + shift # past value + ;; + -p|--choose-privileges-escalator) + shift # past argument + [[ $1 = "" ]] && echo "No program specified, exiting, run -h for help" && exit 4 + program_privileges=$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 -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 +[[ ! $POSITIONAL_ARGS = "" ]] && file=$POSITIONAL_ARGS || file=../nginx.conf $(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 +[[ $? -eq 1 ]] && printf "File $file not found, exiting\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 ~/.local/share/ne/settings/program_privileges ]] && program_privileges=$(cat ~/.local/share/ne/settings/program_privileges) || program_privileges=sudo +#qua command -v e non which per silent quando fallisce +[[ ! -e $(command -v $text_editor) ]] && echo "$program_privileges is not installed" && exit 3 +[[ ! -e $(command -v $program_privileges) ]] && echo "$program_privileges is not installed" && exit 4 $program_privileges $text_editor $file diff --color=always $file /tmp/copia_check_nginx_${USER} edited=$? if [[ $edited = 1 ]] then - $program_privileges nginx -t || exit 1 - [[ $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" + $program_privileges nginx -t || exit 1 #se errori in conf, esci senza reload + + 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 echo "No edits found, no need for nginx reload" exit 1 diff --git a/ne.1 b/ne.1 index 8640f6e..a2393ac 100644 --- a/ne.1 +++ b/ne.1 @@ -1,10 +1,10 @@ .\" Manpage for ne - nginx edit. .\" Contact marco@raber.me to correct errors or typos. -.TH man 8 "31 March 2022" "1.0" "ne man page" +.TH man 8 "9 April 2022" "1.0" "ne man page" .SH NAME ne \- edit nginx conf .SH SYNOPSIS -ne [FILENAME] [-y] [-c EDITOR] +ne [FILENAME] [-y] [-c EDITOR] [-p sudo] .SH DESCRIPTION ne is a script that launches your preferred text editor to edit the nginx configuration. Once you close the editor it will perform a check on the conf and, if asked to, makes nginx reload the configuration. .SH OPTIONS @@ -18,9 +18,12 @@ ne does not ask for confirmation to reload the nginx conf .TP .B \-c [text editor] ne asks what text editor to use +.TP +.B \-p [program] +ne asks what program to use for privileges escalation. e.g. sudo or doas .SH SEE ALSO nginx .SH BUGS -Se fai ne -c muore tutto perchè non ricordo rip +eh ditemeli voi .SH AUTHOR Marco Raber (marco@raber.me)