-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp
executable file
·79 lines (64 loc) · 1.97 KB
/
warp
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
#!/bin/bash
warp() {
# if a first argument is supplied, then use it as the warp file location
# otherwise, use the default "$HOME/.warp" location
local SOURCE="${1:-$HOME/.warp}"
# ensure SOURCE file exists
if [ ! -f "$SOURCE" ]; then
echo "$SOURCE does not exist..."
return 1
fi
# cleanup TARGET file
local TARGET=$HOME/.picked
if [ -e "$TARGET" ]; then
rm "$TARGET"
fi
# make a selection, with vim
cat "$SOURCE" | vim -c "setlocal noreadonly" \
-c "setlocal cursorline" \
-c "setlocal number" \
-c "nnoremap <buffer> <CR> V:w! ~/.picked<CR>:qa!<CR>" \
-c "vnoremap <buffer> <CR> :w! ~/.picked<CR>:qa!<CR>" \
-R -
# nothing was selected, abort
if [ ! -e "$TARGET" ]; then
return
fi
# transform seletion in ssh command
local SSH=${SSH:-ssh}
local COMMAND=""
if [[ $(cat "$TARGET" | wc -l) ]]; then
line=$(head -n 1 $TARGET)
CONFIG=(`echo ${line}`)
if [[ ${CONFIG[2]} ]]; then
pass=$(printf '%q' "${CONFIG[2]}")
eval 'expect -c "
spawn ssh -o PubkeyAuthentication=no ${CONFIG[1]}
expect {
\"*assword\" {set timeout 6000; send '$pass'\n;}
\"yes/no\" {send \"yes\n\"; exp_continue;}
\"Last*\" { send_user \"login success ${CONFIG[1]}\";}
}
interact"'
else
eval "ssh ${CONFIG[1]}"
fi
fi
# add the command to the bash history as if we had typed it, will only work if sourced
# Determine which history command to use based on shell
# if [ -n "$BASH_VERSION" ]; then
# eval "history -s $COMMAND"
# elif [ -n "$ZSH_VERSION" ]; then
# eval "print -s $COMMAND"
# fi
# run the command
# eval $COMMAND
}
# allow warp to be sourced without running
if [[ $_ == $0 ]]; then
# pass all arguments to the warp function
warp "$@"
else
# Hide warp from history if using zsh and setopt histignorespace
alias warp=" warp"
fi