forked from bitrise-steplib/steps-ftp-upload
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep.sh
executable file
·148 lines (126 loc) · 3.99 KB
/
step.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
148
#!/bin/bash
set -e
#=======================================
# Functions
#=======================================
RESTORE='\033[0m'
RED='\033[00;31m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
GREEN='\033[00;32m'
function color_echo {
color=$1
msg=$2
echo -e "${color}${msg}${RESTORE}"
}
function echo_fail {
msg=$1
echo
color_echo "${RED}" "${msg}"
exit 1
}
function echo_warn {
msg=$1
color_echo "${YELLOW}" "${msg}"
}
function echo_info {
msg=$1
echo
color_echo "${BLUE}" "${msg}"
}
function echo_details {
msg=$1
echo " ${msg}"
}
function echo_done {
msg=$1
color_echo "${GREEN}" " ${msg}"
}
function validate_required_input {
key=$1
value=$2
if [ -z "${value}" ] ; then
echo_fail "[!] Missing required input: ${key}"
fi
}
function print_secret_value {
if ! [ -z $1 ] ; then
echo "***"
fi
}
#=======================================
# Main
#=======================================
# Validate parameters
echo_info "Configs:"
echo_details "* hostname: $hostname"
echo_details "* username: $(print_secret_value $username)"
echo_details "* password: $(print_secret_value $password)"
echo_details "* upload_source_path: $upload_source_path"
echo_details "* upload_target_path: $upload_target_path"
validate_required_input "hostname" $hostname
validate_required_input "username" $username
validate_required_input "password" $password
validate_required_input "upload_source_path" $upload_source_path
validate_required_input "upload_target_path" $upload_target_path
os=$(uname -s)
command -v lftp >/dev/null 2>&1
is_lftp_installed=$?
if [[ $is_lftp_installed -ne 0 ]] ; then
if [[ "$os" == "Darwin" ]] ; then
echo_info "Installing lftp on Darwin"
echo_details "$ brew install homebrew/boneyard/lftp"
brew install homebrew/boneyard/lftp
elif [[ "$os" == "Linux" ]] ; then
echo_info "Installing lftp on Linux"
echo_details "$ sudo apt-get install lftp"
sudo apt-get install lftp
else
echo_fail "unkown os: $os, supported: [Darwin, Linux]"
fi
fi
echo_info "Uploading ${upload_source_path} -> ${upload_target_path}"
(
let targets_last_index=${#upload_target_path}-1
if [[ -d "${upload_source_path}" ]] ; then
# source: dir | target: dir
if [ "${upload_target_path:$targets_last_index:1}" = "/" ]; then
lftp -u "${username},${password}" "${hostname}" -e "set ftp:ssl-allow no; mirror -R ${upload_source_path} ${upload_target_path%?}; bye"
else
lftp -u "${username},${password}" "${hostname}" -e "set ftp:ssl-allow no; mirror -R ${upload_source_path} ${upload_target_path}; bye"
fi
elif [[ -f "${upload_source_path}" ]] ; then
if [ "${upload_target_path:$targets_last_index:1}" = "/" ]; then
# source: file | target: dir
if [ "$upload_source_path" = "" ] ; then
# target: rootdir
lftp -u "${username},${password}" "${hostname}" -e "set ftp:ssl-allow no; put -O '/' ${upload_source_path}; bye"
else
set +e
lftp -u "${username},${password}" "${hostname}" -e "set ftp:ssl-allow no; mkdir -p ${upload_target_path}; bye"
set -e
lftp -u "${username},${password}" "${hostname}" -e "set ftp:ssl-allow no; put -O ${upload_target_path} ${upload_source_path}; bye"
fi
else
# source: file | target: file
target_directory="$(dirname ${upload_target_path})"
target_filename="$(basename ${upload_target_path})"
if [ "$target_directory" = "" ] ; then
# target-dir: rootdir
lftp -u "${username},${password}" "${hostname}" -e "set ftp:ssl-allow no; put -O '/' ${upload_source_path} -o ${target_filename}; bye"
else
set +e
lftp -u "${username},${password}" "${hostname}" -e "set ftp:ssl-allow no; mkdir -p ${target_directory}; bye"
set -e
lftp -u "${username},${password}" "${hostname}" -e "set ftp:ssl-allow no; put -O ${target_directory} ${upload_source_path} -o ${target_filename}; bye"
fi
fi
else
echo "source is invalid"
exit 1
fi
)
if [ $? -ne 0 ] ; then
echo_fail "Upload failed"
fi
echo_done "Upload succed"