From beddd84836441809abf76878e47a0b2a92b0c4d8 Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 20 Mar 2022 14:05:35 -0700 Subject: [PATCH] + Working neocities upload script --- scripts/neocities.sh | 46 +++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/scripts/neocities.sh b/scripts/neocities.sh index 963e061..20e1e50 100644 --- a/scripts/neocities.sh +++ b/scripts/neocities.sh @@ -7,6 +7,7 @@ upload_file() { + set -e file_path="$1" uri_path="${file_path:6}" @@ -15,12 +16,13 @@ upload_file() { return 1 fi - # Sub-routine uploads a single file for - base='https://neocities.org/api/upload' # Curl gives us the exit code for this sub-routine's exit code - curl \ + curl -s \ -H "Authorization: Bearer ${API_KEY}" \ - -F "${uri_path}=@${file_path}" + -F "${uri_path}=@${file_path}" \ + 'https://neocities.org/api/upload' + + return $! } feed_files() { @@ -34,20 +36,34 @@ feed_files() { # Main uploading section while read -r line; do if upload_file "$line"; then - echo Uploaded $line + echo "Pass $line" else - echo Failed $line >&2 + echo "Failed $line" > /dev/stderr fi - sleep $SLEEP_TIME done < /dev/stdin } -if [ -d public/ ];then - echo Found public directory - # Upload all files that we wish to upload - find public/ -type f | grep '\.[a-z]*' | feed_files -else - echo No public directory found - exit 1 -fi +verify_api_key() { + if [ -z "$API_KEY" ];then + echo '$API_KEY env var is not set' > /dev/stderr + exit 1 + fi +} +verify_public_dir() { + # Check that the public directory is present + if [ ! -d public/ ];then + echo 'No public directory found!' > /dev/stderr + exit 1 + fi +} + +# Pre flight checks to make sure we don't try uploading without +# the required keys/files in place +verify_public_dir; verify_api_key + +# Only upload items that have an extension(files) in the case +# of data generated by hugo +find public/ -type f | \ + grep '\.[a-z]*' | \ + feed_files