diff options
author | typebrook <typebrook@gmail.com> | 2020-01-31 12:21:49 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-01-31 12:21:49 +0800 |
commit | 422febbb03bf130051cc88d31976e7a0cd895b48 (patch) | |
tree | 74bda497ac551cdc51a366fb5008c3dc365a28a5 /scripts/gist | |
parent | 5886506a6f31099c9a626e741140d012c87ad4ec (diff) |
update
Diffstat (limited to 'scripts/gist')
-rwxr-xr-x | scripts/gist | 45 |
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 | |||
65 | GITHUB_API=https://api.github.com | 65 | GITHUB_API=https://api.github.com |
66 | CONFIG=~/.config/gistrc | 66 | CONFIG=~/.config/gistrc |
67 | mkdir -p ~/.config && umask 0077 | 67 | mkdir -p ~/.config && umask 0077 |
68 | configuredClient="" | ||
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 | ||
130 | auth_header="Authorization: token $token" | 132 | auth_header="Authorization: token $token" |
131 | 133 | ||
132 | [[ -z "$folder" ]] && folder=~/gist | 134 | [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder |
133 | mkdir -p $folder | ||
134 | INDEX=$folder/index | 135 | INDEX=$folder/index |
135 | STARRED=$folder/starred | 136 | STARRED=$folder/starred |
136 | 137 | ||
138 | ## This function determines which http get tool the system has installed and returns an error if there isnt one | ||
139 | getConfiguredClient() | ||
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 | ||
156 | httpGet() | ||
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 |
180 | AccessJsonElement() { | 209 | AccessJsonElement() { |
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 | ||
407 | getConfiguredClient | ||
377 | case "$1" in | 408 | case "$1" in |
378 | "") | 409 | "") |
379 | _show_list $INDEX ;; | 410 | _show_list $INDEX ;; |