-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.sh
334 lines (298 loc) · 9.56 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# create ctags file in current folder
function create-ctags()
{
ctags -nR --PHP-kinds=cfi --exclude='.git' --exclude=".svn" --exclude="cache" --exclude=".cache" --exclude="codecache" --exclude="*.phar" --exclude="log" --exclude="node_modules" --exclude="bower_components" --exclude=".tmp" --exclude="*.min.js" --exclude="modules-built" --exclude="phing" --exclude="sebastian" --regex-php='/^[ \t]*trait[ \t]+([a-z0_9_]+)/\1/t,traits/i' -f tags .
}
# traverse into every folder and pull rebase changes (but don't autostash local modifications)
function git-pull-rebase-all-folders()
{
find . -mindepth 1 -maxdepth 1 -type d -exec bash -c 'cd "$1"; echo "$1"; git pull --rebase --no-autostash' -- {} \;
}
function kubernetes-install()
{
echo "Downloading kubectl…"
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
echo "Making kubectl executable…"
chmod +x ./kubectl
./kubectl version
echo "Moving kubectl into /usr/local/bin/kubectl…"
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version
echo "Installing bash-completion…"
kubectl completion bash > kubectl_completion_bash
sudo mv kubectl_completion_bash /etc/bash_completion.d/kubectl
echo "Downloading kubelogin…"
curl -LO https://github.com/int128/kubelogin/releases/download/v$(curl -s 'https://api.github.com/repos/int128/kubelogin/releases/latest' | grep '"tag_name":' | cut -d'"' -f4 | cut -d'v' -f2)/kubelogin_linux_amd64.zip
echo "Extracting kubelogin and making it executable…"
unzip kubelogin_linux_amd64.zip
chmod +x ./kubelogin
sudo mv ./kubelogin /usr/local/bin/kubelogin
}
function kubernetes-lint()
{
if [ $# -ne 1 ]; then
echo "Error: Please provide a k8s yaml file or relative path to a folder with k8s yaml files."
return 1
fi
echo "Using strict kubernetes validation. For details see: https://kubeval.instrumenta.dev/"
echo ""
if [ -f $1 ] ; then
docker run --rm -i garethr/kubeval --strict < $1
else
docker run -it -v $(pwd)/$1:/microk8s garethr/kubeval --strict microk8s/*.yaml
fi
}
function docker-lint()
{
echo "Checking dockerfile. For errors/rules see: https://github.com/hadolint/hadolint#rules"
echo ""
docker run --rm -i hadolint/hadolint:latest-debian < ${1:-Dockerfile}
}
# curl -sS https://getcomposer.org/installer | php
function getcomposer()
{
local EXPECTED_SIGNATURE=$(wget https://composer.github.io/installer.sig -O - -q)
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
local ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');")
if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]
then
php composer-setup.php
local RESULT=$?
if [[ $RESULT -ne 0 ]] ; then
echo "INSTALLATION OF COMPOSER FAILED!"
else
mv composer.phar ~/bin/composer
echo "Composer installed: ~/bin/composer"
fi
else
>&2 echo 'ERROR: Invalid installer signature'
fi
rm composer-setup.php
}
# awk column print shorthand, e.g. print 2nd column: "df -h | fawk 2"
function fawk
{
local first="awk '{print "
local last="}' $2"
local cmd="${first}\$${1}${last}"
echo $cmd
eval $cmd
}
# remind with desktop notification (ubuntu only atm)
function remind()
{
if [ -n "$2" ]; then
sleep $1 && notify-send "$2" &
else
echo "Usage: remind 15m 'you gotta leave!'"
fi;
}
# list configured hosts from .ssh/config file
function ssh-list()
{
awk '$1 ~ /Host$/ { print $2 }' ~/.ssh/config
}
# extract a few known archive files via a common command
function extract-archive()
{
if [ $# -ne 1 ]
then
echo "Error: No archive file specified."
return 1
fi
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xvjf $1;;
*.tar.gz) tar xvzf $1;;
*.bz2) bunzip2 $1;;
*.rar) unrar x $1;;
*.gz) gunzip $1;;
*.tar) tar xvf $1;;
*.tbz2) tar xvjf $1;;
*.tgz) tar xvzf $1;;
*.zip) unzip $1;;
*.Z) uncompress $1;;
*.7z) 7z x $1;;
*) echo "'$1' cannot be extracted via extract-archive command.";;
esac
else
echo "'$1' is not a valid file."
fi
}
# quick info about data files
function i()
{
(head -n 5;tail -n 5) < "$1" | column -t
}
# resize remote image to local file with given width via curl/webservice
function resize-web-image()
{
if [ "" = "$3" ]; then
echo "Usage: resize-web-image '<image-url>' <width> <outputfile>";
else
local imageUrl=$1;
local width=$2;
local outputfile=$3;
curl -o $outputfile http://src.sencha.io/$width/$imageUrl;
fi
}
# extract manifest from the given jar file and print it to stdout
function jar-print-manifest()
{
example 'jar_manifest lib/foo.jar'
if [ $# -ne 1 ]
then
echo "Usage: jar-manifest foo/bar.jar"
return 1
fi
if [ -f $1 ]; then
unzip -c $1 META-INF/MANIFEST.MF
else
echo "'$1' is not a valid file."
fi
}
# checks whether the given URL is down for everyone or just you ;-)
function down4me()
{
if [ $# -ne 1 ]
then
echo "Usage: down4me <website-url>"
return 1
fi
curl -s "http://www.downforeveryoneorjustme.com/$1" | sed '/just you/!d;s/<[^>]*>//g'
}
# try to determine the public ip address
function my-public-ip()
{
local response=$(curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+')
echo -e "Your public IP address is: \033[1;32m$response\033[0m"
}
# backup given file with the current timestamp
function backup-with-timestamp()
{
if [ $# -ne 1 ]
then
echo "Usage: backup-with-timestamp <path>"
return 1
fi
local filename=$1
local filetime=$(date +%Y%m%d_%H%M%S)
cp ${filename} ${filename}_${filetime}
}
function matrix-effect()
{
echo -e "\e[1;40m";
clear;
while :
do
echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 ));
sleep 0.05;
done | gawk '{
letters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$%^&*()";
c=$4;
letter=substr(letters,c,1);
a[$3]=0;
for (x in a) {
o=a[x];
a[x]=a[x]+1;
printf "\033[%s;%sH\033[2;32m%s",o,x,letter;
printf "\033[%s;%sH\033[1;37m%s\033[0;0H",a[x],x,letter;
if (a[x] >= $1) {
a[x]=0;
}
}
}'
}
# serve <port> (defaults to 9999)
function serve() {
local usage_text="Usage: serve [port]
Serves the current directory via HTTP on the specified port (defaults to 9999)"
local help_pattern="-h|--help"
if [[ $1 =~ $help_pattern ]]; then
echo "$usage_text"
return 0
fi
local port="${1:-9999}"
local port_pattern="^[0-9]+$"
if ! [[ $port =~ $port_pattern ]]; then
echo "Invalid port argument: \"$1\""
return 1
fi
sleep 1 && open "http://localhost:${port}/" &
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}
# start php server from current directory with port (defaults to 4000; requires PHP v5.4+)
function phpserver() {
local port="${1:-4000}"
local ip=$(ifconfig | grep -i inet[^6] | cut -d: -f2 | cut -d' ' -f1 | tail -n 1)
sleep 1 && open "http://${ip}:${port}/" &
php -S "${ip}:${port}"
}
# calculate
function calc() {
local result=""
result="$(printf "scale=5;$*\n" | bc -l | tr -d '\\\n')"
if [[ "$result" == *.* ]]; then
echo $result | sed -e 's/^\./0./' -e 's/^-\./-0./' -e 's/0*$//;s/\.$//'
else
echo $result
fi
}
# create a data uri from a file
function dataurl() {
local mimeType=$(file -b --mime-type "$1")
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8"
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
}
# compare original and gzipped file sizes
function gz() {
local orig_size=$(wc -c < "$1")
local gzip_size=$(gzip -c "$1" | wc -c)
local size_ratio=$(echo "scale=3; $gzip_size * 100 / $orig_size" | bc -l)
printf "orig: %d bytes\n" "$orig_size"
printf "gzip: %d bytes (%s%%)\n" "$gzip_size" "$size_ratio"
}
# escape UTF-8 characters into their 3-byte format
function escape() {
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u)
if [ -t 1 ]; then
echo
fi
}
# decode \x{ABCD} unicode escape sequences
function unidecode() {
perl -e "binmode(STDOUT, ':utf8'); print \"$@\""
if [ -t 1 ]; then
echo
fi
}
# get a character's unicode code point
function codepoint() {
perl -e "use utf8; print sprintf('U+%04X', ord(\"$@\"))"
if [ -t 1 ]; then
echo
fi
}
# find 10 most frequently used words in a given text file
function words-most-freq() {
if [ $# -ne 1 ]
then
echo "Usage: words-most-freq <path-to-text-file>"
return 1
fi
cat $1 | tr -s '[:space:]' '\n' | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -nr | head -10
}
function randpw() {
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
}
# pretty print JSON strings or files
function json() {
if [ -t 0 ]; then
python -mjson.tool <<< "$*" | pygmentize -l javascript
else
python -mjson.tool | pygmentize -l javascript
fi
}
# vim: set ts=4 sw=4 tw=0 et :