I would like to be able to use bash variable replacement in the following query to Artifactory Query Language api (AQL)
In the shell, the command works like this:
$ curl -H 'content-type: text/plain' -H 'X-Api-Key: APIKEYGOESHERE' -X POST https://foo.jfrog.io/foo/api/search/aql -d '
> items.find({
> "repo":{"$eq":"foo-docker"},
> "path":{"$match":"REPONAME/*"}
> })
> .sort({
> "$desc":["created"]
> }
> ).limit(1)'
{
"results" : [ {
"repo" : "docker-local",
"path" : "REPONAME/0.0.1-dev.54621",
"name" : "manifest.json",
"type" : "file",
"size" : 3470,
"created" : "2019-12-31T11:09:38.106Z",
"created_by" : "automation",
"modified" : "2019-12-31T11:09:37.940Z",
"modified_by" : "automation",
"updated" : "2019-12-31T11:09:38.107Z"
} ],
"range" : {
"start_pos" : 0,
"end_pos" : 1,
"total" : 1,
"limit" : 1
}
However, putting this curl command into a bash variable with replacement is proving difficult
Here's what I've tried so far, no luck
#!/bin/bash
# This script can find the most recent version of a docker image in artifactory
if [ $# -ne 2 ]; then
echo "Usage: $0 apikey repo-path-name"
exit 1
fi
apikey=$1
echo "apikey: $apikey"
repopath=$2
echo "repopath: $repopath"
output=`curl -H 'content-type: text/plain' -H "X-Api-Key: $apikey" -X POST https://foo.jfrog.io/foo/api/search/aql -d 'items.find({
"repo":{"\$eq":"foo-docker"},
"path":{"\$match":"$repopath/*"}
})
.sort({
"$desc":["created"]
}
).limit(1)'`
echo $output
It almost works, except the $repopath variable does not get substituted into the call.
The variable isn't expanding because it's inside single quotes (starting right before items.find
). You should temporarily switch from single to double quotes to do the expansion, like this:
#!/bin/bash
# This script can find the most recent version of a docker image in artifactory
if [ $# -ne 2 ]; then
echo "Usage: $0 apikey repo-path-name"
exit 1
fi
apikey=$1
echo "apikey: $apikey"
repopath=$2
echo "repopath: $repopath"
output=`curl -H 'content-type: text/plain' -H "X-Api-Key: $apikey" -X POST https://foo.jfrog.io/foo/api/search/aql -d 'items.find({
"repo":{"\$eq":"foo-docker"},
"path":{"\$match":"'"$repopath"'/*"}
})
.sort({
"$desc":["created"]
}
).limit(1)'`
echo $output