-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfunctions.sh
executable file
·79 lines (65 loc) · 1.63 KB
/
functions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# shellcheck disable=SC1091,SC2034
ORANGE='\033[0;33m'
NC='\033[0m' # No Color
check_shell(){
[ -n "${BASH_VERSION}" ] && return
echo -e "${ORANGE}WARNING: These scripts are ONLY tested in a bash shell${NC}"
sleep "${SLEEP_SECONDS:-8}"
}
check_git_root(){
[ -n "${GIT_ROOT}" ] && return
if [ -d .git ] && [ -d scripts ]; then
GIT_ROOT=$(pwd)
export GIT_ROOT
echo "GIT_ROOT: ${GIT_ROOT}"
return
else
echo "Please run this script from the root of the git repo"
exit
fi
}
check_script_path(){
[ -n "${SCRIPT_DIR}" ] && return
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
export SCRIPT_DIR
echo "SCRIPT_DIR: ${SCRIPT_DIR}"
}
source_library(){
for file in "${SCRIPT_DIR}/library/"*.sh
do
if [ -f "$file" ] ; then
# shellcheck source=/dev/null
. "$file"
fi
done
}
setup_bin_path(){
[ -z ${GIT_ROOT+x} ] && return 1
BIN_PATH="${GIT_ROOT}/scratch/bin"
mkdir -p "${BIN_PATH}"
echo "${PATH}" | grep -q "${BIN_PATH}" || \
PATH="${BIN_PATH}:${PATH}"
export PATH
}
get_functions(){
# echo -e "loaded functions:\n"
sed -n '/(){/ {/^_/d; s/(){$//p}' "${SCRIPT_DIR}/"{library/*,functions}.sh | sort -u
}
is_sourced(){
if [ -n "$ZSH_VERSION" ]; then
case $ZSH_EVAL_CONTEXT in *:file:*) return 0;; esac
else # Add additional POSIX-compatible shell names here, if needed.
case ${0##*/} in dash|-dash|bash|-bash|ksh|-ksh|sh|-sh) return 0;; esac
fi
return 1 # NOT sourced.
}
check_shell
check_git_root
check_script_path
source_library
setup_bin_path
usage(){
echo "USAGE: get_functions"
}
is_sourced && usage