summaryrefslogtreecommitdiffhomepage
path: root/scripts/gist
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-01-31 12:21:49 +0800
committertypebrook <typebrook@gmail.com>2020-01-31 12:21:49 +0800
commit422febbb03bf130051cc88d31976e7a0cd895b48 (patch)
tree74bda497ac551cdc51a366fb5008c3dc365a28a5 /scripts/gist
parent5886506a6f31099c9a626e741140d012c87ad4ec (diff)
update
Diffstat (limited to 'scripts/gist')
-rwxr-xr-xscripts/gist45
1 files changed, 38 insertions, 7 deletions
diff --git a/scripts/gist b/scripts/gist
index 010f6c9..b6ab681 100755
--- a/scripts/gist
+++ b/scripts/gist
@@ -65,6 +65,7 @@ set -eo pipefail
65GITHUB_API=https://api.github.com 65GITHUB_API=https://api.github.com
66CONFIG=~/.config/gistrc 66CONFIG=~/.config/gistrc
67mkdir -p ~/.config && umask 0077 67mkdir -p ~/.config && umask 0077
68configuredClient=""
68 69
69_config_cases() { 70_config_cases() {
70 if [[ $1 == 'token' ]]; then 71 if [[ $1 == 'token' ]]; then
@@ -115,7 +116,8 @@ _ask_token() {
115_apply_config() { 116_apply_config() {
116 source $CONFIG 117 source $CONFIG
117 if [[ ! -e $CONFIG ]] || [[ -z $user ]]; then 118 if [[ ! -e $CONFIG ]] || [[ -z $user ]]; then
118 echo 'Hi fellow! To access your gists, I need your Github username, also a personal token with scope which allows "gist"!' 119 echo -n 'Hi fellow! To access your gists, I need your Github username, '
120 echo -n "also a personal token with scope which allows "gist"!'"
119 _ask_username 121 _ask_username
120 _ask_token 122 _ask_token
121 elif [[ -z $token ]] && [[ $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then 123 elif [[ -z $token ]] && [[ $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then
@@ -129,11 +131,38 @@ _apply_config "$@"
129 131
130auth_header="Authorization: token $token" 132auth_header="Authorization: token $token"
131 133
132[[ -z "$folder" ]] && folder=~/gist 134[[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder
133mkdir -p $folder
134INDEX=$folder/index 135INDEX=$folder/index
135STARRED=$folder/starred 136STARRED=$folder/starred
136 137
138## This function determines which http get tool the system has installed and returns an error if there isnt one
139getConfiguredClient()
140{
141 if command -v curl &>/dev/null; then
142 configuredClient="curl"
143 elif command -v wget &>/dev/null; then
144 configuredClient="wget"
145 elif command -v http &>/dev/null; then
146 configuredClient="httpie"
147 elif command -v fetch &>/dev/null; then
148 configuredClient="fetch"
149 else
150 echo "Error: This tool requires either curl, wget, httpie or fetch to be installed." >&2
151 return 1
152 fi
153}
154
155## Allows to call the users configured client without if statements everywhere
156httpGet()
157{
158 case "$configuredClient" in
159 curl) curl -A curl -s "$@" ;;
160 wget) wget -qO- "$@" ;;
161 httpie) http -b GET "$@" ;;
162 fetch) fetch -q "$@" ;;
163 esac
164}
165
137# Show the list of gist, but not updated time 166# Show the list of gist, but not updated time
138# TODO a way to show files 167# TODO a way to show files
139# TODO show git status outdated 168# TODO show git status outdated
@@ -168,7 +197,7 @@ _update() {
168 local mark="" 197 local mark=""
169 [[ "$1" =~ ^(star|s)$ ]] && list_file=$STARRED && route="gists/starred" && mark="s" 198 [[ "$1" =~ ^(star|s)$ ]] && list_file=$STARRED && route="gists/starred" && mark="s"
170 199
171 curl -s $GITHUB_API/$route \ 200 httpGet $GITHUB_API/$route \
172 | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file \ 201 | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file \
173 && _show_list $list_file \ 202 && _show_list $list_file \
174 || echo Fail to update gists 203 || echo Fail to update gists
@@ -178,7 +207,8 @@ _update() {
178 207
179## parse JSON from STDIN with string of commands 208## parse JSON from STDIN with string of commands
180AccessJsonElement() { 209AccessJsonElement() {
181 PYTHONIOENCODING=utf-8 python -c "from __future__ import print_function; import sys, json; raw = json.load(sys.stdin); $1" 210 PYTHONIOENCODING=utf-8 \
211 python -c "from __future__ import print_function; import sys, json; raw = json.load(sys.stdin); $1"
182 return "$?" 212 return "$?"
183} 213}
184 214
@@ -304,10 +334,10 @@ for comment in raw:
304# TODO format with simple text 334# TODO format with simple text
305_show_detail() { 335_show_detail() {
306 _gist_id $1 336 _gist_id $1
307 curl -s $GITHUB_API/gists/$GIST_ID \ 337 httpGet $GITHUB_API/gists/$GIST_ID \
308 | AccessJsonElement "$(_handle_gist)" 338 | AccessJsonElement "$(_handle_gist)"
309 339
310 curl -s $GITHUB_API/gists/$GIST_ID/comments \ 340 httpGet $GITHUB_API/gists/$GIST_ID/comments \
311 | AccessJsonElement "$(_handle_comment)" 341 | AccessJsonElement "$(_handle_comment)"
312} 342}
313 343
@@ -374,6 +404,7 @@ _help_message() {
374 sed -E -n ' /^$/ q; 8,$ s/^#//p' $0 404 sed -E -n ' /^$/ q; 8,$ s/^#//p' $0
375} 405}
376 406
407getConfiguredClient
377case "$1" in 408case "$1" in
378 "") 409 "")
379 _show_list $INDEX ;; 410 _show_list $INDEX ;;