polish for upcoming changes (its gonna be huge)

This commit is contained in:
koksnuss 2019-07-25 13:50:25 +02:00
parent d30f95f3b4
commit 29cb17026c

67
bsf
View File

@ -3,7 +3,7 @@
# Bash Framework # Bash Framework
# Some useful bash functions to make things easier. # Some useful bash functions to make things easier.
# #
# Copyright (C) 2017-2018 willipink.eu # Copyright (C) 2017-2019 willipink.eu
# Author Moritz Münch moritzmuench@mailbox.org # Author Moritz Münch moritzmuench@mailbox.org
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
@ -20,6 +20,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# Variables
# supress colors if used to write to a log
LOG=True LOG=True
for arg in $*; do for arg in $*; do
if [[ "$arg" == "--log" ]]; then if [[ "$arg" == "--log" ]]; then
@ -28,8 +32,7 @@ for arg in $*; do
done done
# VARIABLES ## colors (use ANSI escape codes)
## COLORS (use ANSI escape codes)
if [[ "$LOG" == True ]]; then if [[ "$LOG" == True ]]; then
normal () { printf '\033[0m' ; } normal () { printf '\033[0m' ; }
red () { printf '\033[0;31m' ; } red () { printf '\033[0;31m' ; }
@ -71,22 +74,32 @@ else
fi fi
# FUNCTIONS
# Print somethong to STDOUT. Use printf for safety purposes, see http://www.etalabs.net/sh_tricks.html # Functions
# @param string what to say
say () { say () {
# Print something to stdout.
# Use printf for safety purposes, see http://www.etalabs.net/sh_tricks.html
# @param what to print
printf %s\\n "$*" printf %s\\n "$*"
return 0 return 0
} }
# Print to Stdout. Do not finish with a linebreak
sayn () { sayn () {
# Like say without linebreak.
# @param what to print
printf %s "$*" printf %s "$*"
return 0 return 0
} }
# quick styler, usage "style *color*"
sayc() { sayc() {
# quick styler, usage "style *color*"
if [[ "$1" == "" ]]; then if [[ "$1" == "" ]]; then
blue blue
else else
@ -98,6 +111,7 @@ sayc() {
normal normal
return 0 return 0
} }
saycn() { saycn() {
$1 $1
sayn " ::: " sayn " ::: "
@ -107,24 +121,21 @@ saycn() {
return 0 return 0
} }
# check if user is root
# @return true if user is root, else false
root () { root () {
# Exit immediately if user is not root
# Usage: root
if [[ "$EUID" -ne 0 ]]; then if [[ "$EUID" -ne 0 ]]; then
say say
sayc red "You need to be root." sayc red "You need to be root."
exit 1 exit 1
else
say
sayc green "You are root."
return 0
fi fi
} }
# Check if last call had errors. If so, print error message and exit.
ok() { ok() {
# Check if last call had errors. If so, print error message and exit.
# Usage: ok <command>
if [[ "$?" -ne 0 ]]; then if [[ "$?" -ne 0 ]]; then
sayc red "ERROR, aborting." sayc red "ERROR, aborting."
exit 1 exit 1
@ -134,25 +145,17 @@ ok() {
fi fi
} }
# Do a job and check if it ran successfull.
# @param $*: command to execute
try() { try() {
$* # Do a job and check if it ran successfull.
if [[ "$?" -ne 0 ]]; then # Usage: try <command>
sayc red "ERROR, aborting"
say $* && ok
exit 1
else
sayc green "done"
return 0
fi
} }
# Do a job, write a message, color it and check if it ran successfull.
# @param $1 color
# @param $2 message
# @param $3 command
trycm() { trycm() {
# Do a job, write a message, color it and check if it ran successfull.
# Usage: trycmd <color> <message> <command>
if [[ "$2" != "" ]]; then if [[ "$2" != "" ]]; then
say say
sayc "$1" "$2" sayc "$1" "$2"