-
Notifications
You must be signed in to change notification settings - Fork 5
/
alf-old
330 lines (303 loc) · 7.77 KB
/
alf-old
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
#!/usr/bin/env bash
# ---------------------------------------------------------------
# Alf - Your Little Bash Alias Friend
# ---------------------------------------------------------------
usage() {
if $LONG_USAGE; then
printf "alf %s - Your Little Bash Alias Friend\n\n" "$VERSION"
fi
printf "Usage:\n"
printf " alf c|connect REPO [-y]\n"
if $LONG_USAGE; then
printf " Connect to a remote git repository.\n"
printf " REPO can be:\n"
printf " - Your username on GitHub. In this case, we will assume the\n"
printf " repository is named 'alf-conf'\n"
printf " - Your username/repo on GitHub\n"
printf " - Any other full URL to a repository\n"
printf " In case the -y flag is specified, the operation will be\n"
printf " executed without prompting for confirmation.\n\n"
fi
printf " alf d|download\n"
if $LONG_USAGE; then
printf " Perform 'git pull' on a previously connected repo\n\n"
fi
printf " alf u|upload\n"
if $LONG_USAGE; then
printf " Perform 'git commit' and 'git push' on a previously connected\n"
printf " repo\n\n"
fi
printf " alf g|generate\n"
if $LONG_USAGE; then
printf " Generate aliases from the config file in the connected repo\n"
printf " to stdout\n\n"
fi
printf " alf s|save\n"
if $LONG_USAGE; then
printf " Generate aliases and save to ~/.bash_aliases\n\n"
fi
printf " alf e|edit\n"
if $LONG_USAGE; then
printf " Open your alf.conf for editing\n\n"
fi
printf " alf w|which CODE [SUBCODE]\n"
if $LONG_USAGE; then
printf " Shows the alias command\n\n"
fi
printf " alf -u|--upgrade|upgrade\n"
if $LONG_USAGE; then
printf " Upgrade alf to latest version\n\n"
fi
printf " alf -h|--help|help\n"
if $LONG_USAGE; then
printf " Show this message\n\n"
fi
printf " alf -v|--version|version\n"
if $LONG_USAGE; then
printf " Show version number\n\n"
fi
}
find_config() {
REPO_PATH="$PWD/alf-conf"
CONFIG_FILE="alf.conf"
if [[ -f $RC_FILE ]]; then
REPO_PATH=$(<"$RC_FILE")
CONFIG_FILE=$REPO_PATH/alf.conf
fi
if [[ ! -f $CONFIG_FILE ]]; then
printf "ERROR: Cannot find config file\n\n"
echo "You should either:"
echo "- Run this command in a folder with 'alf.conf' file, or"
echo "- Run 'alf connect' to properly connect to a remote config"
exit 1
fi
}
generate_last_cmd() {
if [[ -n $lastcmd ]]; then
if [[ $lastcmd =~ ^! ]]; then
fullcmd="echo this alias requires a subcommand"
elif [[ $lastcmd =~ \$ ]]; then
fullcmd=$lastcmd
else
fullcmd="$lastcmd \"\$@\""
fi
if [[ $cond = "if" ]]; then
echo " $fullcmd"
echo "}"
else
echo " else"
echo " $fullcmd"
echo " fi"
echo "}"
cond="if"
fi
fi
}
generate() {
regex="^( *)([a-z0-9\-]+): *(.+)$"
cond="if"
lastcmd=""
find_config
echo "# This file was automatically generated by alf"
echo "# https://github.com/dannyben/alf"
while IFS= read -r line || [ -n "$line" ]; do
if [[ $line =~ $regex ]]; then
indent="${BASH_REMATCH[1]}"
if [[ -z $indent ]]; then
ali1="${BASH_REMATCH[2]}"
cmd1="${BASH_REMATCH[3]}"
local_regex="^$ali1( +|$)"
if [[ $cmd1 =~ $local_regex ]]; then
cmd1="command $cmd1"
fi
unset ali2 cmd2
generate_last_cmd
lastcmd=$cmd1
else
ali2="${BASH_REMATCH[2]}"
cmd2="${BASH_REMATCH[3]}"
fi
if [[ -n $ali2 ]]; then
echo " $cond [[ \$1 = \"$ali2\" ]]; then"
echo " shift"
if [[ $cmd2 =~ ^! ]]; then
cmd=${cmd2:1}
elif [[ $cmd1 =~ ^! ]]; then
cmd="$cmd2"
else
cmd="$cmd1 $cmd2"
fi
if [[ $cmd2 =~ \$ ]]; then
echo " $cmd"
else
echo " $cmd \"\$@\""
fi
cond="elif"
else
echo ""
echo "unalias $ali1 1>/dev/null 2>&1"
echo "$ali1() {"
fi
fi
done < "$CONFIG_FILE"
generate_last_cmd
}
make_full_repo_path() {
if [[ -z $REPO ]]; then
echo "ERROR: Please provide a repo name or URL"
exit 1
elif [[ $REPO =~ ':' ]]; then
REPO_URL=$REPO
elif [[ $REPO =~ '/' ]]; then
PARTIAL_GITHUB_URL=1
REPO_URL="$REPO.git"
else
PARTIAL_GITHUB_URL=1
REPO_URL="$REPO/alf-conf.git"
fi
}
download() {
find_config
if [[ ! -f $RC_FILE ]]; then
echo "Cannot find $RC_FILE"
echo "Please connect alf to a repository first"
exit 1
fi
pushd "$REPO_PATH" > /dev/null
echo "Pulling from repository to $REPO_PATH"
git pull
popd > /dev/null
save
}
upload() {
find_config
if [[ ! -f $RC_FILE ]]; then
echo "Cannot find $RC_FILE"
echo "Please connect alf to a repository first"
exit 1
fi
pushd "$REPO_PATH" > /dev/null
echo "Pushing $REPO_PATH to repository"
git commit -am "automatic push"
git push
popd > /dev/null
}
connect() {
make_full_repo_path
if [[ $FORCE != "-y" ]]; then
echo "This operation will:"
echo ""
if [[ ! -d ./alf-conf ]]; then
echo " clone $REPO_URL"
echo " to ./alf-conf"
fi
echo " write $PWD/alf-conf"
echo " to $RC_FILE"
echo ""
if [[ -n "$PARTIAL_GITHUB_URL" ]]; then
echo "Would you like to connect to GitHub via SSH or HTTPS?"
echo ""
printf "[S]SH, [H]TTPS or [A]bort (default): "
read -r answer
if [[ $answer =~ [Ss] ]]; then
REPO_URL="git@github.com:$REPO_URL"
answer=y
echo ""
echo "Using $REPO_URL"
elif [[ $answer =~ [Hh] ]]; then
REPO_URL="https://github.com/$REPO_URL"
answer=y
echo ""
echo "Using $REPO_URL"
fi
else
printf "Continue? [yN] "
read -r answer
fi
else
answer=y
fi
if [[ $answer =~ [Yy] ]]; then
if [[ -d ./alf-conf ]]; then
echo "Skipping clone, directory already exists"
else
git clone "$REPO_URL" ./alf-conf
fi
echo "$PWD/alf-conf" > "$RC_FILE"
echo "Storing location in $RC_FILE"
else
echo "Aborting"
exit
fi
save
}
show() {
if [[ -z $CODE ]]; then
echo "ERROR: Please provide a code"
exit 1
fi
find_config
regex_exact_code="^($CODE): *(.+)$"
regex_exact_subcode="^( +)($SUBCODE): *(.+)$"
cmd1=""
while IFS= read -r line || [ -n "$line" ]; do
if [[ $line =~ $regex_exact_code ]]; then
cmd1="${BASH_REMATCH[2]}"
if [[ -z $SUBCODE ]]; then
echo "$cmd1"
exit 0
fi
elif [[ -n $cmd1 ]]; then
if [[ $line =~ $regex_exact_subcode ]]; then
cmd2="${BASH_REMATCH[3]}"
if [[ $cmd2 =~ ^! ]]; then
echo "${cmd2:1}"
else
echo "$cmd1 $cmd2"
fi
exit 0
fi
fi
done < "$CONFIG_FILE"
echo "Error: No such alias: $CODE $SUBCODE"
exit 1
}
save() {
find_config
echo "Saving to $ALIASES_FILE"
generate > "$ALIASES_FILE"
echo "To apply new aliases to the current session, run:"
echo "$ source ~/.bash_aliases"
}
edit() {
find_config
${EDITOR:-vi} "$CONFIG_FILE"
}
upgrade() {
bash <(curl -s https://raw.githubusercontent.com/DannyBen/alf/master/setup)
}
run() {
case "$1" in
c | connect ) REPO=$2; FORCE=$3; connect ;;
d | download ) download ;;
g | generate ) generate ;;
s | save ) save ;;
u | upload ) upload ;;
e | edit ) edit ;;
w | which ) CODE=$2; SUBCODE=$3; show ;;
-u | --upgrade | upgrade ) upgrade ;;
-h | --help | help ) LONG_USAGE=true; usage ;;
-v | --version | version ) echo "$VERSION" ;;
* ) usage ;;
esac
}
initialize() {
VERSION="0.3.2"
LONG_USAGE=false
REPO=${ALF_REPO:=''}
ALIASES_FILE=${ALF_ALIASES_FILE:=~/.bash_aliases}
RC_FILE=${ALF_RC_FILE:=~/.alfrc}
set -e
}
initialize
run "$@"