-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcprm.sh
executable file
·147 lines (126 loc) · 3.39 KB
/
cprm.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
# Description: Send a regular file or directory to a remote machine
# Dependencies: fzf, and one of these: scp, ffsend, croc
# Author: L. Abramovich
# License: GPL2+
if [ -z "$1" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
name="${CLIFM_PLUGIN_NAME:-$(basename "$0")}"
printf "Copy a regular file or directory to a remote machine
\x1b[1mUSAGE\x1b[0m\n %s [-e, --edit] [FILE/DIR]\n" "$name"
exit 0
fi
CONFIG_FILE="${XDG_CONFIG_HOME:-${HOME}/.config}/clifm/plugins/cprm.cfg"
if ! [ -f "$CONFIG_FILE" ]; then
echo "### cprm configuration file ####
# Ex:
# See scp(1) for information about scp options
#[Desktop machine]
#Options=\"-P 1046\"
#Target=\"[email protected]:\"
#
#[Android phone]
#Options=\"-P 2222 -o HostKeyAlgorithms=+ssh-dss\"
#Target=\"[email protected]:/storage/emulated/0/Download\"
#
#[Termux]
#Options=\"-P 8022\"
#Target=\"[email protected]:\"
#
# Just install ffsend. You'll get a download link
# Do not alter this remote name
#[ffsend]
#Options=\"--downloads 1 --copy\"
#Target=\"\"
#
# Just install croc. You'll get a download code
# Do not alter this remote name
#[croc]
#Options=\"\"
#Target=\"\"" > "$CONFIG_FILE"
fi
if ! type fzf >/dev/null 2>&1; then
printf "clifm: fzf: Command not found\n" >&2
exit 127
fi
if [ "$1" = "-e" ] || [ "$1" = "--edit" ]; then
if "${EDITOR:-nano}" "$CONFIG_FILE"; then
exit 0
fi
exit 1
fi
if [ -n "$2" ]; then
printf "Multiple files are not allowed.
To copy more than one file, either archive them (via 'ac') or move them \
to a single directory (then use that directory here).\n"
exit 1
fi
file="$(echo "$1" | sed 's/\\//g')"
is_dir=0
if [ -e "$file" ]; then
[ -d "$file" ] && is_dir=1
else
printf "%s: No such file or directory\n" "$file" >&2
exit 2
fi
# Source our plugins helper
if [ -z "$CLIFM_PLUGINS_HELPER" ] || ! [ -f "$CLIFM_PLUGINS_HELPER" ]; then
printf "clifm: Unable to find plugins-helper file\n" >&2
exit 1
fi
# shellcheck source=/dev/null
. "$CLIFM_PLUGINS_HELPER"
remotes="$(grep "^\[" "$CONFIG_FILE" | tr -d '[]')"
if [ -z "$remotes" ]; then
printf "No remotes found. Use the -e option to edit the configuration \
file\n" >&2
exit 1
fi
remotes_num="$(echo "$remotes" | grep -c '^')"
remotes_num="$((remotes_num + 2))"
# shellcheck disable=SC2154
remote="$(printf "%s" "$remotes" | fzf \
--ansi --prompt "$fzf_prompt" \
--reverse --height "$remotes_num" --info=inline \
--header "Choose a remote" \
--bind "tab:accept" --info=inline --color="$(get_fzf_colors)")"
[ -z "$remote" ] && exit 0
opts="$(grep -A2 "$remote" "$CONFIG_FILE" | grep ^"Options" \
| cut -d= -f2-10 | sed 's/\"//g')"
target="$(grep -A2 "$remote" "$CONFIG_FILE" | grep ^"Target" \
| cut -d= -f2 | sed 's/\"//g')"
##### FFSEND #####
if [ "$remote" = "ffsend" ]; then
if ! type ffsend >/dev/null 2>&1; then
printf "clifm: ffsend: Command not found\n" >&2
exit 127
else
# shellcheck disable=SC2086
ffsend upload $opts "$file" && exit 0
exit 1
fi
fi
##### CROC #####
if [ "$remote" = "croc" ]; then
if ! type croc >/dev/null 2>&1; then
printf "clifm: croc: Command not found\n" >&2
exit 127
else
croc send "$file" && exit 0
exit 1
fi
fi
##### SCP #####
if ! type scp >/dev/null 2>&1; then
printf "clifm: scp: Command not found\n" >&2
exit 127
fi
if [ -z "$target" ]; then
printf "No target specified\n" >&2;
exit 1
fi
if [ "$is_dir" -eq 1 ]; then
opts="-r $opts"
fi
# shellcheck disable=SC2086
scp $opts "$file" "$target" && exit 0
exit 1