Modifications

- proper exit codes in brewlog
- check if /usr/local/bin is writable (and use sudo [user permission])
This commit is contained in:
robocopAlpha 2020-08-04 01:32:34 +03:00
parent 7228492a12
commit da0cd940ed
2 changed files with 30 additions and 13 deletions

View file

@ -59,29 +59,36 @@ if [ ! -f "$LOGFILE" ] ; then
touch "$LOGFILE"
fi
if [ $# -eq 0 ]; then
if [ "$#" -eq 0 ]; then
HELP
exit 0;
fi
if [ "$1" == "--help" ]; then
HELP
exit 0;
elif [ "$1" == "--brew-help" ]; then
brew help
exit 0;
elif [ "$1" == "version" ]; then
VERSION
exit 0;
elif [ "$1" == "tail" ]; then
if [ "$2" == "-n" ]; then
# tail with specified number of lines
tail -n "$3" "$LOGFILE"
exit 0;
else
# tail with 15 lines (default -n for tail is 10, I wanted more)
tail -n 15 "$LOGFILE"
exit 0;
fi
elif [ "$1" == "archive" ]; then
if [ -f "$LOGFILE" ] ; then
# Archiving logfile i.e. brew.log is removed (will be created on next run)
xz -vf $LOGFILE
mv "${LOGFILE}.xz" "$LOGFILE-$(date +'%Y%m%d').xz"
exit 0;
else
echo "$LOGFILE doesn't exist"
exit 1
@ -90,4 +97,5 @@ else
echo "brew $* :: $(date)" | tee -a "$LOGFILE"
# logs both STDOUT and STDERR to $LOGFILE
$(which brew) "$@" 2>&1 | tee -a "$LOGFILE"
exit 0;
fi

View file

@ -1,17 +1,26 @@
#!/bin/bash
if [ -w /usr/local/bin ]; then
curl -sSL 'https://raw.githubusercontent.com/robocopAlpha/brewlog/master/brewlog.sh' > /usr/local/bin/brewlog
chmod +x /usr/local/bin/brewlog
else
sudo -s
if [ -w /usr/local/bin ]; then
curl -sSL 'https://raw.githubusercontent.com/robocopAlpha/brewlog/master/brewlog.sh' > /usr/local/bin/brewlog
chmod +x /usr/local/bin/brewlog
exit
sudo -k
else
echo "cannot write to /usr/local/bin".
exit 1;
fi
fi
curl -sSL 'https://raw.githubusercontent.com/robocopAlpha/brewlog/master/brewlog.sh' >| brewlog.sh
echo "user $(whoami) cannot write to /usr/local/bin."
read -r -p "use sudo (y/N): " choice
shopt -q nocasematch;
if [ "$?" -eq "1" ]; then
shopt -s nocasematch
flag=1
fi
if [[ "$choice" =~ ^(yes|y)$ ]]
then
sudo mv brewlog.sh /usr/local/bin/brewlog
sudo chmod +x /usr/local/bin/brewlog
sudo -k
else
echo "cannot install /usr/local/bin/brewlog without sudo rights"
exit 1;
fi
if [ "$flag" -eq "1" ]; then
shopt -s nocasematch
fi
fi