2022-03-31 15:35:27 +02:00
|
|
|
#!/bin/bash
|
2022-04-09 15:32:19 +02:00
|
|
|
|
|
|
|
#exit table
|
|
|
|
#exit 1 - legato a file
|
|
|
|
#exit 3 - legato a editor
|
|
|
|
#exit 4 - legato a sudo
|
2022-03-31 15:35:27 +02:00
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo "ne - nginx edit"
|
|
|
|
echo "Usage: ne file_to_edit [-y] [-c]"
|
|
|
|
echo "Options:"
|
2022-03-31 16:26:25 +02:00
|
|
|
echo "-h -> Show this help"
|
2022-03-31 15:35:27 +02:00
|
|
|
echo "-y -> skip confirm on reload"
|
|
|
|
echo "-c -> manually choose the text editor (default = vim)"
|
2022-04-09 15:01:41 +02:00
|
|
|
echo "Note: ne chooses sudo by default, to change this behaviour update the file ~/.local/share/ne/program_privileges"
|
2022-03-31 15:35:27 +02:00
|
|
|
echo "Default behaviour: without arguments edits nginx.conf with confirmation"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2022-03-31 16:26:25 +02:00
|
|
|
pushd /etc/nginx/sites-enabled
|
2022-03-31 15:35:27 +02:00
|
|
|
|
2022-04-09 15:17:01 +02:00
|
|
|
#load default settings
|
2022-04-09 15:32:19 +02:00
|
|
|
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
|
2022-04-09 15:17:01 +02:00
|
|
|
[[ -e ~/.local/share/ne/settings/program_privileges ]] && program_privileges=$(cat ~/.local/share/ne/settings/program_privileges) || program_privileges=sudo
|
2022-03-31 15:35:27 +02:00
|
|
|
|
2022-04-09 15:17:01 +02:00
|
|
|
#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
|
2022-04-09 15:32:19 +02:00
|
|
|
[[ $1 = "" ]] && echo "No specified editor, exiting, run -h for help" && exit 3
|
2022-04-09 15:17:01 +02:00
|
|
|
text_editor=$1
|
|
|
|
shift # past value
|
|
|
|
;;
|
2022-04-09 15:32:19 +02:00
|
|
|
-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
|
|
|
|
;;
|
2022-04-09 15:17:01 +02:00
|
|
|
-*|--*)
|
|
|
|
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
|
2022-03-31 15:35:27 +02:00
|
|
|
|
|
|
|
$(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:
|
2022-04-09 15:32:19 +02:00
|
|
|
[[ $? -eq 1 ]] && printf "File $file not found, exiting\n" && exit 1
|
2022-03-31 15:35:27 +02:00
|
|
|
|
2022-04-09 15:32:19 +02:00
|
|
|
#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
|
2022-04-09 15:01:41 +02:00
|
|
|
$program_privileges $text_editor $file
|
2022-03-31 15:35:27 +02:00
|
|
|
diff --color=always $file /tmp/copia_check_nginx_${USER}
|
|
|
|
|
|
|
|
edited=$?
|
|
|
|
if [[ $edited = 1 ]]
|
|
|
|
then
|
2022-04-09 15:17:01 +02:00
|
|
|
$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
|
2022-03-31 15:35:27 +02:00
|
|
|
else
|
|
|
|
echo "No edits found, no need for nginx reload"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
pushd
|