Skip to content

Instantly share code, notes, and snippets.

@DonBattery
Last active November 3, 2018 23:24
Show Gist options
  • Save DonBattery/238821cda7846085df40ffe9989ceefe to your computer and use it in GitHub Desktop.
Save DonBattery/238821cda7846085df40ffe9989ceefe to your computer and use it in GitHub Desktop.
Turbo Gists with python-gist and PoopDB

A wrapper for python-gist utility, using poopdb for named gists

https://pypi.org/project/python-gist/

You will need an access token from GitHub for python-gist, you can get one under
Settings > Developper Settings > Personal access tokens
Create a .gist file under your HOME folder like this:

[gist]
token: yourtoken
editor: youreditor

Then create a directory named "poopdb" under your home folder and copy gistR.sh and poopdb.engine to it.

Copy this to your .bashrc to get the functionality

if [ -d "$HOME/poopdb" ]; then
  mkdir -p $HOME/poopdb/db
  export POOPDB_DIR="$HOME/poopdb/db"
  export POOPDB_ENGINE="$HOME/poopdb/poopdb.engine"
  . "$HOME/poopdb/gistR.sh"
else
  echo "Cannot find poopdb at $HOME/poopdb"
fi

Or execute install.sh which will do the same

chmod +x install.sh
./install.sh

You can populate the poopdab with existing gists by executing

chmod +x populate.sh
./populate.sh

Usage

  • gi : shorthand for the original gist command
  • gil : list all gists
  • gill : list all gists and ID
  • gic : create new gist gic name "description" files...
  • girm : remove a gist grim name
  • gig : get gist content gig name or gig name filename
  • gif : get filenames in a gist gif name
  • giclo : clone a gist giclo name
# Wrapper for python-gist command using poopdb for named gists
# Import the poopdb
. $POOPDB_ENGINE
create_gist () {
url=`gist create "$2" "${@:3}"` &&\
poopdb_set $1 ${url#*https://gist.github.com/} &&\
echo "Gist created $1 : `poopdb_get $1`" &&\
return 0
echo "Error creating Gist $1"
return 1
}
remove_gist () {
gist delete `poopdb_get $1` &&\
echo "Gist removed $1 : `poopdb_get $1`" &&\
poopdb_del $1 &&\
return 0
echo "Error removing Gist $1"
return 1
}
get_gist_content () {
gist content `poopdb_get $1` $2
}
get_gist_files () {
gist files `poopdb_get $1`
}
git_clone_gist () {
gist clone `poopdb_get $1` $1
}
alias "gi"="gist"
alias "gil"="ls $POOPDB_DIR"
alias "gill"="poopdb_get_all"
alias "gic"="create_gist"
alias "girm"="remove_gist"
alias "gig"="get_gist_content"
alias "gif"="get_gist_files"
alias "giclo"="git_clone_gist"
#!/bin/bash
set -euo pipefail
mkdir -p $HOME/poopdb/db
cp ./gistR.sh $HOME/poopdb/
cp ./poopdb.engine $HOME/poopdb/
cat >> $HOME/.bashrc <<- EOM
# eneble donbattery's poopdb
if [ -d "$HOME/poopdb" ]; then
# create database dir if not exists
mkdir -p $HOME/poopdb/db
export POOPDB_DIR="$HOME/poopdb/db"
export POOPDB_ENGINE="$HOME/poopdb/poopdb.engine"
# source the gistR wrapper to get aliases for named gists
. "$HOME/poopdb/gistR.sh"
else
echo "Cannot find poopdb at $HOME/poopdb"
fi
EOM
# donbattery's poopdb
if [ ! -d "$POOPDB_DIR" ]; then
printf "Error : cannot find poopdb at %s\n" "$POOPDB_DIR"
return
fi
poopdb_get () {
[ -f "$POOPDB_DIR/$1" ] && printf "%s" "$(< "$POOPDB_DIR/$1")"
}
poopdb_set () {
printf "%s" "$2" > "$POOPDB_DIR/$1"
}
poopdb_del () {
[ -f "$POOPDB_DIR/$1" ] && rm "$POOPDB_DIR/$1"
}
poopdb_get_all() {
for RECORD in `ls $POOPDB_DIR`
do
echo "$RECORD:`poopdb_get $RECORD`"
done
}
#!/bin/bash
set -euo pipefail
# Import the poopdb
. $POOPDB_ENGINE
OLDIFS=$IFS
IFS=$'\n'
for GIST in `gist list`
do
echo $GIST
read -p 'Name :' GISTNAME
if [ -n "$GISTNAME" ]; then
poopdb_set $GISTNAME ${GIST%% *}
echo "Saved to poopdb"
echo
fi
done
IFS=$OLDIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment