-
Notifications
You must be signed in to change notification settings - Fork 10
/
add-datasets.sh
120 lines (117 loc) · 3.81 KB
/
add-datasets.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
#!/bin/bash
# check if datasets.json exists and exit if not
if [ ! -f datasets.json ]; then
echo "datasets.json does not exist. Please run from the same directory as datasets.json."
exit 1
fi
# input: file group metadata
read -p "Parent directory: " dir_parent
read -p "Meta URL: " meta_url
read -p "Meta URL name: " meta_url_name
read -p "Meta group 1: " meta_group_1
read -p "Meta group 2 (leave blank for none): " meta_group_2
# define function: input individual dataset info
function input_dataset() {
# input: individual dataset info
read -p "Meta name: " meta_name
read -p "Active (y/n): " active
# check validity of active
while [ "$active" != "y" ] && [ "$active" != "n" ]; do
echo "Invalid input. Please enter 'y' or 'n'."
read -p "Active: " active
done
if [ "$active" = "y" ]; then
active="True"
active_key="active"
else
active="False"
active_key="inactive"
fi
read -p "URL: " url
read -p "File directory: " dir_file
read -p "File name: " file_name
# replace spaces in file name with underscores
if [[ "$file_name" == *" "* ]]; then
file_name="${file_name// /_}"
echo "Spaces in file name have been replaced with underscores."
fi
read -p "File extension: " file_ext
# derived variables
if [ "$file_ext" = "html" ]; then
dl_fun="html_page"
else
dl_fun="dl_file"
fi
id_name="${dir_parent^^} - ${meta_url_name} - ${meta_name}"
if [ "$file_ext" = "html" ]; then
id_name="{$id_name} (webpage)"
fi
# generate UUID
uuid=$(uuidgen --random)
# create JSON entry
# create metadata
metadata=$(jq \
--arg meta_name "$meta_name" \
--arg meta_url "$meta_url" \
--arg meta_url_name "$meta_url_name" \
--arg meta_group_1 "$meta_group_1" \
'. | .["meta_name"]=$meta_name |
.["meta_url"]=$meta_url |
.["meta_url_name"]=$meta_url_name |
.["meta_licence"]="" |
.["meta_licence_url"]="" |
.["meta_group_1"]=$meta_group_1' \
<<<'{}'
)
# append meta_group_2 to metadata if it is not blank
if [ "$meta_group_2" != "" ]; then
metadata=$(jq \
--arg meta_group_2 "$meta_group_2" \
'. | .["meta_group_2"]=$meta_group_2' \
<<<$metadata
)
fi
# create rest of JSON entry
json=$(jq \
--arg id_name "$id_name" \
--arg uuid "$uuid" \
--arg active "$active" \
--arg url "$url" \
--arg dir_parent "$dir_parent" \
--arg dir_file "$dir_file" \
--arg file_name "$file_name" \
--arg file_ext "$file_ext" \
--arg dl_fun "$dl_fun" \
--argjson metadata "$metadata" \
'. | .["id_name"]=$id_name |
.["uuid"]=$uuid |
.["active"]=$active |
.["url"]=$url |
.["dir_parent"]=$dir_parent |
.["dir_file"]=$dir_file |
.["file_name"]=$file_name |
.["file_ext"]=$file_ext |
.["dl_fun"]=$dl_fun |
.["args"]={} |
.["supplementary"]={} |
.["metadata"]=$metadata |
.["notes"]={}' \
<<<'{}'
)
echo "$json"
# append JSON entry to datasets.json using active and dir_parents keys
jq --argjson json "$json" \
--arg active_key "$active_key" \
--arg dir_parent "$dir_parent" \
'.[$active_key][$dir_parent] += [$json]' datasets.json > datasets.json.tmp && mv datasets.json.tmp datasets.json
}
# run function and then prompt to add another dataset or exit
input_dataset
while true; do
read -p "Add another dataset to this file group? (y/n): " yn
case $yn in
[Yy]* ) input_dataset;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done