summaryrefslogtreecommitdiffhomepage
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/gist32
1 files changed, 21 insertions, 11 deletions
diff --git a/scripts/gist b/scripts/gist
index 007032b..7d90336 100755
--- a/scripts/gist
+++ b/scripts/gist
@@ -50,16 +50,22 @@ mkdir -p $folder
50index=$folder/index 50index=$folder/index
51 51
52# Validate settings. 52# Validate settings.
53[ "$TRACE" ] && set -x && echo foo 53[ "$TRACE" ] && set -x
54
55# Show the list of gist, but not updated time
56_show_list() {
57 cat $index | cut -d' ' -f1-2,4-
58}
54 59
55# get the list of gists 60# get the list of gists
56_update() { 61_update() {
57 curl -s -H "$auth_header" $github_api/users/$user/gists |\ 62 curl -s -H "$auth_header" $github_api/users/$user/gists |\
58 jq '.[] | "\( .html_url ) \(.files | keys | length) \(.comments) \( .description )"' |\ 63 jq '.[] | "\(.html_url) \(.updated_at) \(.files | keys | length) \(.comments) \(.description)"' |\
59 tr -d '"' | tac | nl |\ 64 tr -d '"' | tac | nl |\
60 while read line_num link file_num comment_num description; do 65 while read line_num link file_num comment_num description; do
61 echo $line_num $link $file_num $comment_num $(echo $description | cut -c -70) 66 echo $line_num $link $file_num $comment_num $(echo $description | cut -c -80)
62 done | tee $index 67 done > $index && \
68 _show_list
63} 69}
64 70
65# clone repos which are not in the local 71# clone repos which are not in the local
@@ -70,23 +76,27 @@ _sync_repos() {
70 xargs -I{} git clone git@github.com:{}.git $folder/{} 76 xargs -I{} git clone git@github.com:{}.git $folder/{}
71} 77}
72 78
79_gist_id() {
80 cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##'
81}
82
73_goto_gist() { 83_goto_gist() {
74 gist_num=$(wc -l $index | cut -d' ' -f1) 84 gist_num=$(wc -l $index | cut -d' ' -f1)
75 if [[ ! "$1" =~ [0-9]+ ]] || (( $1 > $gist_num )); then 85 if [[ ! "$1" =~ [0-9]+ ]] || (( $1 > $gist_num )); then
76 echo Not a valid gist number: $1 86 echo Not a valid gist number: $1
77 echo Use the number in the first column instead: 87 echo Use the number in the first column instead:
78 echo 88 echo
79 cat $index 89 show_list
80 return 0 90 return 0
81 fi 91 fi
82 92
83 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') 93 GIST_ID=$(_gist_id $1)
84 echo This gist is at $folder/$GIST_ID 94 echo This gist is at $folder/$GIST_ID
85 cd $folder/$GIST_ID && tig --all 2> /dev/null 95 cd $folder/$GIST_ID && tig --all 2> /dev/null
86} 96}
87 97
88_delete_gist() { 98_delete_gist() {
89 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') 99 GIST_ID=$(_gist_id $1)
90 curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \ 100 curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \
91 _update 101 _update
92} 102}
@@ -99,9 +109,9 @@ _clean_repos() {
99} 109}
100 110
101_show_detail() { 111_show_detail() {
102 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') 112 GIST_ID=$(_gist_id $1)
103 curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ 113 curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\
104 jq '{site: .html_url, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' 114 jq '{site: .html_url, description: .description, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}'
105 115
106 curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\ 116 curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\
107 jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' 117 jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}'
@@ -129,7 +139,7 @@ _create_gist() {
129 139
130# update description of a gist 140# update description of a gist
131_edit_gist() { 141_edit_gist() {
132 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') 142 GIST_ID=$(_gist_id $1)
133 143
134 jq -n --arg DESC "$2" '{ description: ($DESC) }' |\ 144 jq -n --arg DESC "$2" '{ description: ($DESC) }' |\
135 curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \ 145 curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \
@@ -138,7 +148,7 @@ _edit_gist() {
138 148
139case "$1" in 149case "$1" in
140 "") 150 "")
141 cat $index 151 _show_list
142 ;; 152 ;;
143 create | c) 153 create | c)
144 shift; 154 shift;