# Gets the MD5 hash of a component from a raw repository:
# Arg1: The raw repository (e.g. raw-dataset-example)
# Arg2: The component (living inside the raw repository) of which we want the md5
# Arg3: The credentials for the repository in the following format "user:pass"
# Return value: It will output to stdout the hash of the file, you can capture it with $(getRepositoryMD5 args...)
function getRepositoryMD5 {
local REPO=$1
local COMPONENT=$2
local CREDENTIALS=$3
local data="[{\"action\":\"coreui_Component\",\"method\":\"readAssets\",\"data\":[{\"page\":0,\"start\":0,\"limit\":1000,\"filter\":[{\"property\":\"repositoryName\",\"value\":\"$REPO\"}]}],\"type\":\"rpc\",\"tid\":0}]"
local hash=$(curl -k --silent --request POST \
--url https://x.x.x.x/nexus/service/extdirect \
--header 'accept: application/json' \
--user ${CREDENTIALS} \
--connect-timeout 5 \
--header 'content-type: application/json' \
--data ${data} | jq --arg COMPONENT ${COMPONENT} '.result.data[] | select(.name==$COMPONENT) |.attributes.checksum.md5'| tr -d '"')
# Use the tr call to remove the quotes around the hash (e.g "abs3" -> abs3)
# Check that we have all the necessary variables, or fail otherwise
echo ${hash}
}