Skip to content

Commit 3f4e962

Browse files
committed
Caching functions added, hgrc updated to use opendiff on OS X, distrib errors with usage
1 parent fe401b2 commit 3f4e962

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

bash.d/01-functions.sh

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function path_inject() {
88
(( $DEBUG )) && echo;
99
}
1010

11-
function root_login () {
11+
function root_login() {
1212
local timeout=1800;
1313
local keyActive=`ssh-add -l |grep 'administrator.dsa'|wc -l`;
1414

@@ -40,6 +40,111 @@ function root_login () {
4040

4141
}
4242

43+
# Figure out how to checksum
44+
type shasum &> /dev/null
45+
if [[ $? -eq 0 ]]; then
46+
CHECKSUM_PROG=shasum
47+
else
48+
type sha1sum &> /dev/null
49+
if [[ $? -eq 0 ]]; then
50+
CHECKSUM_PROG=sha1sum
51+
else
52+
CHECKSUM_PROG=md5sum
53+
fi
54+
fi
55+
# CACHE UUID
56+
[ -z "$CACHE_UUID" ] && export CACHE_UUID=$(head -1 /dev/urandom |$CHECKSUM_PROG|awk '{print $1}');
57+
# Cache cleaning
58+
find $HOME/.cache -type f -mtime +1 -exec rm -f {} \; &> /dev/null
59+
find $HOME/.cache -type d -empty -exec rmdir -f {} \; &> /dev/null
60+
61+
function cache_reset() {
62+
if [ -d "$HOME/.cache/$CACHE_UUID" ]; then
63+
find "$HOME/.cache/$CACHE_UUID" -maxdepth 1 -type f -exec rm -f {} \;
64+
fi
65+
}
66+
67+
function cache_protected() {
68+
# Variables
69+
local check_name=$1
70+
local check=$2
71+
local timeout=3600
72+
73+
# Setup cache directory
74+
[ ! -d "$HOME/.cache" ] && mkdir "$HOME/.cache";
75+
76+
# Setup a UUID for this session
77+
[ ! -d "$HOME/.cache/$CACHE_UUID" ] && mkdir "$HOME/.cache/$CACHE_UUID";
78+
79+
# Only do this if cache is stale
80+
cachefile="$HOME/.cache/$CACHE_UUID/$check_name"
81+
if [ -e "$cachefile" ]; then
82+
cachetime=$(stat -r $cachefile|awk '{print $9}');
83+
now=$(date +%s);
84+
oldest=$(($now - $timeout))
85+
cachevalue=$(cat $cachefile);
86+
if [[ $cachetime -gt $oldest ]]; then
87+
echo $cachevalue;
88+
return;
89+
fi
90+
fi
91+
92+
# Run the check, get the value
93+
(($DEBUG)) && echo " + cache_protected() is running '$check'";
94+
value=$($check 2>/dev/null);
95+
rc=$?;
96+
97+
# store the and return
98+
echo "$rc $value" > $cachefile;
99+
echo "$rc $value";
100+
}
101+
102+
function ip_is_in() {
103+
local file=$1
104+
105+
result=$(cache_protected "get_external_ip" "curl -XGET http://icanhazip.com/")
106+
ip=$(echo $result|cut -d' ' -f2)
107+
108+
grep $ip $file &> /dev/null
109+
rc=$?
110+
111+
echo $rc;
112+
}
113+
114+
function check_proxy_wrapper() {
115+
# Set a proxy in the environment variable MY_PROXY
116+
program="$1"
117+
args=("$@")
118+
unset args[0]
119+
120+
result=$(ip_is_in $HOME/.work_proxies);
121+
if [ "$result" == "0" ]; then
122+
(($DEBUG)) && echo " + setting proxies to $MY_PROXY";
123+
export HTTP_PROXY="http://$MY_PROXY/"
124+
export http_proxy="http://$MY_PROXY/"
125+
export https_proxy="https://$MY_PROXY/"
126+
else
127+
(($DEBUG)) && echo " + removing proxies";
128+
unset HTTP_PROXY
129+
unset http_proxy
130+
unset https_proxy
131+
fi
132+
133+
command $program "${args[@]}";
134+
}
135+
136+
function ssh_check_proxy() {
137+
result=$(ip_is_in $HOME/.work_proxies);
138+
if [ "$result" == "0" ]; then
139+
export SSH_PROXY="int"
140+
else
141+
export SSH_PROXY="ext"
142+
fi
143+
(($DEBUG)) && echo " + ssh is using $SSH_PROXY proxies";
144+
145+
command ssh $@
146+
}
147+
43148
function contents() {
44149
if [ -f "$1" ] && [ -r "$1" ]; then
45150
file_lines=`wc -l $1 | awk '{print $1}'`;

hgrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
[ui]
22
username = "Brad Lhotsky <[email protected]>"
33
verbose = True
4+
merge = filemerge
5+
6+
[merge-tools]
7+
filemerge.executable = opendiff
8+
filemerge.args = $local $other -ancestor $base -merge $output

support/distrib.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ if [ ! -f ~/.distrib_hosts ]; then
1313
fi;
1414

1515
## Setups
16-
if [ "$MODE" == "movein" ]; then
16+
if [ -z $MODE ]; then
17+
echo "usage: $0 [movein|local]"
18+
exit 1;
19+
elif [ "$MODE" == "movein" ]; then
1720
if [ ! -x "$MOVEIN" ]; then
1821
echo "!! Could not execute $MOVEIN !!"
1922
exit 1;

0 commit comments

Comments
 (0)