:-'Generic Commit'
This commit is contained in:
79
sos/sos
Executable file
79
sos/sos
Executable file
@ -0,0 +1,79 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# sos upload <server> <label:tag> <file>
|
||||
|
||||
# example:
|
||||
# sos upload tools.dropshell.app file:latest ./file.txt
|
||||
|
||||
# this will upload the file to the server, and return the download URL
|
||||
|
||||
|
||||
function show_help() {
|
||||
cat << EOF
|
||||
|
||||
sos is a script to upload files to a simple object storage server.
|
||||
|
||||
Usage:
|
||||
sos upload <server> <label:tag> <file>
|
||||
|
||||
Example:
|
||||
sos upload tools.dropshell.app file:latest ./file.txt
|
||||
|
||||
This will upload the file to the server, and return the download URL
|
||||
EOF
|
||||
exit 0
|
||||
}
|
||||
|
||||
function die() {
|
||||
echo "$@"
|
||||
exit 1
|
||||
}
|
||||
|
||||
|
||||
function upload() {
|
||||
if [ "$#" -ne 3 ]; then
|
||||
echo "Usage: sos upload <server> <label:tag> <file>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
server=$1
|
||||
label=$2
|
||||
file=$3
|
||||
|
||||
[ -f "$file" ] || die "File not found: $file"
|
||||
|
||||
# if the label doesn't have a tag, add :lastest
|
||||
if [[ ! "$label" =~ : ]]; then
|
||||
label="$label:latest"
|
||||
fi
|
||||
|
||||
# upload the file
|
||||
echo "Uploading $file to $server - $label..."
|
||||
|
||||
METADATA="{\"labeltags\":[\"$label\"],\"description\":\"Uploaded by sos\"}"
|
||||
|
||||
HASH=""
|
||||
JSON=$(curl -s -X POST -F "file=@$file" -F "metadata=$METADATA" "https://$server/$label") || die "Failed to upload $file to $server - $label"
|
||||
HASH=$(echo "$JSON" | jq -r '.hash')
|
||||
JSON2=$(curl -s "https://$server/meta/$HASH") || die "Failed to get meta for $HASH"
|
||||
FILENAME=$(echo "$JSON2" | jq -r '.filename')
|
||||
|
||||
echo "Download URL: https://$server/$label > $FILENAME"
|
||||
echo "Alternative: https://$server/$HASH > $FILENAME"
|
||||
echo "Hash: $HASH"
|
||||
}
|
||||
|
||||
# if no arguments, show help
|
||||
[ "$#" -gt 0 ] || show_help
|
||||
|
||||
CMD="$1"
|
||||
shift
|
||||
|
||||
if [ "$CMD" == "upload" ]; then
|
||||
upload "$@"
|
||||
exit $?
|
||||
fi
|
||||
|
||||
die "Unknown command: $CMD"
|
Reference in New Issue
Block a user