Skip to content

Commit

Permalink
cleanup ; minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
froger-me committed Jan 9, 2024
1 parent 8bbf90a commit c36aa3f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
38 changes: 20 additions & 18 deletions integration/dummy-generic/dummy-generic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

if [ "$1" == "status" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "false" ]; then
echo "Status: Not installed"
# halt the script

exit 0
elif [ "$1" == "status" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "true" ]; then
echo "Status: Installed"
# halt the script

exit 0
elif [ "$1" == "status" ]; then
echo "Status: Unknown"
# halt the script

exit 1
fi

Expand All @@ -21,11 +21,11 @@ fi
if [ "$1" == "install" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "false" ] && [ "$2" != "" ]; then
bash "$(dirname "$0")/wppus-api.sh" install $2
echo "Installed"
# halt the script

exit 0
elif [ "$1" == "install" ]; then
echo "Failed to install"
# halt the script

exit 1
fi

Expand All @@ -35,11 +35,11 @@ if [ "$1" == "uninstall" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_instal
# uninstall the package
bash "$(dirname "$0")/wppus-api.sh" uninstall
echo "Uninstalled"
# halt the script

exit 0
elif [ "$1" == "uninstall" ]; then
echo "Nothing to uninstall"
# halt the script

exit 1
fi

Expand All @@ -49,11 +49,11 @@ if [ "$1" == "activate" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_install
# activate the license
bash "$(dirname "$0")/wppus-api.sh" activate_license
echo "Activated"
# halt the script

exit 0
elif [ "$1" == "activate" ]; then
echo "The package is not installed"
# halt the script

exit 1
fi

Expand All @@ -63,11 +63,11 @@ if [ "$1" == "deactivate" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_insta
# activate the license
bash "$(dirname "$0")/wppus-api.sh" deactivate_license
echo "Deactivated"
# halt the script

exit 0
elif [ "$1" == "deactivate" ]; then
echo "The package is not installed"
# halt the script

exit 1
fi

Expand Down Expand Up @@ -95,39 +95,41 @@ if [ "$1" == "get_update_info" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_
# pretty print the response
echo "$info" | jq
echo ""
# halt the script

exit 0
elif [ "$1" == "get_update_info" ]; then
echo "The package is not installed"
# halt the script

exit 1
fi

### UPDATING THE PACKAGE ###

if [ "$1" == "update" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "true" ]; then
# update the package
bash "$(dirname "$0")/wppus-api.sh" check_for_updates
bash "$(dirname "$0")/wppus-api.sh" update
echo "Updated"
echo ""
bash "$(dirname "$0")/wppus-api.sh" get_update_info
# halt the script

exit 0
elif [ "$1" == "update" ]; then
echo "The package is not installed"
# halt the script

exit 1
fi

### USAGE ###

echo "Usage: bash \"$(dirname "$0")/wppus-api.sh\" [command] [arguments]"
echo "Usage: ./wppus-api.sh [command] [arguments]"
echo "Commands:"
echo " install [license] - install the package"
echo " uninstall - uninstall the package"
echo " activate - activate the license"
echo " deactivate - deactivate the license"
echo " get_update_info - output information about the remote package update"
echo " update - update the package if available"
# halt the script
echo " status - output the package status"
echo "Note: this package assumes it needs a license."

exit 1
55 changes: 34 additions & 21 deletions integration/dummy-generic/wppus-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
# It is just a collection of basic functions and snippets, and they do not
# perform the necessary checks to ensure data integrity ; they assume that all
# the requests are successful, and do not check paths or permissions.
# They also assume that the package necessitates a license key, stored in an
# environment variable WPPUS_GENERIC_PACKAGE_LICENSE
# They also assume that the package necessitates a license key.

# replace https://server.domain.tld/ with the URL of the server where
# WP Packages Update Server is installed in wppus.json
Expand All @@ -28,6 +27,7 @@ version=$(jq -r '.packageData.Version' "$(cd "$(dirname "$0")"; pwd -P)/wppus.js
license_key=$(jq -r '.licenseKey' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
# define license_signature from the wppus.json file
license_signature=$(jq -r '.licenseSignature' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")

# define the domain
if [[ "$(uname)" == "Darwin" ]]; then
# macOS
Expand All @@ -42,7 +42,6 @@ fi
function install() {
# add the license key to wppus.json
jq '.licenseKey = "'"$1"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"

# add a file '.installed' in current directory
touch "$(cd "$(dirname "$0")"; pwd -P)/.installed"
}
Expand All @@ -51,30 +50,33 @@ function install() {

function uninstall() {
local license=$license_key
license_signature=""

# remove the license key from wppus.json
jq '.licenseKey = ""' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
# remove the license signature from wppus.json
jq '.licenseSignature = ""' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
license_signature=""

# remove the file '.installed' from current directory
rm "$(cd "$(dirname "$0")"; pwd -P)/.installed"
}

### CHECKING IF THE PACKAGE IS INSTALLED ###

function is_installed() {

# check if the file '.installed exists in current directory
if [ -f "$(cd "$(dirname "$0")"; pwd -P)/.installed" ]; then

# return true
echo "true"
else

# return false
echo "false"
fi
}

### SENDNG AN API REQUEST ###
### SENDING AN API REQUEST ###

function send_api_request() {
# build the request url
Expand All @@ -91,11 +93,14 @@ function send_api_request() {

function urlencode() {
local old_lc_collate=$LC_COLLATE

LC_COLLATE=C

local length="${#1}"

for (( i = 0; i < length; i++ )); do
local c="${1:$i:1}"

case $c in
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
*) printf '%%%02X' "'$c" ;;
Expand All @@ -107,6 +112,7 @@ function urlencode() {

function urldecode() {
local url_encoded="${1}"

printf '%b' "${url_encoded//%/\\x}"
}

Expand All @@ -125,6 +131,7 @@ function check_for_updates() {
)
# make the request
local response=$(send_api_request "$endpoint" "${args[@]}")

# return the response
echo "$response"
}
Expand All @@ -144,8 +151,10 @@ function activate_license() {
local response=$(send_api_request "$endpoint" "${args[@]}")
# get the signature from the response
local signature=$(urldecode $(echo -n "$response" | jq -r '.license_signature'))

# add the license signature to wppus.json
jq '.licenseSignature = "'"$signature"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"

license_signature=$signature
}

Expand All @@ -160,10 +169,12 @@ function deactivate_license() {
"allowed_domains=$(urlencode "$domain")"
"package_slug=$(urlencode "$package_name")"
)

# make the request
local response=$(send_api_request "$endpoint" "${args[@]}")
send_api_request "$endpoint" "${args[@]}"
# remove the license signature from wppus.json
jq '.licenseSignature = ""' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"

license_signature=""
}

Expand All @@ -172,10 +183,12 @@ function deactivate_license() {
function download_update() {
# get the download url from the response in $1
local url=$(urldecode $(echo -n "$1" | jq -r '.download_url'))
# set the path to the downloaded file in /tmp/dummy-generic.zip
# set the path to the downloaded file
local output_file="/tmp/$zip_name"

# make the request
curl -sS -L -o $output_file "$url"

# return the path to the downloaded file
echo $output_file
}
Expand All @@ -186,36 +199,36 @@ function download_update() {
if [ "$1" == "get_version" ]; then
# return the version
echo "$version"
# halt the script

exit 0
fi

# check if the script was called with the argument "install"
if [ "$1" == "install" ]; then
# install the package
echo $(install "$2")
# halt the script

exit 0
fi

# check if the script was called with the argument "uninstall"
if [ "$1" == "uninstall" ]; then
# uninstall the package
echo $(uninstall)
# halt the script

exit 0
fi

# check if the script was called with the argument "is_installed"
if [ "$1" == "is_installed" ]; then
# check if the package is installed
echo $(is_installed)
# halt the script

exit 0
fi

# check if the script was called with the argument "check_for_updates"
if [ "$1" == "check_for_updates" ]; then
# check if the script was called with the argument "update"
if [ "$1" == "update" ]; then
# check for updates
response=$(check_for_updates)
# get the version from the response
Expand All @@ -226,6 +239,7 @@ if [ "$1" == "check_for_updates" ]; then
if [ "$(printf '%s\n' "$new_version" "$version" | sort -V | tail -n1)" != "$version" ]; then
# download the update
output_file=$(download_update "$response")

# extract the zip in /tmp/$(package_name)
unzip -q -o $output_file -d /tmp

Expand All @@ -235,10 +249,11 @@ if [ "$1" == "check_for_updates" ]; then
elif [[ "$OSTYPE" == "darwin"* ]]; then
OCTAL_MODE=$(stat -f '%p' "$package_script" | cut -c 4-6)
fi

# set the permissions of the new file to the permissions of the old file
chmod $OCTAL_MODE /tmp/$package_name/$package_name.sh

# move all the files except the update scripts (all languages) to the
# move all the files except the wppus-api (all languages) to the
# current directory ; the updated main script is in charge of
# overriding the update scripts by moving files around after update
for file in /tmp/$package_name *; do
Expand All @@ -248,7 +263,7 @@ if [ "$1" == "check_for_updates" ]; then
done

# add the license key to wppus.json
jq '.licenseKey = "'"$1"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
jq '.licenseKey = "'"$license_key"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
# add the license signature to wppus.json
jq '.licenseSignature = "'"$signature"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"

Expand All @@ -257,35 +272,33 @@ if [ "$1" == "check_for_updates" ]; then
# remove the zip
rm $output_file

# halt the script
exit 0
fi

# halt the script
exit 0
fi

# check if the script was called with the argument "activate_license"
if [ "$1" == "activate_license" ]; then
# activate the license
echo $(activate_license)
# halt the script

exit 0
fi

# check if the script was called with the argument "deactivate_license"
if [ "$1" == "deactivate_license" ]; then
# deactivate the license
echo $(deactivate_license)
# halt the script

exit 0
fi

# check if the script was called with the argument "get_update"
if [ "$1" == "get_update_info" ]; then
# get the update information
echo "$(check_for_updates)"
# halt the script

exit 0
fi

0 comments on commit c36aa3f

Please sign in to comment.