From b1ba5845badf809bae285deaeba88f91c9ee9c4c Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Thu, 26 Dec 2019 14:49:39 +0800 Subject: --- gist | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 gist diff --git a/gist b/gist new file mode 100644 index 0000000..aac4ea3 --- /dev/null +++ b/gist @@ -0,0 +1,156 @@ +#!/usr/bin/env bash +# +# Author: Hsieh Chin Fan +# License: MIT +# https://gist.github.com/typebrook/ +# +# +# This script host your gists as local Github repo +# Use the following commands to manage your gists +# +# * update your gists list with Github API +# gist [update | u] +# +# * list your gists with format: [number] [url] [file_num] [comment_num] [short description] +# gist +# +# * Go to local gist repo +# gist +# +# * create a new gist with a file and description +# gist [create | c] "" +# +# * show the detail of a gist +# gist [detail | d] +# +# * edit a gist description +# gist [edit | e] +# +# * delete a gist +# gist [delete | D] +# +# * clean removed gists in local +# gist [clean | C] + +# define your environmemnts here +#------------------- +github_api_token=$(cat $SETTING_DIR/tokens/github) +user=typebrook +folder=~/git/gist +#------------------- + +github_api=https://api.github.com +auth_header="Authorization: token $github_api_token" +mkdir -p $folder +index=$folder/index + +# get the list of gists +_update() { + curl -s -H "$auth_header" $github_api/users/$user/gists |\ + tee keep |\ + jq '.[] | "\( .html_url ) \(.files | keys | length) \(.comments) \( .description )"' |\ + tr -d '"' | tac | nl |\ + while read line_num link file_num comment_num description; do + echo $line_num $link $file_num $comment_num $(echo $description | cut -c -70) + done | tee $index +} + +# clone repos which are not in the local +# TODO pull if repo is outdated +_sync_repos() { + 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/{} +} + +_goto_gist() { + gist_num=$(wc -l $index | cut -d' ' -f1) + if [[ ! "$1" =~ [0-9]+ ]] || (( $1 > $gist_num )); then + echo Not a valid gist number: $1 + echo Use the number in the first column instead: + echo + cat $index + return 0 + fi + + 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 +} + +_delete_gist() { + GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') + curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \ + _update +} + +# remove repos which are not in user gists anymore +_clean_repos() { + comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ + <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ + xargs -I{} rm -rf $folder/{} +} + +_show_detail() { + GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') + curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ + jq '{site: .html_url, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' + + curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\ + jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' +} + +# create a new gist with a file and description +# TODO support folder of files +_create_gist() { + FILE=$(basename $1) + + jq --arg FILE "$FILE" --arg DESC "$2" '. as $content | { + public: true, + files: { + ($FILE): {content: $content} + }, + description: ($DESC) + }' -R -s $1 |\ + curl -s -H "$auth_header" --data @- $github_api/gists > /dev/null && \ + _update +} + +# update description of a gist +_edit_gist() { + GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') + + jq -n --arg DESC "$2" '{ description: ($DESC) }' |\ + curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \ + _update +} + +case "$1" in + "") + cat $index + ;; + create | c) + _create_gist "$2" "$3" + ;; + edit | e) + _edit_gist "$2" "$3" + ;; + update | u) + _update + ;; + sync | s) + _sync_repos + ;; + detail | d) + _show_detail "$2" + ;; + delete | D) + _delete_gist "$2" + ;; + clean | C) + _clean_repos + ;; + *) + _goto_gist "$1" + ;; +esac -- cgit v1.2.3-70-g09d2