-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnd_mv.sh
228 lines (223 loc) · 9.1 KB
/
nd_mv.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
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
#!/bin/bash
##################################################################
#
# About This Script:
# nd_mv: implements the BASH mv command with options for local
# and/or Palantir target system;
# NOTE: requires curl, jq;
#
##################################################################
#
# NOTE: options and associated input, if any, may be passed
# in any order;
#
# Usage:
# sh nd_mv.sh -ff (-from_file) "from_local_file_name.extension"
# -tf (-to_file) "to_local_file_name.extension"
# -fr (-from_rid) "from_palantir_object_rid"
# -fp (-from_path) "from_palantir_object_path"
# -tr (-to_rid) "to_palantir_object_rid"
# -tp (-to_path) "to_palantir_object_path"
# -u (-user_token) "user_token"
# -d (-domain) "domain_url"
# [default:https://nidap.nih.gov]
# ............moves from_file/_path as to_file/_path; direction
# determined by input; e.g., providing -ff and -tf
# results in local move, providing -fr,-fp, -tr, -tp,
# and -u results in Palantir move, providing combin-
# ations produce intuitive results; i.e., providing
# -tf with -fr, -fp and -u moves from Palantir to
# local system, while -ff with -tr, -tp and -u moves
# from local system to Palantir;
#
# sh nd_mv.sh -h (-help)
# ............displays function help;
#
# sh nd_mv.sh -t (-test)
# ............test function;
#
##################################################################
#
# Returns:
# SUCCESS or FAILURE
#
##################################################################
# execute request
strArg=`echo "$1" | awk -va="$1" '{print tolower(a)}'`
if [ "$#" -gt 1 ] && [ "$strArg" != "-h" ] && [ "$strArg" != "-help" ] && [ "$strArg" != "-t" ] && [ "$strArg" != "-test" ]
then
strFromFile=""
strToFile=""
strFromRID=""
strFromPath=""
strToRID=""
strToPath=""
strUserToken=""
strDomain=""
while [ $# -ne 0 ]
do
strArg=`echo "$1" | awk -va="$1" '{print tolower(a)}'`
if [ "$strArg" = "-ff" ] || [ "$strArg" = "-from_file" ]
then
shift
strFromFile="$1"
elif [ "$strArg" = "-tf" ] || [ "$strArg" = "-to_file" ]
then
shift
strToFile="$1"
elif [ "$strArg" = "-fr" ] || [ "$strArg" = "-from_rid" ]
then
shift
strFromRID="$1"
elif [ "$strArg" = "-fp" ] || [ "$strArg" = "-from_path" ]
then
shift
strFromPath="$1"
elif [ "$strArg" = "-tr" ] || [ "$strArg" = "-to_rid" ]
then
shift
strToRID="$1"
elif [ "$strArg" = "-tp" ] || [ "$strArg" = "-to_path" ]
then
shift
strToPath="$1"
elif [ "$strArg" = "-u" ] || [ "$strArg" = "-user_token" ]
then
shift
strUserToken="$1"
elif [ "$strArg" = "-d" ] || [ "$strArg" = "-domain" ]
then
shift
strDomain="$1"
fi
shift
done
strTimeStamp=$(date '+%Y-%m-%d %H:%M:%S')
strWorkingDirectory=$(pwd)
strTempFile="$strWorkingDirectory/temp/$strTimeStamp.___"
# local move
if [ "$strFromFile" != "" ] && [ "$strToFile" != "" ]
then
#echo "Moving local file: [$strFromFile] to [$strToFile]...";
if mv "$strFromFile" "$strToFile"
then
echo "SUCCESS\n"
else
echo "FAILURE\n$?"
fi
# palatir move
elif [ "$strFromRID" != "" ] && [ "$strFromPath" != "" ] && [ "$strToRID" != "" ] && [ "$strToPath" != "" ] && [ "$strUserToken" != "" ]
then
#echo "Moving file: [$strFromRID]:[$strFromPath] to [$strToRID]:[$strToPath]...";
strDownloadResult=/bin/bash ./nd_pd.sh -tf "$strTempFile" -fr "$strFromRID" -fp "$strFromPath" -u "$strUserToken" -d "$strDomain"
if touch "$strTempFile"
then
strUploadResult=/bin/bash ./nd_pu.sh -ff "$strTempFile" -tr "$strToRID" -tp "$strToPath" -u "$strUserToken" -d "$strDomain"
# TODO: determine error conditions
strDeleteResult=/bin/bash ./nd_rm.sh -r "$strFromRID" -p "$strFromPath" -u "$strUserToken" -d "$strDomain"
/bin/bash ./nd_json_parser.sh "$strUploadResult"
/bin/bash ./nd_json_parser.sh "$strDeleteResult"
rm "$strTempFile"
else
/bin/bash ./nd_json_parser.sh "$strDownloadResult"
echo "FAILURE\n$?"
fi
# TODO: determine error conditions
# local to palatir move (upload with local delete)
elif [ "$strFromFile" != "" ] && [ "$strToRID" != "" ] && [ "$strToPath" != "" ] && [ "$strUserToken" != "" ]
then
#echo "Moving file: [$strFromRID]:[$strFromPath] to [$strToRID]:[$strToPath]...";
strUploadResult=/bin/bash ./nd_pu.sh -ff "$strFromFile" -tr "$strToRID" -tp "$strToPath" -u "$strUserToken" -d "$strDomain"
# TODO: determine error conditions
strDeleteResult=/bin/bash ./nd_rm.sh -f "$strFromFile"
/bin/bash ./nd_json_parser.sh "$strUploadResult"
/bin/bash ./nd_json_parser.sh "$strDeleteResult"
# palatir to local move (download with Palantir delete)
elif [ "$strToFile" != "" ] && [ "$strFromRID" != "" ] && [ "$strFromPath" != "" ] && [ "$strUserToken" != "" ]
then
#echo "Moving file: [$strFromRID]:[$strFromPath] to [$strToRID]:[$strToPath]...";
strDownloadResult=/bin/bash ./nd_pd.sh -tf "$strToFile" -fr "$strFromRID" -fp "$strFromPath" -u "$strUserToken" -d "$strDomain"
# TODO: determine error conditions
strDeleteResult=/bin/bash ./nd_rm.sh -r "$strFromRID" -p "$strFromPath" -u "$strUserToken" -d "$strDomain"
/bin/bash ./nd_json_parser.sh "$strDownloadResult"
/bin/bash ./nd_json_parser.sh "$strDeleteResult"
else
strError="FAILURE"
strError="$strError\nInvalid Input Set:"
strError="$strError\nLocal From File:\t$strFromFile"
strError="$strError\nLocal To File:\t$strToFile"
strError="$strError\nPalantir From RID:\t$strFromRID"
strError="$strError\nPalantir From RID:\t$strFromRID"
strError="$strError\nPalantir To RID:\t$strToRID"
strError="$strError\nPalantir To Path:\t$strToPath"
strError="$strError\nUser Token:\t$strUserToken"
strError="$strError\nDomain:\t$strDomain"
strError="$strError\n$0\n"
echo "$strError"
fi
# display help
elif [ "$#" -eq 1 ] && [ "$strArg" = "-h" ] || [ "$strArg" = "-help" ]
then
echo "##################################################################"
echo "#"
echo "# About This Script:"
echo "# nd_mv: implements the BASH mv command with options for local"
echo "# and/or Palantir target system;"
echo "#"
echo "##################################################################"
echo "#"
echo "# NOTE: options and associated input, if any, may be passed"
echo "# in any order;"
echo "#"
echo "# Usage:"
echo "# sh nd_mv.sh -ff (-from_file) \"from_local_file_name.extension\""
echo "# -tf (-to_file) \"to_local_file_name.extension\""
echo "# -fr (-from_rid) \"from_palantir_object_rid\""
echo "# -fp (-from_path) \"from_palantir_object_path\""
echo "# -tr (-to_rid) \"to_palantir_object_rid\""
echo "# -tp (-to_path) \"to_palantir_object_path\""
echo "# -u (-user_token) \"user_token\""
echo "# -d (-domain) \"domain_url\""
echo "# [default:https://nidap.nih.gov]"
echo "# ............moves from_file/_path as to_file/_path; direction"
echo "# determined by input; e.g., providing -ff and -tf"
echo "# results in local move, providing -fr,-fp, -tr, -tp,"
echo "# and -u results in Palantir move, providing combin-"
echo "# ations produce intuitive results; i.e., providing"
echo "# -tf with -fr, -fp and -u moves from Palantir to"
echo "# local system, while -ff with -tr, -tp and -u moves"
echo "# from local system to Palantir;"
echo "#"
echo "# sh nd_mv.sh -h (-help)"
echo "# ............displays function help;"
echo "#"
echo "# sh nd_mv.sh -t (-test)"
echo "# ............test function;"
echo "#"
echo "##################################################################"
echo "#"
echo "# Returns:"
echo "# Contents of requested folder/dataset(s)"
echo "#"
echo "##################################################################"
# test routine
elif [ "$#" -eq 1 ] && [ "$strArg" = "-t" ] || [ "$strArg" = "-test" ]
then
touch "test_from.___"
if test -f "test_from.___"
then
mv -i "test_from.___" "test_to.___"
if test -f "test_to.___"
then
echo "SUCCESS\n"
rm "test_to.___"
else
echo "FAILURE\n$?"
rm "test_from.___"
fi
else
echo "FAILURE\n$?"
fi
else
echo "FAILURE\nUnrecognized Input\nUsage: sh nd_mv.sh -h\n$0"
fi