Skip to content

Instantly share code, notes, and snippets.

@Kubuxu
Created February 22, 2016 16:04
Show Gist options
  • Save Kubuxu/d0c133cbc0ea4693db67 to your computer and use it in GitHub Desktop.
Save Kubuxu/d0c133cbc0ea4693db67 to your computer and use it in GitHub Desktop.
Script to publish directory to filesAPI with timestamping support
#!/usr/bin/env bash
usage() {
echo "$0 [dir_to_publish [, target_dir_in_files_api]]"
echo "If there are no arguments file .ipfspublish in current working directory is loaded and evaluated."
echo "It has to assign value to PUBLIC_DIR and can assign value to TARGET_DIR."
echo "If there is no target directory specified its value is same as public directory."
exit 1
}
exec 5>&1 # fd 5 is alternative terminal output
if [ $# -eq 0 ]; then
[ ! -e .ipfspublish ] && usage
. .ipfspublish
[ -z "$PUBLIC_DIR" ] && usage
else
PUBLIC_DIR="$1"
TARGET_DIR="$2"
fi
echo "Publishing $PUBLIC_DIR into ${TARGET_DIR:=/$PUBLIC_DIR}."
echo
echo "Adding to IPFS:"
# use alternative terminal output to capture output of command and still show the result on screen
# stderr has to be nulled as progress bar does not work well doing that
HASH="/ipfs/$(ipfs add -r "$PUBLIC_DIR" 2>/dev/null | tee /dev/fd/5 | tail -1 | cut -d' ' -f 2)"
echo
echo "Hash of $PUBLIC_DIR in IPFS: $HASH."
# create parent directory if it does not exist
ipfs files stat "$TARGET_DIR" &>/dev/null || (echo "Creating directory $TARGET_DIR"; ipfs files mkdir -p "$TARGET_DIR")
DATE=$(date -u +%FT%TZ) # ISO8601
echo "Inserting $HASH under $TARGET_DIR/$DATE."
# insert the new release under current date
ipfs files cp "$HASH" "$TARGET_DIR/$DATE"
# remove old current release
# TODO(Kubuxu): remove this when --force flag is in files API
ipfs files stat "$TARGET_DIR/current" &>/dev/null && (echo "Removing old $TARGET_DIR/current."; ipfs files rm -r "$TARGET_DIR/current")
echo "Copying $TARGET_DIR/$DATE to $TARGET_DIR/current."
ipfs files cp "$TARGET_DIR/$DATE" "$TARGET_DIR/current"
echo "Removing original pin."
ipfs pin rm "$HASH" &>/dev/null
echo
for DIR in $TARGET_DIR $MORE_DIRS_TO_PRINT; do
echo "New hash of $DIR directory is /ipfs/$(ipfs files stat $DIR | head -1)."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment