-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions
113 lines (85 loc) · 2.76 KB
/
.functions
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
#!/bin/bash
##############################################################################
# functions
# -----------
# Usefull bash functions
#
# :authors: Sébastien BARON, @cbastienbaron
# :date: 18 March 2018
# :version: 0.0.1
##############################################################################
# run command in the background
background() {
[[ -z "$@" ]] && return 1
"$@" &
}
# serve PWD as web server
function serve() {
# serve [port] [directory]
local port=${1:-8000}
local dir=$2
xdg-open "http://localhost:$port"
[[ -n "$dir" ]] && pushd -q $dir
if (( $+commands[python] )); then
python -m SimpleHTTPServer $port
else
php -S "0.0.0.0:$port"
fi
[[ -n "$dir" ]] && popd -q
}
# check if uri is up
isup() {
local uri=$1
if curl -s --head --request GET "$uri" | grep "200 OK" > /dev/null ; then
notify-send --urgency=critical "$uri is down"
else
notify-send --urgency=low "$uri is up"
fi
}
# Create a new directory and enter it
mkd() {
mkdir -p "$@"
cd "$@" || exit
}
# Run `dig` and display the most useful info
digga() {
dig +nocmd "$1" any +multiline +noall +answer
}
# Call from a local repo to open the repository on github/bitbucket in browser
# Modified version of https://github.com/zeke/ghwd
repo() {
# Figure out github repo base URL
local base_url
base_url=$(git config --get remote.origin.url)
base_url=${base_url%\.git} # remove .git from end of string
# Fix [email protected]: URLs
base_url=${base_url//git@github\.com:/https:\/\/github\.com\/}
# Fix git://github.com URLS
base_url=${base_url//git:\/\/github\.com/https:\/\/github\.com\/}
# Fix [email protected]: URLs
base_url=${base_url//[email protected]:/https:\/\/bitbucket\.org\/}
# Fix [email protected]: URLs
base_url=${base_url//git@gitlab\.com:/https:\/\/gitlab\.com\/}
# Validate that this folder is a git folder
if ! git branch 2>/dev/null 1>&2 ; then
echo "Not a git repo!"
exit $?
fi
# Find current directory relative to .git parent
full_path=$(pwd)
git_base_path=$(cd "./$(git rev-parse --show-cdup)" || exit 1; pwd)
relative_path=${full_path#$git_base_path} # remove leading git_base_path from working directory
# If filename argument is present, append it
if [ "$1" ]; then
relative_path="$relative_path/$1"
fi
# Figure out current git branch
# git_where=$(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
git_where=$(command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
# Remove cruft from branchname
branch=${git_where#refs\/heads\/}
[[ $base_url == *bitbucket* ]] && tree="src" || tree="tree"
url="$base_url/$tree/$branch$relative_path"
echo "Calling $(type open) for $url"
open "$url" &> /dev/null || (echo "Using $(type open) to open URL failed." && exit 1);
}