#!/bin/bash github_api_token=$(cat $SETTING_DIR/tokens/github) user=typebrook folder=~/git/gist mkdir -p $folder index=$folder/index function _update() { # get the list of gists curl -s -H "Authorization: token $github_api_token" https://api.github.com/users/$user/gists |\ jq '.[] | "\( .html_url ) \(.files | keys | length) \( .description )"' |\ tr -d '"' | tac | nl |\ while read line_num link file_num description; do echo $line_num $link $file_num $(echo $description | cut -c -70) done | tee $index # clone repos which are not in the local machine comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ xargs -I{} git clone git@github.com:{}.git $folder/{} } function _go_to_gist() { GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') echo this gist is at $folder/$GIST_ID cd $folder/$GIST_ID && tig --all 2> /dev/null } function _show_detail() { GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') curl -s -H "Authorization: token $github_api_token" https://api.github.com/gists/$GIST_ID } function _create() { curl -v -H "Authorization: token $github_api_token" \ --data '{"public":true,"files":{"foo.gpx":{"content":"$2"}}}' \ https://api.github.com/gists } if [[ $# -eq 0 ]]; then cat $index exit 0 fi case "$1" in update | u) _update ;; detail | d) _show_detail $2 ;; create | c) _create $2 ;; *) _go_to_gist $1 ;; esac