You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.9 KiB

8 years ago
  1. #!/bin/bash
  2. # parse file
  3. applyRegex() {
  4. local regex=$1
  5. local file=$2
  6. if [[ $file =~ $regex ]];
  7. then
  8. version="${BASH_REMATCH[1]}"
  9. echo "${version}"
  10. else
  11. echo "$file doesn't match" >&2 # this could get noisy if there are a lot of non-matching files
  12. exit 1
  13. fi
  14. echo "${version}"
  15. }
  16. # Use jq regex since it supports grouping
  17. applyRegex_version() {
  18. local regex=$1
  19. local file=$2
  20. jq -n "{
  21. version: $(echo $file | jq -R .)
  22. }" | jq --arg v "$regex" '.version | capture($v)' | jq -r '.version'
  23. }
  24. # retrieve all versions from artifactory
  25. artifactory_artifacts() {
  26. local artifacts_url=$1
  27. local regex=$2
  28. curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)'
  29. }
  30. # retrieve current from artifactory
  31. artifactory_current_version() {
  32. local artifacts_url=$1
  33. local regex=$2
  34. curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' | jq '[.[length-1] | {version: .version}]'
  35. }
  36. # check provided version returning all version
  37. artifactory_versions() {
  38. local artifacts_url=$1
  39. local regex=$2
  40. curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' | jq '[.[] | {version: .version}]'
  41. }
  42. check_version() {
  43. local artifacts_url=$1
  44. local regex=$2
  45. local version=$3
  46. result=$(artifactory_versions "$artifacts_url" "$regex")
  47. echo $result | jq --arg v "$version" '[foreach .[] as $item ([]; $item ; if $item.version >= $v then $item else empty end)]'
  48. }
  49. # check provided version returning all version
  50. artifactory_files() {
  51. local artifacts_url=$1
  52. local regex="(?<uri>$2)"
  53. curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' | jq '[.[] | {uri: .uri, version: .version}]'
  54. }
  55. in_file_with_version() {
  56. local artifacts_url=$1
  57. local regex="(?<uri>$2)"
  58. local version=$3
  59. result=$(artifactory_files "$artifacts_url" "$regex")
  60. echo $result | jq --arg v "$version" '[foreach .[] as $item ([]; $item ; if $item.version == $v then $item else empty end)]'
  61. }
  62. check_file_with_version() {
  63. local artifacts_url=$1
  64. local regex=$2
  65. local version=$3
  66. result=$(artifactory_versions "$artifacts_url" "$regex")
  67. echo $result | jq --arg v "$version" '[foreach .[] as $item ([]; $item ; if $item.version == $v then $item else empty end)]'
  68. }
  69. version=$(applyRegex_version "carshare-(?<module>admin|api|customer)-(?<version>.*).tar.gz" "carshare-api-1.0.0-rc.0.tar.gz")
  70. echo "version -> $version"
  71. url=http://localhost:8081/artifactory/api/storage/UrbanActive/Products/Maven/admin
  72. echo "Testing retrieving artifactis with version group"
  73. echo $(artifactory_artifacts "$url" "carshare-(admin|api|customer)-(?<version>.*).tar.gz")
  74. echo "Testing retrieving artifactis with module and version group"
  75. echo $(artifactory_artifacts "$url" "carshare-(?<module>admin|api|customer)-(?<version>.*).tar.gz")
  76. echo "Testing retrieving current version"
  77. echo $(artifactory_current_version "$url" "carshare-(admin|api|customer)-(?<version>.*).tar.gz")
  78. echo "Testing check version"
  79. result=$(artifactory_versions "$url" "carshare-(admin|api|customer)-(?<version>.*).tar.gz")
  80. echo $result
  81. result='[ { "version": "1.0.0-rc.0" }, { "version": "1.0.0.2" }, { "version": "1.0.0.3" } ]'
  82. echo "Testing check by version output"
  83. echo $result | jq '[foreach .[] as $item ([]; $item ; if $item.version >= "1.0.0.2" then $item else empty end)]'
  84. echo "Testing artifactory files"
  85. result=$(artifactory_files "$url" "carshare-(admin|api|customer)-(?<version>.*).tar.gz")
  86. echo $result
  87. echo "Testing in with good version"
  88. result=$(in_file_with_version "$url" "carshare-(admin|api|customer)-(?<version>.*).tar.gz" "1.0.0.2")
  89. echo $result
  90. echo "############### Testing check by version output function"
  91. url="-u admin:password http://192.168.1.224:8081/artifactory/api/storage/libs-snapshot-local/Pivotal"
  92. echo $(check_version "$url" "carshare-(admin|api|customer)-(?<version>.*).tar.gz" "1.0.0.2")