Last active
April 23, 2021 00:42
-
-
Save sbrl/4b94436b2699d3a61e8a2ad929665aa7 to your computer and use it in GitHub Desktop.
A Simple CLI for Pepperminty Wiki. In development to test Pepperminty Wiki's machine friendlyness :D #cli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
########################## | |
### Colour Definitions ### | |
#### ANSI color codes #### | |
RS="\033[0m" # reset | |
HC="\033[1m" # hicolor | |
UL="\033[4m" # underline | |
INV="\033[7m" # inverse background and foreground | |
FBLK="\033[30m" # foreground black | |
FRED="\033[31m" # foreground red | |
FGRN="\033[32m" # foreground green | |
FYEL="\033[33m" # foreground yellow | |
FBLE="\033[34m" # foreground blue | |
FMAG="\033[35m" # foreground magenta | |
FCYN="\033[36m" # foreground cyan | |
FWHT="\033[37m" # foreground white | |
BBLK="\033[40m" # background black | |
BRED="\033[41m" # background red | |
BGRN="\033[42m" # background green | |
BYEL="\033[43m" # background yellow | |
BBLE="\033[44m" # background blue | |
BMAG="\033[45m" # background magenta | |
BCYN="\033[46m" # background cyan | |
BWHT="\033[47m" # background white | |
CCMD="${HC}${FYEL}"; # Command names | |
CTOKEN="${HC}${FCYN}"; # Tokens | |
if [[ "$#" -eq "" ]]; then | |
echo -e "${HC}${FRED}P${FWHT}e${FRED}p${FWHT}p${FRED}e${FWHT}r${FRED}m${FWHT}i${FRED}n${FWHT}t${FRED}y${FWHT} W${FRED}i${FWHT}k${FRED}i${FWHT} CLI${RS}"; | |
echo -e " by Starbeamrainbowlabs"; | |
echo -e ""; | |
echo -e "Usage: "; | |
echo -e " peppermint <action> <options>"; | |
echo -e ""; | |
echo -e "Actions: "; | |
echo -e " ${CCMD}connect${RS} ${CTOKEN}{alias}${RS} ${CTOKEN}{url to wiki}${RS}"; | |
echo -e " Interactively connect to the specified wiki. Aliases the given url to the specified string. Asks if you want to login."; | |
echo -e ""; | |
echo -e " ${CCMD}disconnect${RS} ${CTOKEN}{alias}${RS}"; | |
echo -e " Disconnects & logs out from the specified wiki."; | |
echo -e ""; | |
echo -e " ${CCMD}use${RS} ${CTOKEN}{alias}${RS}"; | |
echo -e " Sets the specified alias to be the default wiki for this session."; | |
echo -e ""; | |
echo -e " ${CCMD}list${RS}"; | |
echo -e " Lists all the pages on the specified wiki."; | |
echo -e ""; | |
echo -e " ${CCMD}view${RS} ${CTOKEN}{page-name}${RS} [${CTOKEN}{revision-number}${RS}]"; | |
echo -e " Fetches the content of the specified page. Optionally takes the revision number to display. Fetches the last version if not specified."; | |
echo -e ""; | |
echo -e " ${CCMD}revisions${RS} ${CTOKEN}{page-name}${RS}"; | |
echo -e " Shows a list of revisions for the specified page."; | |
echo -e ""; | |
echo -e " ${CCMD}search${RS} ${CTOKEN}{query}${RS} (coming soon)"; | |
echo -e " Searches the wiki for the specified query string. Displays the results."; | |
echo -e ""; | |
exit 1; | |
fi | |
action=$1; | |
alias_file="/tmp/peppermint-${USER}/aliases"; | |
cookie_file="/tmp/peppermint-${USER}/cookies"; | |
mkdir -p $(dirname "${alias_file}"); | |
touch "${alias_file}" "${cookie_file}"; | |
chmod 0700 "${alias_file}"; | |
chmod 0600 "${cookie_file}"; | |
source "${alias_file}"; | |
case ${action} in | |
connect ) | |
if [[ "$#" -lt 3 ]]; then | |
echo -e "Usage:"; | |
echo -e " peppermint ${CCMD}connect${RS} ${CTOKEN}{alias}${RS} ${CTOKEN}{url-of-wiki}${RS}"; | |
exit 1; | |
fi | |
new_alias=$2; | |
wiki_url=$3; | |
grep "alias_${new_alias}=" "${alias_file}" >/dev/null 2>&1; | |
if [[ "$#" -eq 0 ]]; then | |
echo "Error: You are already connected to ${wiki_url}! Try disconnecting first."; | |
exit 2; | |
fi | |
wiki_http_status="$(curl -I "${wiki_url}" --silent | head -n1 | cut -d' ' -f2)"; | |
if [[ "${wiki_http_status}" -lt 200 ]] || [[ "${wiki_http_status}" -gt 299 ]]; then | |
read -p "This wiki may require you to login. Do so now? [Yn]" result; | |
echo ${result} | grep -i n; | |
[[ "$?" -eq "0" ]] && echo "abort." && exit 127; | |
read -p "Username: " wiki_username; | |
read -s -p "Password for ${wiki_username}: " wiki_password; | |
echo -e ""; | |
echo -n "Logging in - "; | |
curl --cookie "${cookie_file}" --cookie-jar "${cookie_file}" "$wiki_url?action=checklogin" --data "user=${wiki_username}" --data "pass=${wiki_password}"; | |
if [[ "$?" -ne "0" ]]; then | |
echo "Error: Something has gone wrong when logging in! Perhaps you typed your username or password incorrectly?"; | |
exit 2; | |
fi | |
echo "done"; | |
fi | |
echo "alias_${new_alias}=\"${wiki_url}\";" >>${alias_file}; | |
echo "Aliased ${wiki_url} to ${new_alias}"; | |
;; | |
disconnect ) | |
curl --silent --cookie "${cookie_file}" --cookie-jar "${cookie_file}" "${alias_default}?action=logout" >/dev/null; | |
grep -v "${alias_default}" "${alias_file}" >>$(dirname ${alias_file})/alias_file_temp; | |
mv "$(dirname ${alias_file})/alias_file_temp" "${alias_file}"; | |
;; | |
use ) | |
if [[ "$#" -lt 2 ]]; then | |
echo -e "Usage: "; | |
echo -e " peppermint ${CCMD}use${RS} ${CTOKEN}{alias}${RS}"; | |
exit 2; | |
fi | |
new_alias=$2; | |
grep "alias_${new_alias}=" "${alias_file}" >/dev/null 2>&1; | |
if [[ "$?" -ne 0 ]]; then | |
echo "Error: The alias ${new_alias} doesn't exist yet! Try connecting to it first."; | |
exit 2; | |
fi | |
echo "alias_default=\"\${alias_${new_alias}}\";" >>${alias_file}; | |
echo "Made ${new_alias} the default alias"; | |
;; | |
list ) | |
curl --silent --cookie "${cookie_file}" --cookie-jar "${cookie_file}" "${alias_default}?action=list&format=text"; | |
echo -e ""; | |
;; | |
view ) | |
if [[ "$#" -lt 2 ]]; then | |
echo -e "Usage: "; | |
echo -e " peppermint ${CCMD}view${RS} ${CTOKEN}{page-name}${CTOKEN} [${CTOKEN}{revision-number}${RS}]"; | |
exit 2; | |
fi | |
page_name=$2; | |
revision=$3; | |
request_params="action=raw&page=${page_name}"; | |
if [ "${revision}" != "" ]; then | |
request_params="${request_params}&revision=${revision}"; | |
fi | |
curl --silent --cookie "${cookie_file}" --cookie-jar "${cookie_file}" "${alias_default}?${request_params}"; | |
;; | |
revisions ) | |
if [[ "$#" -lt 2 ]]; then | |
echo -e "Usage: "; | |
echo -e " peppermint ${CCMD}revisions${RS} ${CTOKEN}{page-name}${CTOKEN}"; | |
exit 2; | |
fi | |
page_name=$2; | |
curl --silent --cookie "${cookie_file}" --cookie-jar "${cookie_file}" "${alias_default}?action=history&page=${page_name}&format=csv" | column -ts,; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment