diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3fd7c21 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cookies.txt diff --git a/tesoft.bash b/tesoft.bash new file mode 100755 index 0000000..8f2e94e --- /dev/null +++ b/tesoft.bash @@ -0,0 +1,180 @@ +#!/bin/bash + +cookie_path=./cookies.txt + +print_help() { + echo "tesoft.dev helper script" + echo "" + echo "usage: $0 " + echo "" + echo "login into the webpage with a password:" + echo "> $0 login" + echo "" + echo "logout from the webpage:" + echo "> $0 logout" + echo "" + echo "create a new blog article:" + echo "> $0 upload ./article.tar" + echo "" + echo "update an existing blog article:" + echo "> $0 patch 123 ./article.tar" + echo "" + echo "publish an existing blog article:" + echo "> $0 publish 123" + echo "" + echo "unpublish an existing blog article:" + echo "> $0 unpublish 123" + echo "" + echo "delete an existing blog article:" + echo "> $0 drop 123" + echo "" +} + +login() { + echo -n "password: " + read -s password + echo "" + out=$(curl -v -f -H 'Content-Type: application/json' -d "{\"password\":\"$password\"}" "$URL/api/login" -c "$cookie_path" 2>&1) + if [ $? -ne 0 ]; then + echo "$out" >&2 + exit 1 + fi + + echo "ok" +} + +logout() { + out=$(curl -v -f -X POST "$URL/api/logout" -b "$cookie_path" 2>&1) + if [ $? -ne 0 ]; then + echo "$out" >&2 + exit 1 + fi + + rm -f "$cookie_path" + echo "ok" +} + +upload() { + if [ -z "$1" ]; then + echo "error: article file is not passed" >&2 + exit 1 + fi + if ! [ -f "$1" ]; then + echo "error: article file not found" >&2 + exit 1 + fi + + out=$(curl -v -f -X PUT -F "file=@$1" "$URL/api/blog" -b "$cookie_path" 2>&1) + if [ $? -ne 0 ]; then + echo "$out" >&2 + exit 1 + fi + + echo "ok, id = $(echo "$out" | tail -1 | grep -oP '[0-9]+')" +} + +patch() { + if [ -z "$1" ]; then + echo "error: article id is not passed" >&2 + exit 1 + fi + if [ -z "$2" ]; then + echo "error: article file is not passed" >&2 + exit 1 + fi + if ! [ -f "$2" ]; then + echo "error: article file not found" >&2 + exit 1 + fi + + out=$(curl -v -f -X PATCH -F "file=@$2" "$URL/api/blog/$1" -b "$cookie_path" 2>&1) + if [ $? -ne 0 ]; then + echo "$out" >&2 + exit 1 + fi + + echo "ok" +} + +publish() { + if [ -z "$1" ]; then + echo "error: article id is not passed" >&2 + exit 1 + fi + + out=$(curl -v -f -X POST "$URL/api/blog/$1/publish" -b "$cookie_path" 2>&1) + if [ $? -ne 0 ]; then + echo "$out" >&2 + exit 1 + fi + + echo "ok" +} + +unpublish() { + if [ -z "$1" ]; then + echo "error: article id is not passed" >&2 + exit 1 + fi + + out=$(curl -v -f -X POST "$URL/api/blog/$1/unpublish" -b "$cookie_path" 2>&1) + if [ $? -ne 0 ]; then + echo "$out" >&2 + exit 1 + fi + + echo "ok" +} + +drop() { + if [ -z "$1" ]; then + echo "error: article id is not passed" >&2 + exit 1 + fi + + out=$(curl -v -f -X DELETE "$URL/api/blog/$1" -b "$cookie_path" 2>&1) + if [ $? -ne 0 ]; then + echo "$out" >&2 + exit 1 + fi + + echo "ok" +} + +if [ "$#" -eq 0 ] || [ "$1" = "--help" ]; then + print_help + exit 0 +fi + +if [ -z "$URL" ]; then + URL="https://tesoft.dev" +fi + +case "$1" in + login) + login + ;; + logout) + logout + ;; + upload) + upload "$2" + ;; + patch) + patch "$2" "$3" + ;; + publish) + publish "$2" + ;; + unpublish) + unpublish "$2" + ;; + drop) + drop "$2" + ;; + *) + echo "error: unknown action" >&2 + exit 1 + ;; +esac +