This is an open pastebin for users to use.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

80 lines
2.3 KiB

#!/bin/bash
# File: aninixpb
#
# Description: This file creates a pastebin
#
# Package: AniNIX::Foundation/Snippets
# Copyright: WTFPL
#
# Author: DarkFeather <darkfeather@aninix.net>
unset dryRun
snipDir="/opt/aninix/Snippets"
function Usage() {
### Show helptext
### param retcode: what to return
retcode="$1"
echo "Usage: $0"
echo " $0 -d somedir"
echo " $0 -h"
echo "Add -v for verbosity."
exit $retcode
}
function SetupCheckout() {
### Setup the checkout
# Make everyone able to work the checkout directory.
if [ ! -d "$snipDir" ]; then
sudo mkdir "$snipDir"
sudo chmod 0777 "$snipDir"
git clone gitea@aninix.net:AniNIX/Snippets.git "$snipDir"
sudo chmod -R 1777 "$snipDir"
fi
# Fetch everything
git -C "$snipDir" fetch --all --prune
git -C "$snipDir" checkout master
# We need to suppress the reset on non-dryruns so that local testing can work.
if [ -z "$dryRun" ]; then
git -C "$snipDir" reset --hard origin/master
fi
# Get in sync
git -C "$snipDir" pull
}
function NewSnip() {
### Create a new snip
### param stdin: read from stdin
# Choose the next snip as a random hash.
SetupCheckout
# Follow Git and use random strings instead of increments. Easier to avoid a collision and more performant.
nextSnip="${snipDir}/$(/usr/sbin/pwgen -A 8 1).snip"
# Pull stdin into the snippet
cat > "$nextSnip"
# Disable writes on dry runs
if [ -z "$dryRun" ]; then
# Add, commit, and push to publish the file.
git -C "$snipDir" add -A && git -C "$snipDir" commit -m "Adding $nextSnip via $0" && git -C "$snipDir" push origin master
# Inform the user if something went wrong.
if [ $? -ne 0 ]; then
echo Ran into a conflict with git. Please try again.
exit 2
fi
fi
# Provide a link
echo "New snippet created at https://foundation.aninix.net/AniNIX/Snippets/src/branch/master/$(basename "$nextSnip") from stdin"
}
# Parse arguments
while getopts 'd:Nhv' OPTION; do
case "$OPTION" in
d) snipDir="${OPTARG}" ;;
N) dryRun=1 ;;
h) echo "Create a pastebin on the AniNIX"; Usage 0 ;;
v) set -x ;;
esac
done
# If we haven't exited, new snip needed
NewSnip