如何使用JFrog CLI按版本号对Artifactory软件包搜索结果进行排序? [英] How to sort Artifactory package search result by version number with JFrog CLI?
问题描述
我需要在 Artifactory 中获取特定NuGet软件包的最新版本.
我使用以下JFrog CLI命令接收所有版本的列表(--limit=1
之后),包括使用jq进行JSON解析:
I need to get the latest version of a specific NuGet package in Artifactory.
I use following JFrog CLI command to receive a list of all versions (later on with --limit=1
), including JSON parsing with jq:
jfrog rt s myRepo/Path/ --props "nuget.id=MyLib" --sort-by=name --sort-order=desc | jq -M -r ".[] | .props.\"nuget.version\" | .[]"
上面的示例将输出原始字符串,如下所示:
The above example results in raw string output like this:
1.2.3.101
1.2.3.103
1.2.3.95
1.2.3.99
1.2.3.99-beta10
1.2.3.99-beta9
我的目标是要获得输出按版本排序:
1.2.3.95
1.2.3.99
1.2.3.99-beta9
1.2.3.99-beta10
1.2.3.101
1.2.3.103
很遗憾,我不能不使用--sort-by=created
,因为它可能与版本排序不同.即使我不使用--sort-by
option也不起作用.版本号也可以包含"-beta"之类的字母.
Unfortunately I can not use --sort-by=created
as it can differ from version-sorting. Even if I do not use --sort-by
option it does not work. Also the version numbers can contain letters like "-beta".
在Artifactory TreeView中,它是正确的,但在CLI中是不正确的.
In the Artifactory TreeView it is correct, but not in CLI.
如何获得按版本号排序的结果?
How can I get a result sorted-by version number?
推荐答案
您可以使用jq对版本号字符串进行排序.
You can use jq to sort version number strings.
如果字符串是原始"字符串,每行一个,则可以使用以下jq程序:
If the strings are "raw" strings, one per line, then you could use this jq program:
def parse:
sub("alpha"; "alpha.")
| sub("beta"; "beta.")
| sub("gamma"; "gamma.")
| sub("prerelease"; "prerelease.")
| sub("debug"); "debug.")
| [splits("[-.]")]
| map(tonumber? // .) ;
[inputs]
| sort_by(parse)[]
这个jq程序可以这样运行:
This jq program could be run like so:
jq -nrR -f program.jq versions.txt
使用示例版本号,将产生:
With the sample version numbers, this would produce:
1.2.3.95
1.2.3.99
1.2.3.99-beta9
1.2.3.99-beta10
1.2.3.101
1.2.3.103
根据您的情况,通过将程序修改为使用parse
,而无需再次调用jq,就可以实现结果,方法如上所示. jq程序的主要部分可能看起来像这样:
In your case, the result can be achieved without invoking jq a second time by modifying your program to use parse
along the lines shown above. The main part of the jq program would probably look something like this:
map(.props["nuget.version"]) | sort_by(parse)[]
(当然,仅当使用inputs
进行读取时才需要-n选项.)
(Of course the -n option is only needed when using inputs
to read.)
如果(仅在注释中注明)您只想要最高版本号,则只需将最终的[]
更改为[-1]
:
If (as indicated in a comment is the case) you just want the highest version number, you could simply change the final []
to [-1]
:
... | sort_by(parse)[-1]
这篇关于如何使用JFrog CLI按版本号对Artifactory软件包搜索结果进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!