create-vendor.sh

#!/bin/sh
if [ "$#" -lt 2 ]
then
	echo "Usage: $0 JWT_FILE COMPANY_NAME [EMAIL] [WEBSITE]"
	exit 1
fi

jwt_filepath=$1
COMPANY_NAME=$2
EMAIL="${3:-`echo $COMPANY_NAME@sw.gy | sed -r 's/[^a-zA-Z0-9]+/_/g; s/(^_|_$)//g; y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`}"
WEBSITE="${4:-https://sw.gy}"
SERVER_URL="${SWGY_SERVER:-http://127.0.0.1}"

# BEGIN: JWT Loading

if ! test -f $jwt_filepath;
then
  echo "Cannot read $jwt_filepath"
  exit 1
fi
#
# Read the JWT token from the JSON file
access_token=$(jq -r '.access_token' "$jwt_filepath")
token_type=$(jq -r '.token_type' "$jwt_filepath")

# Check if token_type is "bearer"
if [ "$token_type" != "bearer" ]; then
    echo "Error: invalid token_type"
    exit 1
fi
#
# Set the Authorization header to the JWT token
authorization="Authorization: Bearer $access_token"

# END: JWT Loading

curl -k -vvv -X "PUT" -H "$authorization" -H "Content-Type: application/json" -d \
	"{\"name\":\"${COMPANY_NAME}\", \"email\":\"${EMAIL}\",\"website\":\"${WEBSITE}\"}" \
 ${SERVER_URL}/api/vendors