-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnd_rm.sh
173 lines (168 loc) · 5.47 KB
/
nd_rm.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
#!/bin/bash
##################################################################
#
# About This Script:
# nd_rm: implements the BASH rm command with options for local
# or Palantir target system;
# NOTE: requires curl, jq;
#
##################################################################
#
# NOTE: options and associated input, if any, may be passed
# in any order;
#
# Usage:
# sh nd_rm.sh -f (-file) "local_file_path_name.extension"
# ............deletes requested file(s) from local system;
#
# sh nd_rm.sh -r (-rid) "palantir_dataset_rid"
# -p (-path) "palantir_file_path_name.extension"
# -u (-user_token) "user_token"
# -d (-domain) "domain_url"
# [default:https://nidap.nih.gov]
# ............deletes requested file(s) from Palantir system;
#
# sh nd_rm.sh -h (-help)
# ............displays function help;
#
# sh nd_rm.sh -t (-test)
# ............tests functionality (ignores file pathname provided;
# deletes temporary test file);
#
##################################################################
#
# Returns:
# string: SUCCESS or FAILURE
#
##################################################################
strDefaultDomain="https://nidap.nih.gov"
# execute request
strArg=`echo "$1" | awk -va="$1" '{print tolower(a)}'`
if [ "$#" -gt 1 ] && [ "$strArg" != "-h" ] && [ "$strArg" != "-help" ] && [ "$strArg" != "-t" ] && [ "$strArg" != "-test" ]
then
strFile=""
strRID=""
strPath=""
strUserToken=""
strDomain=""
while [ $# -ne 0 ]
do
strArg=`echo "$1" | awk -va="$1" '{print tolower(a)}'`
if [ "$strArg" = "-f" ] || [ "$strArg" = "-file" ]
then
shift
strFile="$1"
elif [ "$strArg" = "-r" ] || [ "$strArg" = "-rid" ]
then
shift
strRID="$1"
elif [ "$strArg" = "-p" ] || [ "$strArg" = "-path" ]
then
shift
strPath="$1"
elif [ "$strArg" = "-u" ] || [ "$strArg" = "-user_token" ]
then
shift
strUserToken="$1"
elif [ "$strArg" = "-d" ] || [ "$strArg" = "-domain" ]
then
shift
strDomain="$1"
fi
shift
done
if [ "$strDomain" = "" ]
then
strDomain="$strDefaultDomain"
fi
# local remove
if [ "$strFile" != "" ]
then
#echo "Deleting local file(s): [$strFile]...";
if rm "$strFile"
then
echo "SUCCESS\n"
else
echo "FAILURE\n$?"
fi
# Palantir remove
elif [ "$strRID" != "" ] && [ "$strPath" != "" ] && [ "$strUserToken" != "" ] && [ "$strDomain" != "" ]
then
#echo "Deleting Palantir dataset [$strRID] file(s): [$strPath]...";
strPost=$(curl -X DELETE \
"$strDomain/foundry-catalog/api/catalog/datasets/$strRID/files/$strPath?preview=true" \
-H "Authorization: Bearer $strUserToken")
if [ "$strPost" != "" ]
then
sh ./nd_json_parser.sh "$strPost"
else
echo "WARNING\nResponse is empty.\n$0"
fi
else
strError="FAILURE"
strError="$strError\nInvalid Input Set:"
strError="$strError\nLocal File:\t$strFile"
strError="$strError\nObject RID:\t$strRID"
strError="$strError\nObject Path:\t$strPath"
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_rm: implements the BASH rm command with options for local"
echo "# 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_rm.sh -f (-file) \"local_file_path_name.extension\""
echo "# ............deletes requested file(s) from local system;"
echo "#"
echo "# sh nd_rm.sh -r (-rid) \"rid_of_dataset\""
echo "# -p (-path) \"palantir_file_path_name.extension\""
echo "# -u (-user_token) \"user_token\""
echo "# -d (-domain) \"domain_url\""
echo "# [default:https://nidap.nih.gov]"
echo "# ............deletes requested file(s) from Palantir system;"
echo "#"
echo "# sh nd_rm.sh -h (-help)"
echo "# ............displays function help;"
echo "#"
echo "# sh nd_rm.sh -t (-test)"
echo "# ............tests functionality (ignores file pathname provided;"
echo "# deletes temporary test file);"
echo "#"
echo "##################################################################"
echo "#"
echo "# Returns:"
echo "# nothing"
echo "#"
echo "##################################################################"
# test routine
elif [ "$#" -eq 1 ] && [ "$strArg" = "-t" ] || [ "$strArg" = "-test" ]
then
touch "test.___"
if test -f "test.___"
then
rm "test.___"
if test -f "test.___"
then
echo "SUCCESS\n"
else
echo "FAILURE\n$?"
fi
else
echo "FAILURE\n$?"
fi
else
echo "FAILURE\nUnrecognized Input\nUsage: sh nd_rm.sh -h\n$0"
fi