【问题标题】:How to do bash variable replacement with curl artifactory AQL query [duplicate]如何使用 curl 工件 AQL 查询进行 bash 变量替换 [重复]
【发布时间】:2020-04-21 15:14:57
【问题描述】:

我希望能够在 Artifactory Query Language api (AQL) 的以下查询中使用 bash 变量替换

在 shell 中,命令的工作方式如下:

$ 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
}

然而,将这个 curl 命令放入一个带有替换的 bash 变量是很困难的

这是我迄今为止尝试过的,没有运气

#!/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

它几乎可以工作,除了 $repopath 变量没有被替换到调用中。

【问题讨论】:

  • 最好使用jq之类的工具来创建JSON对象。它将确保所有内容都被正确引用和转义,如果您只是将变量替换为字符串,这很困难。
  • 其实AQL不使用JSON,而是自定义变体
  • 我刚刚检查了文档。它说条件是有效 JSON 格式的搜索条件

标签: bash curl artifactory quotes dollar-sign


【解决方案1】:

变量没有扩展,因为它在单引号内(从items.find 之前开始)。您应该暂时从单引号切换到双引号来进行扩展,如下所示:

#!/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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-05
    • 2017-07-30
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2021-03-30
    相关资源
    最近更新 更多