-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-lib.sh
110 lines (101 loc) · 3.11 KB
/
bash-lib.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
#SC2059 is disabled to have POSIX compliant colour text.
#shellcheck disable=SC2059
export C_GREEN="\\033[32m"
export C_PINK="\\033[35m"
export C_BLUE="\\033[94m"
export C_GOLD="\\033[33m"
export C_CYAN="\\033[36m"
export C_REG="\\033[0;39m"
export C_BLACK="\\e[30m"
export B_WHITE="\\e[107m"
export B_GREY="\\e[47m"
export B_BLUE_LIGHT="\\e[104m"
run_task () {
local __command=${1}
eval "${__command}" 2>/dev/null &
pid=$!
spin[0]=". "
spin[1]=".. "
spin[2]="... "
spin[3]=".... "
i=0
while kill -0 $pid 2>/dev/null || [ $i -ne 3 ]
do
i=$(( (i+1) %4 ))
announce "${spin[$i]}"
sleep .1
done
printf "${C_GREEN}Done!${C_REG}\n"
}
ask_permission () {
announce "${1}" 0
printf " ${C_REG}(y/n): "
while true
do
if [ "${auto_approve}" ]; then ans="y" && echo "y (auto)"; else read -r ans; fi
case ${ans} in
[yY]* ) break;;
[nN]* ) announce "${3}" 4; exit;;
* ) announce "Please enter y or n (y/n):" 3;;
esac
done
}
announce () {
local __message=${1}
local __kind=${2}
if [[ "$__kind" -eq "0" ]]; then
printf "\r${C_GREEN}[INFO]${C_REG} ${__message}${C_REG}\n"
elif [[ "$__kind" -eq "1" ]]; then
printf "\r${C_PINK}[INFO]${C_REG} ${__message}${C_REG}\n"
elif [[ "$__kind" -eq "2" ]]; then
printf "\r${C_GOLD}[WARN]${C_REG} ${__message}${C_REG}\n"
elif [[ "$__kind" -eq "3" ]]; then
printf "\r${C_CYAN}[USER]${C_REG} ${__message}${C_REG}\n"
elif [[ "$__kind" -eq "4" ]]; then
printf "\r${C_PINK}[ERRO]${C_REG} ${__message}${C_REG}\n"
fi
}
check_deps () { #this command expects an associative array of binary_name: brew package
# Example:
# $ declare -A deps=( ["gcloud"]="google-cloud-sdk" )
# $ check_deps "$(declare -p deps)"
eval "declare -A __deps=${1#*=}"
local __resp
for dep in "${!__deps[@]}"; do
command -v "${dep}" &> /dev/null
local __resp=$?
if [[ $__resp -ne 0 ]]; then
ask_permission "${dep} not found. Should we install it?" "We cannot proceed without ${dep}"
install_dep "${__deps[$dep]}"
fi
done
announce "Dependency check complete!\n" 0
}
install_dep() {
local __target=${1}
if [[ "$OSTYPE" == "darwin"* ]]; then
brew install "${__target}"
elif [[ "$OSTYPE" == "linux-gnu" ]]; then
announce "Linux currently not supported. Exiting..." 1
exit 1
else
echo "${C_PINK}[ERRO]${C_REG} "
announce "Unsupported OS. Exiting..." 1
exit 1
fi
}
keychain_del () {
local __account=${1}; local __service=${2}
security delete-generic-password -a "${__account}" -s "${__service}" > /dev/null 2>&1
}
keychain_get () {
local __account=${1}; local __service=${2}
security find-generic-password -a "${__account}" -s "${__service}" -w 2> /dev/null
}
keychain_set () {
local __account=${1}; local __service=${2}; local __value=${3};
keychain_del "${__account}" "${__service}" || true
sleep 0.5
security add-generic-password -a "${__account}" -s "${__service}" -w "${__value}" #> /dev/null 2>&1
}