-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.sh
executable file
·118 lines (94 loc) · 1.76 KB
/
utils.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
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
brew_install() {
declare -r CMD="$3"
declare -r FORMULA="$2"
declare -r FORMULA_READABLE_NAME="$1"
if brew "$CMD" list "$FORMULA" &> /dev/null; then
print_success "$FORMULA_READABLE_NAME"
else
execute "brew $CMD install $FORMULA" "$FORMULA_READABLE_NAME"
fi
}
answer_is_yes() {
[[ "$REPLY" =~ ^[Yy]$ ]] \
&& return 0 \
|| return 1
}
ask() {
print_question "$1"
read
}
ask_for_confirmation() {
print_question "$1 (y/n) "
read -n 1
printf "\n"
}
ask_for_sudo() {
# Ask for the administrator password upfront
sudo -v &> /dev/null
# Update existing `sudo` time stamp until this script has finished
# https://gist.github.com/cowboy/3118588
while true; do
sudo -n true
sleep 60
kill -0 "$$" || exit
done &> /dev/null &
}
cmd_exists() {
command -v "$1" &> /dev/null
return $?
}
execute() {
eval "$1" &> /dev/null
print_result $? "${2:-$1}"
}
get_answer() {
printf "$REPLY"
}
is_git_repository() {
git rev-parse &> /dev/null
return $?
}
mkd() {
if [ -n "$1" ]; then
if [ -e "$1" ]; then
if [ ! -d "$1" ]; then
print_error "$1 - a file with the same name already exists!"
else
print_success "$1"
fi
else
execute "mkdir -p $1" "$1"
fi
fi
}
print_error() {
print_in_red " [✖] $1 $2\n"
}
print_in_green() {
printf "\e[0;32m$1\e[0m"
}
print_in_purple() {
printf "\e[0;35m$1\e[0m"
}
print_in_red() {
printf "\e[0;31m$1\e[0m"
}
print_in_yellow() {
printf "\e[0;33m$1\e[0m"
}
print_info() {
print_in_purple "\n $1\n\n"
}
print_question() {
print_in_yellow " [?] $1"
}
print_result() {
[ $1 -eq 0 ] \
&& print_success "$2" \
|| print_error "$2"
return $1
}
print_success() {
print_in_green " [✔] $1\n"
}