aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xgist96
1 files changed, 73 insertions, 23 deletions
diff --git a/gist b/gist
index ad08016..da30e4b 100755
--- a/gist
+++ b/gist
@@ -14,7 +14,7 @@
14# gist (update | u) [star | s] 14# gist (update | u) [star | s]
15# 15#
16# * list your gists with format: [number] [url] [file_num] [comment_num] [short description] 16# * list your gists with format: [number] [url] [file_num] [comment_num] [short description]
17# gist 17# gist [star | s]
18# 18#
19# * clone gist repos which are not in local 19# * clone gist repos which are not in local
20# * pull master branch if a local repo is behind its remote 20# * pull master branch if a local repo is behind its remote
@@ -48,31 +48,52 @@
48# * show this help message 48# * show this help message
49# gist (help | h) 49# gist (help | h)
50 50
51# define your environmemnts here 51# TODO pipe syntax fix
52# TODO support auth prompt, remove personal info here
53# TODO error handling, unit test 52# TODO error handling, unit test
54# TODO parallel branch works with json parsing on python 53# TODO parallel branch works with json parsing on python
55# TODO parallel branch works with wget and other stuff 54# TODO parallel branch works with wget and other stuff
56# completion 55# TODO completion
57#------------------- 56
58github_api_token=$(cat $SETTING_DIR/tokens/github) 57# Validate settings.
59user=typebrook 58config=~/.config/gistrc
60folder=~/git/gist 59[ "$TRACE" ] && set -x
61#------------------- 60
61# TODO error handling while password is not true
62_auth() {
63 data="{\"scopes\":[\"gist\"], \"note\": \"gist-$(date -u +'%Y-%m-%dT%H:%M:%SZ')\"}"
64 read -p "Github username: " user
65 read -sp "Github password: " password
66 mkdir -p ~/.config && umask 0077 && echo user=$user > $config
67
68 curl https://api.github.com/authorizations \
69 --user "$user:$password" \
70 --data "$data" > /dev/null
71
72 read -p "2-factor code: " OTP
73 curl https://api.github.com/authorizations \
74 --user "$user:$password" -H "X-GitHub-OTP: $OTP" \
75 --data "$data" |\
76 sed '1 s/[^{]//g' | jq -r .token |\
77 sed 's/^/token=/' >> $config
78}
79
80while ! source $config; do
81 _auth
82done
62 83
63github_api=https://api.github.com 84github_api=https://api.github.com
64auth_header="Authorization: token $github_api_token" 85auth_header="Authorization: token $token"
86
87[[ -z "$folder" ]] && folder=~/git/gist
65mkdir -p $folder 88mkdir -p $folder
66index=$folder/index 89index=$folder/index
67starred=$folder/starred 90starred=$folder/starred
68 91
69# Validate settings.
70[ "$TRACE" ] && set -x
71
72# Show the list of gist, but not updated time 92# Show the list of gist, but not updated time
93# TODO a way to show files
73# TODO show git status outdated 94# TODO show git status outdated
74_show_list() { 95_show_list() {
75 if [[ ! -e $1 ]]; then 96 if [[ ! -e "$1" ]]; then
76 echo No local file found for last update 97 echo No local file found for last update
77 echo Please run command: 98 echo Please run command:
78 echo " gist update" 99 echo " gist update"
@@ -106,7 +127,7 @@ _update() {
106 curl -s -H "$auth_header" $github_api/$route |\ 127 curl -s -H "$auth_header" $github_api/$route |\
107 _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file && \ 128 _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file && \
108 _show_list $list_file 129 _show_list $list_file
109 (_sync_repos $1 > /dev/null 2>&1 &) 130 if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi
110} 131}
111 132
112# TODO check if a user create a very first gist 133# TODO check if a user create a very first gist
@@ -144,7 +165,7 @@ _sync_repos() {
144 165
145_gist_id() { 166_gist_id() {
146 GIST_ID=$(cat $index $starred | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') 167 GIST_ID=$(cat $index $starred | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##')
147 if [[ -z $GIST_ID ]]; then 168 if [[ -z "$GIST_ID" ]]; then
148 echo -e "Not a valid index: \e[31m$1\e[0m" 169 echo -e "Not a valid index: \e[31m$1\e[0m"
149 echo Use the index number in the first column instead: 170 echo Use the index number in the first column instead:
150 echo 171 echo
@@ -153,7 +174,7 @@ _gist_id() {
153 fi 174 fi
154} 175}
155 176
156# FIXME error handling 177# FIXME error handling, if repo not cloned yet
157_goto_gist() { 178_goto_gist() {
158 _gist_id $1 179 _gist_id $1
159 echo This gist is at $folder/$GIST_ID 180 echo This gist is at $folder/$GIST_ID
@@ -192,7 +213,7 @@ _show_detail() {
192} 213}
193 214
194_set_gist() { 215_set_gist() {
195 while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in 216 while [[ "$1" =~ ^- && "$1" != "--" ]]; do case $1 in
196 -d | --desc) 217 -d | --desc)
197 description="$2" 218 description="$2"
198 shift; shift;; 219 shift; shift;;
@@ -202,7 +223,7 @@ _set_gist() {
202 esac 223 esac
203 done 224 done
204 if [[ "$1" == '--' ]]; then shift; fi 225 if [[ "$1" == '--' ]]; then shift; fi
205 files=$@ 226 files="$@" && echo $files | xargs ls > /dev/null || exit 1
206} 227}
207 228
208_new_file() { 229_new_file() {
@@ -216,11 +237,11 @@ _new_file() {
216} 237}
217 238
218# create a new gist with files 239# create a new gist with files
219# FIXME error handling if gist is not created 240# FIXME error handling if gist is not created, file doesn't exist
220_create_gist() { 241_create_gist() {
221 _set_gist "$@" 242 _set_gist "$@"
222 [[ -z $files ]] && files=$(_new_file $filename) 243 [[ -z "$files" ]] && files=$(_new_file $filename)
223 [[ -z $description ]] && echo -n 'Type description: ' && read description 244 [[ -z "$description" ]] && echo -n 'Type description: ' && read description
224 245
225 for file in $files; do 246 for file in $files; do
226 FILE=$(basename $file) 247 FILE=$(basename $file)
@@ -254,6 +275,31 @@ _help_message() {
254 sed -E -n ' /^$/ q; 8,$ s/^#//p' $0 275 sed -E -n ' /^$/ q; 8,$ s/^#//p' $0
255} 276}
256 277
278_cases() {
279 if [[ $1 == 'token' ]]; then
280 [[ ${#2} -eq 40 ]] && echo token=$2 ||\
281 echo Invalid token format, it is not 40 chars '\n' > /dev/tty
282 elif [[ $1 == 'auto_sync' ]]; then
283 [[ $2 == 'false' ]] && echo $1=$2 ||\
284 echo $1=true
285 elif [[ $1 == 'folder' ]]; then
286 [[ -n "$2" ]] && echo $1=$2 ||\
287 echo $1=~/gist
288 elif [[ $1 == 'user' ]]; then
289 echo $1=$2
290 fi
291}
292
293# TODO consider the case that $2 is empty -> remove original setting
294_configure() {
295 [[ -z "$@" ]] && (vim $config) && exit 0
296 target=$(_cases "$@")
297
298 [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $config && echo $target >> $config
299 cat $config
300}
301
302
257case "$1" in 303case "$1" in
258 "") 304 "")
259 _show_list $index ;; 305 _show_list $index ;;
@@ -269,12 +315,16 @@ case "$1" in
269 sync | S) 315 sync | S)
270 _sync_repos ;; 316 _sync_repos ;;
271 detail | d) 317 detail | d)
272 _show_detail "$2" ;; 318 shift
319 _show_detail "$@" ;;
273 delete | D) 320 delete | D)
274 shift 321 shift
275 _delete_gist "$@" ;; 322 _delete_gist "$@" ;;
276 clean | C) 323 clean | C)
277 _clean_repos ;; 324 _clean_repos ;;
325 config | c)
326 shift
327 _configure "$@" ;;
278 help | h) 328 help | h)
279 _help_message ;; 329 _help_message ;;
280 *) 330 *)