aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorHsieh Chin Fan <typebrook@gmail.com>2019-12-26 14:49:39 +0800
committerGitHub <noreply@github.com>2019-12-26 14:49:39 +0800
commitb1ba5845badf809bae285deaeba88f91c9ee9c4c (patch)
tree7b6abe1bb88e19369ce3f6b7eea73545912e7bfc
-rw-r--r--gist156
1 files changed, 156 insertions, 0 deletions
diff --git a/gist b/gist
new file mode 100644
index 0000000..aac4ea3
--- /dev/null
+++ b/gist
@@ -0,0 +1,156 @@
1#!/usr/bin/env bash
2#
3# Author: Hsieh Chin Fan
4# License: MIT
5# https://gist.github.com/typebrook/
6#
7#
8# This script host your gists as local Github repo
9# Use the following commands to manage your gists
10#
11# * update your gists list with Github API
12# gist [update | u]
13#
14# * list your gists with format: [number] [url] [file_num] [comment_num] [short description]
15# gist
16#
17# * Go to local gist repo
18# gist <number_of_gist_in_list>
19#
20# * create a new gist with a file and description
21# gist [create | c] <filename> "<description>"
22#
23# * show the detail of a gist
24# gist [detail | d] <number_of_gist_in_list>
25#
26# * edit a gist description
27# gist [edit | e] <number_of_gist_in_list>
28#
29# * delete a gist
30# gist [delete | D] <number_of_gist_in_list>
31#
32# * clean removed gists in local
33# gist [clean | C]
34
35# define your environmemnts here
36#-------------------
37github_api_token=$(cat $SETTING_DIR/tokens/github)
38user=typebrook
39folder=~/git/gist
40#-------------------
41
42github_api=https://api.github.com
43auth_header="Authorization: token $github_api_token"
44mkdir -p $folder
45index=$folder/index
46
47# get the list of gists
48_update() {
49 curl -s -H "$auth_header" $github_api/users/$user/gists |\
50 tee keep |\
51 jq '.[] | "\( .html_url ) \(.files | keys | length) \(.comments) \( .description )"' |\
52 tr -d '"' | tac | nl |\
53 while read line_num link file_num comment_num description; do
54 echo $line_num $link $file_num $comment_num $(echo $description | cut -c -70)
55 done | tee $index
56}
57
58# clone repos which are not in the local
59# TODO pull if repo is outdated
60_sync_repos() {
61 comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \
62 <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\
63 xargs -I{} git clone git@github.com:{}.git $folder/{}
64}
65
66_goto_gist() {
67 gist_num=$(wc -l $index | cut -d' ' -f1)
68 if [[ ! "$1" =~ [0-9]+ ]] || (( $1 > $gist_num )); then
69 echo Not a valid gist number: $1
70 echo Use the number in the first column instead:
71 echo
72 cat $index
73 return 0
74 fi
75
76 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##')
77 echo This gist is at $folder/$GIST_ID
78 cd $folder/$GIST_ID && tig --all 2> /dev/null
79}
80
81_delete_gist() {
82 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##')
83 curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \
84 _update
85}
86
87# remove repos which are not in user gists anymore
88_clean_repos() {
89 comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \
90 <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\
91 xargs -I{} rm -rf $folder/{}
92}
93
94_show_detail() {
95 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##')
96 curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\
97 jq '{site: .html_url, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}'
98
99 curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\
100 jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}'
101}
102
103# create a new gist with a file and description
104# TODO support folder of files
105_create_gist() {
106 FILE=$(basename $1)
107
108 jq --arg FILE "$FILE" --arg DESC "$2" '. as $content | {
109 public: true,
110 files: {
111 ($FILE): {content: $content}
112 },
113 description: ($DESC)
114 }' -R -s $1 |\
115 curl -s -H "$auth_header" --data @- $github_api/gists > /dev/null && \
116 _update
117}
118
119# update description of a gist
120_edit_gist() {
121 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##')
122
123 jq -n --arg DESC "$2" '{ description: ($DESC) }' |\
124 curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \
125 _update
126}
127
128case "$1" in
129 "")
130 cat $index
131 ;;
132 create | c)
133 _create_gist "$2" "$3"
134 ;;
135 edit | e)
136 _edit_gist "$2" "$3"
137 ;;
138 update | u)
139 _update
140 ;;
141 sync | s)
142 _sync_repos
143 ;;
144 detail | d)
145 _show_detail "$2"
146 ;;
147 delete | D)
148 _delete_gist "$2"
149 ;;
150 clean | C)
151 _clean_repos
152 ;;
153 *)
154 _goto_gist "$1"
155 ;;
156esac