Skip to content

Commit 1e70791

Browse files
committed
Apply shellcheck recommandations to fake aggregator import.sh
See https://github.com/koalaman/shellcheck/wiki for more
1 parent f4e9666 commit 1e70791

File tree

1 file changed

+27
-25
lines changed
  • mithril-test-lab/mithril-aggregator-fake/scripts

1 file changed

+27
-25
lines changed

mithril-test-lab/mithril-aggregator-fake/scripts/import.sh

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ check_requirements() {
1212
display_help() {
1313
if [ -n "${1-""}" ]; then echo "ERROR: ${1}"; echo; fi
1414
echo "HELP"
15-
echo $0
15+
echo "$0"
1616
echo
1717
echo "Import and save data from a given Mithril Aggregator."
1818
echo
@@ -31,7 +31,7 @@ error() {
3131

3232
clean_directory() {
3333
echo "Cleaning data directory…"
34-
rm "$DATA_DIR/"*.json || true
34+
rm "${DATA_DIR:?}/"*.json || true
3535
}
3636

3737
# $1=URL $2=artifact_name $3=target_dir (default to $DATA_DIR)
@@ -57,16 +57,16 @@ download_artifacts() {
5757
echo -n "Downloading ${artifact} data: "
5858
tput sc;
5959

60-
for field in $(jq -r .[].${json_field} < $DATA_DIR/${artifact}s-list.json);
60+
for field in $(jq -r ".[].${json_field}" < "$DATA_DIR/${artifact}s-list.json");
6161
do
6262
tput rc;
6363
download_data "${url}/${field}" "${field}" "$artifact_dir"
6464

6565
if [ true = "$download_missing_certificate" ]; then
66-
download_missing_certificate $(jq -r ".certificate_hash" "$DATA_DIR/${artifact}/${field}.json");
66+
download_missing_certificate "$(jq -r ".certificate_hash" "$DATA_DIR/${artifact}/${field}.json")";
6767
fi
6868

69-
let "nb=nb+1"
69+
((nb=nb+1))
7070
echo -n "$nb "
7171
done
7272
echo "";
@@ -83,29 +83,31 @@ download_missing_certificate() {
8383
}
8484

8585
download_certificate_chain() {
86-
local parent_hash=$(jq -r .[0].hash $DATA_DIR/certificates-list.json);
86+
local parent_hash="";
8787
local certificate_hash;
8888
local -i nb=0
8989

9090
echo -n "Downloading certificate chain: "
9191
tput sc;
9292

93+
parent_hash=$(jq -r ".[0].hash" "$DATA_DIR/certificates-list.json");
9394
until [ -z "$parent_hash" ];
9495
do
9596
tput rc;
9697

9798
certificate_hash=$parent_hash;
9899
download_data "${BASE_URL}/certificate/${certificate_hash}" "${certificate_hash}" "$DATA_DIR/certificate"
99100
parent_hash=$(jq -r .previous_hash "$DATA_DIR/certificate/${certificate_hash}.json");
100-
let "nb=nb+1"
101+
102+
((nb=nb+1))
101103
echo -n "$nb "
102104
done
103105
echo "";
104106
}
105107

106108
# $@=list of transactions_hashes
107109
download_ctx_proof() {
108-
local -r ctx_hashes=${@:?"No cardano transaction hashes given to download_ctx_proof function."};
110+
local -r ctx_hashes=${*:?"No cardano transaction hashes given to download_ctx_proof function."};
109111
local -i nb=0
110112
local -r artifact_dir="$DATA_DIR/ctx-proof"
111113

@@ -118,38 +120,38 @@ download_ctx_proof() {
118120
do
119121
tput rc;
120122
download_data "${BASE_URL}/proof/cardano-transaction?transaction_hashes=${cardano_transaction_hash}" "${cardano_transaction_hash}" "${artifact_dir}"
121-
download_missing_certificate $(jq -r ".certificate_hash" "$DATA_DIR/ctx-proof/${cardano_transaction_hash}.json");
123+
download_missing_certificate "$(jq -r ".certificate_hash" "$DATA_DIR/ctx-proof/${cardano_transaction_hash}.json")";
122124

123-
let "nb=nb+1"
125+
((nb=nb+1))
124126
echo -n "$nb "
125127
done
126128
echo "";
127129
}
128130

129131
# $@=list of transactions_hashes
130132
write_ctx_proof_hashes_list() {
131-
local -r ctx_hashes=${@:?"No cardano transaction hashes given to write_ctx_proof_hashes_list function."};
133+
local -r ctx_hashes=${*:?"No cardano transaction hashes given to write_ctx_proof_hashes_list function."};
132134
local -i nb=0
133135

134-
echo -n "Writting cardano transaction proof ids to a file: "
136+
echo -n "Writing cardano transaction proof ids to a file: "
135137
tput sc;
136138

137-
echo "[" > $DATA_DIR/ctx-proofs-list.json
139+
echo "[" > "$DATA_DIR/ctx-proofs-list.json"
138140

139141
local separator=" "
140142
for cardano_transaction_hash in $ctx_hashes;
141143
do
142144
tput rc;
143-
cat >> $DATA_DIR/ctx-proofs-list.json <<EOF
145+
cat >> "$DATA_DIR/ctx-proofs-list.json" <<EOF
144146
$separator { "transaction_hash": "$cardano_transaction_hash" }
145147
EOF
146148

147149
separator=","
148150

149-
let "nb=nb+1"
151+
((nb=nb+1))
150152
echo -n "$nb "
151153
done
152-
echo "]" >> $DATA_DIR/ctx-proofs-list.json
154+
echo "]" >> "$DATA_DIR/ctx-proofs-list.json"
153155

154156
echo "";
155157
}
@@ -165,10 +167,10 @@ join_artifacts_files() {
165167

166168
echo "Joining ${name} artifacts into ${dest}"
167169

170+
local key=""
168171
buffer+="{"
169172
for filename in "$src/"*; do
170-
local key=$(basename "${filename%.*}")
171-
173+
key=$(basename "${filename%.*}")
172174
buffer+=$(echo "$separator\"$key\": " | cat - "$filename")
173175
separator=","
174176
done
@@ -187,16 +189,16 @@ declare -r DATA_DIR=$1;
187189
shift
188190
declare -r BASE_URL=$1;
189191
shift
190-
declare -r CARDANO_TRANSACTIONS_HASHES=$@;
192+
declare -r CARDANO_TRANSACTIONS_HASHES=$*;
191193

192194
echo "-- MITHRIL AGGREGATOR FAKE - DATA IMPORTER --"
193-
echo "data_dir:" $DATA_DIR
194-
echo "base_url:" $BASE_URL
195-
echo "tx_hashes:" $CARDANO_TRANSACTIONS_HASHES
195+
echo "data_dir:" "$DATA_DIR"
196+
echo "base_url:" "$BASE_URL"
197+
echo "tx_hashes:" "$CARDANO_TRANSACTIONS_HASHES"
196198
echo
197199

198200
if [ ! -d "$DATA_DIR" ]; then error "Specified directory '${DATA_DIR}' is not a directory."; fi
199-
wget --quiet --server-response --spider $BASE_URL 2>/dev/null || error "Could not reach URL '${BASE_URL}'.";
201+
wget --quiet --server-response --spider "$BASE_URL" 2>/dev/null || error "Could not reach URL '${BASE_URL}'.";
200202

201203
export DATA_DIR URL;
202204

@@ -220,8 +222,8 @@ download_data "$BASE_URL/artifact/cardano-transactions" "ctx-snapshots-list"
220222
download_artifacts "$BASE_URL/artifact/cardano-transaction" "ctx-snapshot" "hash"
221223

222224
if [ -n "$CARDANO_TRANSACTIONS_HASHES" ]; then
223-
download_ctx_proof $CARDANO_TRANSACTIONS_HASHES
224-
write_ctx_proof_hashes_list $CARDANO_TRANSACTIONS_HASHES
225+
download_ctx_proof "$CARDANO_TRANSACTIONS_HASHES"
226+
write_ctx_proof_hashes_list "$CARDANO_TRANSACTIONS_HASHES"
225227
join_artifacts_files "ctx-proof"
226228
fi
227229

0 commit comments

Comments
 (0)