-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.sh
More file actions
191 lines (138 loc) · 2.57 KB
/
functions.sh
File metadata and controls
191 lines (138 loc) · 2.57 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
THIS="$(basename $0)"
THISDIR="$(cd $(dirname $0) && pwd)"
###
### Filters
###
unbom() {
# removes BOM from UTF-8 files
local file="$1"
if [[ -f $file && $(head -c 3 $file) == $'\xEF\xBB\xBF' ]]; then
mv $file $file.bak
tail -c +4 $file.bak > $file
fi
}
lowercase() {
echo "$1" | tr '[A-Z]' '[a-z]'
}
uppercase() {
echo "$1" | tr '[a-z]' '[A-Z]'
}
trim() {
local var
if [[ $# -eq 0 ]]; then
read -r var
else
var="$*"
fi
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
###
### Checks
###
command_exists() {
command -v "$1" >/dev/null 2>&1
}
eq_regexp() {
# compatibility replacement for [[ a =~ b ]]
local string="$1" regexp="$2"
echo "$string" | grep -qE "$regexp"
}
array_contains() {
# array_contains "needle" "${haystack[@]}"
# $1 is needle
# #2+ is haystack
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
###
### User prompting
###
ask() {
local question=$1 default=$2
if [[ -n $default ]]; then
question="$question [$default]"
fi
read -rp "$question "
if [[ -z $REPLY ]]; then
REPLY="$default"
fi
}
confirm() {
local question=$1 default=$(lowercase "$2")
if [[ $default == "y" ]]; then
prompt="[Y/n]"
elif [[ $default == "n" ]]; then
prompt="[y/N]"
else
die "WHAT?! confirm doesn't know '$default'"
fi
while true; do
ask "$question $prompt "
REPLY=$(lowercase "$REPLY")
if [[ $REPLY == "yes" ]]; then
REPLY=y
elif [[ $REPLY == "no" ]]; then
REPLY=n
elif [[ -z $REPLY ]]; then
REPLY="$default"
fi
if [[ $REPLY =~ [yn] ]]; then
break
fi
done
}
die() {
echo -e "${@}"
exit
}
###
### Git
###
git_in_repo() {
# whether pwd is in a git repo
git rev-parse --git-dir >/dev/null 2>&1
}
git_current_branch() {
git symbolic-ref --quiet --short HEAD
}
git_branch_exists() {
git show-ref --verify --quiet refs/heads/"$1"
}
git_current_hash() {
git rev-list --max-count=1 HEAD
}
git_dir() {
git rev-parse --git-dir
}
###
### OS Detection
###
os_is_mac() {
[[ $(uname -s) == "Darwin" ]]
}
os_is_linux() {
[[ $(uname -s) == "Linux" ]]
}
os_is_windows() {
# MSYSGIT
[[ $(uname -s) =~ MINGW* ]]
}
###
### SSH
###
ssh_start() {
local host="$1"
ssh -S "/tmp/%r@%h:%p.conn" -MNf "$host"
}
ssh_command() {
local host="$1" command="$2"
ssh -S "/tmp/%r@%h:%p.conn" -n "$host" "$command"
}
ssh_stop() {
local host="$1"
ssh -S "/tmp/%r@%h:%p.conn" -qn -O exit "$host"
}