|
| 1 | +#----------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Basescript function |
| 4 | +# |
| 5 | +# The basescript functions were designed to work as abstract function, |
| 6 | +# so it could be used in many different contexts executing specific job |
| 7 | +# always remembering Unix concept DOTADIW - "Do One Thing And Do It Well" |
| 8 | +# |
| 9 | +# Developed by |
| 10 | +# Evert Ramos <evert.ramos@gmail.com> |
| 11 | +# |
| 12 | +# Copyright Evert Ramos |
| 13 | +# |
| 14 | +#----------------------------------------------------------------------- |
| 15 | +# |
| 16 | +# Be carefull when editing this file, it is part of a bigger script! |
| 17 | +# |
| 18 | +# Basescript - https://github.com/evertramos/basescript |
| 19 | +# |
| 20 | +#----------------------------------------------------------------------- |
| 21 | + |
| 22 | +#----------------------------------------------------------------------- |
| 23 | +# This function has one main objective: |
| 24 | +# 1. Call other functions in the environment passing parameter through |
| 25 | +# |
| 26 | +# You must/might inform the parameters below: |
| 27 | +# 1. Function name |
| 28 | +# 2. [optional] (default: null) you can pass up to 4 parameters ] |
| 29 | +# |
| 30 | +#----------------------------------------------------------------------- |
| 31 | + |
| 32 | +run_function() { |
| 33 | + |
| 34 | + [[ $1 == "" ]] && echoerr "You must inform an argument to the function '${FUNCNAME[0]}', \nplease check the docs." |
| 35 | + |
| 36 | + # Check $SILENT mode |
| 37 | + if [[ "$SILENT" == true ]]; then |
| 38 | + if [[ ! -z $5 ]]; then |
| 39 | + $1 "$2" "$3" "$4" "$5" |
| 40 | + elif [[ ! -z $4 ]]; then |
| 41 | + $1 "$2" "$3" "$4" |
| 42 | + elif [[ ! -z $3 ]]; then |
| 43 | + $1 "$2" "$3" |
| 44 | + elif [[ ! -z $2 ]]; then |
| 45 | + $1 "$2" |
| 46 | + else |
| 47 | + $1 |
| 48 | + fi |
| 49 | + else |
| 50 | + echo "${yellow}[start]---------------------------------------------------------------${reset}" |
| 51 | + |
| 52 | + # Call the specified function |
| 53 | + if [[ -n "$(type -t "$1")" ]] && [[ "$(type -t "$1")" = function ]]; then |
| 54 | + echo "${cyan}...running function \"${1}\":${reset}" |
| 55 | + if [[ ! -z $5 ]]; then |
| 56 | + $1 "$2" "$3" "$4" "$5" |
| 57 | + elif [[ ! -z $4 ]]; then |
| 58 | + $1 "$2" "$3" "$4" |
| 59 | + elif [[ ! -z $3 ]]; then |
| 60 | + $1 "$2" "$3" |
| 61 | + elif [[ ! -z $2 ]]; then |
| 62 | + $1 "$2" |
| 63 | + else |
| 64 | + $1 |
| 65 | + fi |
| 66 | + else |
| 67 | + echo "${red}----------------------------------------------------------------------${reset}" |
| 68 | + echo "${red}|${reset}" |
| 69 | + echo "${red}| [ERROR] Function \"$1\" not found!${reset}" |
| 70 | + echo "${red}|${reset}" |
| 71 | + echo "${red}----------------------------------------------------------------------${reset}" |
| 72 | + echo "${yellow}[ended with ${red}[ERROR]${yellow}]--------------------------------------------------${reset}" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + |
| 76 | + # Show result from the function execution |
| 77 | + if [[ $? -ne 0 ]]; then |
| 78 | + echo "${red}----------------------------------------------------------------------${reset}" |
| 79 | + echo "${red}|${reset}" |
| 80 | + echo "${red}| Ups! Something went wrong...${reset}" |
| 81 | + echo "${red}|${reset}" |
| 82 | + printf "${red}| ${MESSAGE//\\n/\\n|}${reset}" |
| 83 | + echo |
| 84 | + echo "${red}|${reset}" |
| 85 | + echo "${red}----------------------------------------------------------------------${reset}" |
| 86 | + echo "${yellow}[ended with ${red}ERROR${yellow}/WARNING ($?)-----------------------------------------${reset}" |
| 87 | + exit 1 |
| 88 | + else |
| 89 | + echo "${green}>>> Success!${reset}" |
| 90 | + fi |
| 91 | + |
| 92 | + echo "${yellow}[end]-----------------------------------------------------------------${reset}" |
| 93 | + echo |
| 94 | + fi |
| 95 | +} |
0 commit comments