-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsshget
executable file
·72 lines (57 loc) · 1.17 KB
/
sshget
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
#!/usr/bin/env bash
declare -r esc=$'\033'
declare -r c_reset="${esc}[0m"
declare -r c_red="${esc}[31m"
declare -r fifo='/tmp/sshget.fifo'
declare -A domains=()
declare -A paths=()
declare -a files=()
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&2
}
die() {
exit 1
}
usage() {
LESS=-FEXR less <<'HELP'
sshget <user@host1:/path/to/search ...>
HELP
}
has() {
local verbose=0
if [[ $1 = '-v' ]]; then
verbose=1
shift
fi
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( verbose > 0 )) && err "$c not found"
return 1
fi
done
}
has -v fzf rsync || die
cleanup() {
[[ -e "$fifo" ]] && rm "$fifo"
}
trap cleanup SIGHUP SIGINT SIGTERM
mkfifo "$fifo"
if (( $# < 1 )); then
usage
die
fi
for a; do
host="${a%:*}"
path="${a##*:}"
domains+=( ["$a"]="$host" )
paths+=( ["$a"]="$path" )
shift
done
for s in "${!domains[@]}"; do
ssh "${domains[$s]}" "find ${paths[$s]}" | sed -r "s|^|${domains[$s]}:|" >> "$fifo" &
done
mapfile -t files < <(fzf -e --inline-info +s --multi --cycle --bind='Ctrl-A:toggle-all,`:jump' < "$fifo")
if (( ${#files[@]} )); then
rsync --protect-args -auvzP -e ssh "${files[@]}" .
fi
cleanup