Skip to content
This repository has been archived by the owner on May 31, 2018. It is now read-only.

Commit

Permalink
Move JSON functions to a new script
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelgv committed Apr 10, 2017
1 parent 426d9ff commit 5cf1971
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 84 deletions.
89 changes: 89 additions & 0 deletions libpacaur/json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash
#
# json.sh - functions related to JSON operations
#

declare -A jsoncache

##
# Configure JSON cache for list of packages.
#
# usage: SetJson( $aur_packages )
##
SetJson() {
# global json
if [[ -z "${jsoncache[$@]}" ]]; then
jsoncache[$@]="$(DownloadJson $@)"
fi
json="${jsoncache[$@]}"
}

##
# Download JSON information of the list of packages.
#
# usage: DownloadJson( $aur_packages )
#
# NOTE: This function prints downloaded JSON information, this information can
# be stored in a array. For example:
# json_array[$packages]="$(DownloadJson $packages)"
##
DownloadJson() {
local urlencodedpkgs urlargs urlcurl urlarg urlmax j
urlencodedpkgs=($(sed 's/+/%2b/g;s/@/%40/g' <<< $@)) # pkgname consists of alphanum@._+-
urlarg='&arg[]='
urlargs="$(printf "$urlarg%s" "${urlencodedpkgs[@]}")"
urlmax=8125
# ensure the URI length is shorter than 8190 bytes (52 for AUR path, 13 reserved)
if [[ "${#urlargs}" -lt $urlmax ]]; then
curl -sfg --compressed -C 0 "https://$aururl$aurrpc$urlargs"
else
# split and merge json stream
j=0
for i in "${!urlencodedpkgs[@]}"; do
if [[ $((${#urlcurl[$j]} + ${#urlencodedpkgs[$i]} + ${#urlarg})) -ge $urlmax ]]; then
j=$(($j + 1))
fi
urlcurl[$j]=${urlcurl[$j]}${urlarg}${urlencodedpkgs[$i]}
done
urlargs="$(printf "https://$aururl$aurrpc%s " "${urlcurl[@]}")"
curl -sfg --compressed -C 0 $urlargs | sed 's/\(]}{\)\([A-Za-z0-9":,]\+[[]\)/,/g;s/\("resultcount":\)\([0-9]\+\)/"resultcount":0/g'
fi
}

##
# Query information from the JSON cache. This function has several formatting
# options:
# - "var" : print output formatted to be stored in a variable, process
# the entire JSON cache.
# - "varvar" : print output formatted to be stored in a variable, process
# only one package.
# - "array" : print output formatted to be stored in an array, process
# the entire JSON cache.
# - "arrayvar": print output formatted to be stored in an array, process
# only one package.
#
# usage: GetJson( $option, $json_info, $query, {$aur_package} )
#
# NOTE: This function prints formatted JSON information, this information can
# be stored in a array or variable depending on the option given. For example:
# version="$(GetJson "varvar" $json "Version" $package)"
##
GetJson() {
if json_verify -q <<< "$2"; then
case "$1" in
var)
json_reformat <<< "$2" | tr -d "\", " | grep -Po "$3:.*" | sed -r "s/$3:/$3#/g" | awk -F "#" '{print $2}';;
varvar)
json_reformat <<< "$2" | tr -d ", " | sed -e "/\"Name\":\"$4\"/,/}/!d" | \
tr -d "\"" | grep -Po "$3:.*" | sed -r "s/$3:/$3#/g" | awk -F "#" '{print $2}';;
array)
json_reformat <<< "$2" | tr -d ", " | sed -e "/^\"$3\"/,/]/!d" | tr -d '\"' \
| tr '\n' ' ' | sed "s/] /]\n/g" | cut -d' ' -f 2- | tr -d '[]"' | tr -d '\n';;
arrayvar)
json_reformat <<< "$2" | tr -d ", " | sed -e "/\"Name\":\"$4\"/,/}/!d" | \
sed -e "/^\"$3\"/,/]/!d" | tr -d '\"' | tr '\n' ' ' | cut -d' ' -f 2- | tr -d '[]';;
esac
else
Note "e" $"Failed to parse JSON"
fi
}
84 changes: 0 additions & 84 deletions libpacaur/pkgs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -453,87 +453,3 @@ GetInstallScripts() {
installscriptspath=($(find "$clonedir/$1/" -maxdepth 1 -name "*.install"))
[[ -n "${installscriptspath[@]}" ]] && installscripts=($(basename -a ${installscriptspath[@]}))
}

declare -A jsoncache
##
# Configure JSON cache for list of packages.
#
# usage: SetJson( $aur_packages )
##
SetJson() {
# global json
if [[ -z "${jsoncache[$@]}" ]]; then
jsoncache[$@]="$(DownloadJson $@)"
fi
json="${jsoncache[$@]}"
}

##
# Download JSON information of the list of packages.
#
# usage: DownloadJson( $aur_packages )
#
# NOTE: This function prints downloaded JSON information, this information can
# be stored in a array. For example:
# json_array[$packages]="$(DownloadJson $packages)"
##
DownloadJson() {
local urlencodedpkgs urlargs urlcurl urlarg urlmax j
urlencodedpkgs=($(sed 's/+/%2b/g;s/@/%40/g' <<< $@)) # pkgname consists of alphanum@._+-
urlarg='&arg[]='
urlargs="$(printf "$urlarg%s" "${urlencodedpkgs[@]}")"
urlmax=8125
# ensure the URI length is shorter than 8190 bytes (52 for AUR path, 13 reserved)
if [[ "${#urlargs}" -lt $urlmax ]]; then
curl -sfg --compressed -C 0 "https://$aururl$aurrpc$urlargs"
else
# split and merge json stream
j=0
for i in "${!urlencodedpkgs[@]}"; do
if [[ $((${#urlcurl[$j]} + ${#urlencodedpkgs[$i]} + ${#urlarg})) -ge $urlmax ]]; then
j=$(($j + 1))
fi
urlcurl[$j]=${urlcurl[$j]}${urlarg}${urlencodedpkgs[$i]}
done
urlargs="$(printf "https://$aururl$aurrpc%s " "${urlcurl[@]}")"
curl -sfg --compressed -C 0 $urlargs | sed 's/\(]}{\)\([A-Za-z0-9":,]\+[[]\)/,/g;s/\("resultcount":\)\([0-9]\+\)/"resultcount":0/g'
fi
}

##
# Query information from the JSON cache. This function has several formatting
# options:
# - "var" : print output formatted to be stored in a variable, process
# the entire JSON cache.
# - "varvar" : print output formatted to be stored in a variable, process
# only one package.
# - "array" : print output formatted to be stored in an array, process
# the entire JSON cache.
# - "arrayvar": print output formatted to be stored in an array, process
# only one package.
#
# usage: GetJson( $option, $json_info, $query, {$aur_package} )
#
# NOTE: This function prints formatted JSON information, this information can
# be stored in a array or variable depending on the option given. For example:
# version="$(GetJson "varvar" $json "Version" $package)"
##
GetJson() {
if json_verify -q <<< "$2"; then
case "$1" in
var)
json_reformat <<< "$2" | tr -d "\", " | grep -Po "$3:.*" | sed -r "s/$3:/$3#/g" | awk -F "#" '{print $2}';;
varvar)
json_reformat <<< "$2" | tr -d ", " | sed -e "/\"Name\":\"$4\"/,/}/!d" | \
tr -d "\"" | grep -Po "$3:.*" | sed -r "s/$3:/$3#/g" | awk -F "#" '{print $2}';;
array)
json_reformat <<< "$2" | tr -d ", " | sed -e "/^\"$3\"/,/]/!d" | tr -d '\"' \
| tr '\n' ' ' | sed "s/] /]\n/g" | cut -d' ' -f 2- | tr -d '[]"' | tr -d '\n';;
arrayvar)
json_reformat <<< "$2" | tr -d ", " | sed -e "/\"Name\":\"$4\"/,/}/!d" | \
sed -e "/^\"$3\"/,/]/!d" | tr -d '\"' | tr '\n' ' ' | cut -d' ' -f 2- | tr -d '[]';;
esac
else
Note "e" $"Failed to parse JSON"
fi
}

0 comments on commit 5cf1971

Please sign in to comment.