ne/ne
2022-04-09 15:32:19 +02:00

89 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
#exit table
#exit 1 - legato a file
#exit 3 - legato a editor
#exit 4 - legato a sudo
usage() {
echo "ne - nginx edit"
echo "Usage: ne file_to_edit [-y] [-c]"
echo "Options:"
echo "-h -> Show this help"
echo "-y -> skip confirm on reload"
echo "-c -> manually choose the text editor (default = vim)"
echo "Note: ne chooses sudo by default, to change this behaviour update the file ~/.local/share/ne/program_privileges"
echo "Default behaviour: without arguments edits nginx.conf with confirmation"
exit 0
}
pushd /etc/nginx/sites-enabled
#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
[[ ! $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 "File $file not found, exiting\n" && exit 1
#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 #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
fi
pushd