-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.sh
127 lines (105 loc) · 2.34 KB
/
wrapper.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
#!/bin/bash
function print_usage {
echo "Usage: `basename $0` [-v] ssh_url"
}
function err {
echo $1
# TODO: print sample config
}
function parse_config {
# ssh config
file="/Users/tom/.ssh/config"
# git users: lines starting with #username
users=($(awk '{ if (index($0, "#") == 1) { print $2 }}' $file))
user_count=${#users[*]}
if [ ${#users[*]} == 0 ]; then
err "You don't seem to have any user annotations"
exit -1
fi
# ssh hosts: lines following the identified user lines
host_lines=($(awk 'f{print;f=0} /#/{f=1}' $file))
host_line_count=${#host_lines[*]}
if [ $host_line_count -ne $(($user_count * 2)) ]; then
err "Hosts don't match with users"
exit -1
fi
# extract host names
for (( i=1; i<=$(( $host_line_count - 1)); i+=2))
do
hosts=(${hosts[@]} ${host_lines[$i]})
done
}
NO_ARGS=0
E_OPTERR=85
E_SYSERR=-1
verbose=false
if [ $# -eq $NO_ARGS ]; then
print_usage
exit $E_OPTERR
fi
while getopts ":h:v" option
do
case $option in
h) print_usage
exit 0
;;
v) echo "Enabling verbose mode"
verbose=true
;;
*) print_usage
exit $E_OPTERR
;;
esac
done
repo_url=$1
if [ -z ${repo_url+x} ]; then
print_usage
exit E_SYSERR
fi
# validate the repo url
repo_regex="git@(.+):(.+)\/.+\.git"
if [[ ! $repo_url =~ $repo_regex ]]; then
err "$repo_url doesn't seem to be a valid repository"
exit $E_SYSERR
fi
# ssh config
config="$HOME/.ssh/config"
if [ ! -f ${config} ]; then
err "Please create SSH config as: ${config}"
exit $E_SYSERR
fi
# a matrix of users and the associated hosts
declare -a users
declare -a hosts
# Identify users and hosts
parse_config
if [ "$verbose" = true ]; then
# print config
echo "Found mappings (user: host)"
users_count=${#users[*]}
for (( i=0; i<=$(( $users_count - 1)); i++))
do
echo " ${users[$i]}: ${hosts[$i]}"
done
fi
# find the corresponding ssh user
git_user="${BASH_REMATCH[2]}"
index=-1
for (( i=0; i<${#users[*]}; i++))
do
if [[ "$git_user" = "${users[$i]}" ]]; then
index=$i
break
fi
done
if [[ $index -lt 0 ]]; then
err "No such user found in ssh config: ${git_user}"
exit $E_SYSERR
fi
# replace the host
repo_url="${repo_url/${BASH_REMATCH[1]}/${hosts[$i]}}"
if [ "$verbose" = true ]; then
echo "Updated repo url: $repo_url"
fi
# clone the repo
git clone "$repo_url"