fwupdate: add json support when listing versions

This commit is contained in:
Calin Crisan 2018-08-01 23:08:21 +03:00
parent 4bb200c13b
commit bf10f95711
3 changed files with 18 additions and 10 deletions

View File

@ -4,7 +4,7 @@
#### usage #### #### usage ####
function exit_usage() { function exit_usage() {
echo "Usage: fwupdate versions [-u] (lists available versions, optionally showing the URL)" echo "Usage: fwupdate versions [-j] (lists available versions, optionally outputting json)"
echo " fwupdate current (shows the current version" echo " fwupdate current (shows the current version"
echo " fwupdate download <version|url> (downloads a firmware version)" echo " fwupdate download <version|url> (downloads a firmware version)"
echo " fwupdate extract (extracts the downloaded firmware archive)" echo " fwupdate extract (extracts the downloaded firmware archive)"
@ -66,14 +66,14 @@ DD_PID_FILE=dd.pid
function show_versions() { function show_versions() {
source $OS_CONF source $OS_CONF
board=$(cat $SYS_BOARD_FILE) board=$(cat $SYS_BOARD_FILE)
show_url=$1 show_json=$1
# the /usr/libexec/list-versions-* helpers return a table with the following format: # the /usr/libexec/list-versions-* helpers return a table with the following format:
# <version>|<prerelease>|<board>|<url> # <version>|<prerelease>|<board>|<url>
versions=$(FW_USERNAME=$os_firmware_username FW_PASSWORD=$os_firmware_password \ versions=$(FW_USERNAME=$os_firmware_username FW_PASSWORD=$os_firmware_password \
/usr/libexec/list-versions-$os_firmware_method $os_firmware_repo) /usr/libexec/list-versions-$os_firmware_method $os_firmware_repo)
for version in $versions; do for version in ${versions[@]}; do
OIFS=$IFS OIFS=$IFS
IFS="|" IFS="|"
varr=($version) varr=($version)
@ -85,8 +85,12 @@ function show_versions() {
continue # skip other boards continue # skip other boards
fi fi
if [ "$show_url" == "true" ]; then if [ "$show_json" == "true" ]; then
echo ${varr[0]} ${varr[3]} echo "{\"version\": \"${varr[0]}\"," \
"\"url\": \"${varr[3]}\"," \
"\"board\": \"${varr[2]}\"," \
"\"prerelease\": ${varr[1]}," \
"\"date\": \"${varr[4]}\"}"
else else
echo ${varr[0]} echo ${varr[0]}
fi fi
@ -394,9 +398,9 @@ function new_version() {
case "$1" in case "$1" in
versions) versions)
show_url="false" show_json="false"
test "$2" == "-u" && show_url="true" test "$2" == "-j" && show_json="true"
show_versions $show_url show_versions $show_json
;; ;;
current) current)

View File

@ -20,7 +20,8 @@ test -n "$FW_USERNAME" && opts+=" --user $FW_USERNAME:$FW_PASSWORD"
url="https://api.bitbucket.org/2.0/repositories/$1/downloads?pagelen=100&_=$(date +%s)" url="https://api.bitbucket.org/2.0/repositories/$1/downloads?pagelen=100&_=$(date +%s)"
rtrimstr=$(for e in $extensions; do echo -n " | rtrimstr(\"$e\")"; done) rtrimstr=$(for e in $extensions; do echo -n " | rtrimstr(\"$e\")"; done)
jq_expr=".values[] | [{a: .name | split(\"-\"), url: .links.self.href}] | map((.a[2] $rtrimstr), \"false\", .a[1], .url) | join(\"|\")" jq_expr=".values[] | [{a: .name | split(\"-\"), url: .links.self.href, date: .created_on | split(\"T\")[0]}] |
map((.a[2] $rtrimstr), \"false\", .a[1], .url, .date) | join(\"|\")"
curl $opts $url | jq --raw-output "$jq_expr" | while read line; do echo "$line" | check_prerelease; done curl $opts $url | jq --raw-output "$jq_expr" | while read line; do echo "$line" | check_prerelease; done
exit ${PIPESTATUS[0]} exit ${PIPESTATUS[0]}

View File

@ -8,7 +8,10 @@ fi
opts="-s -S -f" opts="-s -S -f"
test -n "$FW_USERNAME" && opts+=" --user $FW_USERNAME:$FW_PASSWORD" test -n "$FW_USERNAME" && opts+=" --user $FW_USERNAME:$FW_PASSWORD"
url=https://api.github.com/repos/$1/releases url=https://api.github.com/repos/$1/releases
jq_expr='.[] | {version: .name, prerelease: .prerelease | tostring} + (.assets[] | {name: .name | split("-")[1], url: .browser_download_url}) | flatten | join("|")'
jq_expr='.[] | {version: .name, prerelease: .prerelease | tostring} +
(.assets[] | {name: .name | split("-")[1], url: .browser_download_url}) +
({date: .created_at | split("T")[0]}) | flatten | join("|")'
curl $opts $url | jq --raw-output "$jq_expr" curl $opts $url | jq --raw-output "$jq_expr"
exit ${PIPESTATUS[0]} exit ${PIPESTATUS[0]}