How to sort Artifactory package search result by version number with JFrog CLI?

試著忘記壹切 提交于 2020-01-05 05:42:07

问题


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

My target is to get an output sorted by version:

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

Unfortunately I can not use --sort-by=created as it can differ from version-sorting. Even if I do not use --sort-byoption it does not work. Also the version numbers can contain letters like "-beta".

In the Artifactory TreeView it is correct, but not in CLI.

How can I get a result sorted-by version number?


回答1:


You can use jq to sort version number strings.

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)[]

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

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)[]

(Of course the -n option is only needed when using inputs to read.)

Highest version number

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]


来源:https://stackoverflow.com/questions/56024932/how-to-sort-artifactory-package-search-result-by-version-number-with-jfrog-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!