diff options
author | typebrook <typebrook@gmail.com> | 2020-01-31 00:36:40 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-01-31 00:36:40 +0800 |
commit | fdf8146cbc9ca09433352ca9c203fdb4ef7c1bf1 (patch) | |
tree | 6ef26d273583c4f90af8af1ceac53af3d4f95b1d /scripts/gist | |
parent | cfe0db91fe4e1b3616c175262be5336482aa2b09 (diff) |
update
Diffstat (limited to 'scripts/gist')
-rwxr-xr-x | scripts/gist | 116 |
1 files changed, 61 insertions, 55 deletions
diff --git a/scripts/gist b/scripts/gist index 40d723a..feebfcc 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -56,50 +56,74 @@ | |||
56 | # TODO completion | 56 | # TODO completion |
57 | # TODO grep mode for description, file content | 57 | # TODO grep mode for description, file content |
58 | 58 | ||
59 | # Validate settings. | 59 | # Validate configuration |
60 | config=~/.config/gistrc | ||
61 | set -eo pipefail | 60 | set -eo pipefail |
62 | [ "$TRACE" ] && set -x | 61 | [ "$TRACE" ] && set -x |
63 | 62 | ||
64 | ## parse JSON from STDIN with string of commands | 63 | github_api=https://api.github.com |
65 | AccessJsonElement() { | 64 | config=~/.config/gistrc |
66 | PYTHONIOENCODING=utf-8 python -c "from __future__ import print_function; import sys, json; raw = json.load(sys.stdin); $1" | 65 | mkdir -p ~/.config && umask 0077 |
67 | return "$?" | 66 | |
67 | _config_cases() { | ||
68 | if [[ $1 == 'token' ]]; then | ||
69 | [[ ${#2} -eq 40 ]] && echo $1=$2 \ | ||
70 | || echo -e Invalid token format, it is not 40 chars '\n' > /dev/tty | ||
71 | elif [[ $1 == 'auto_sync' ]]; then | ||
72 | [[ $2 == 'false' ]] && echo $1=$2 \ | ||
73 | || echo $1=true | ||
74 | elif [[ $1 == 'folder' ]]; then | ||
75 | [[ -n "$2" ]] && echo $1=$2 \ | ||
76 | || echo $1=~/gist | ||
77 | elif [[ $1 == 'user' ]]; then | ||
78 | echo $1=$2 | ||
79 | fi | ||
80 | } | ||
81 | |||
82 | _configure() { | ||
83 | [[ -z "$@" ]] && (vim $config) && exit 0 | ||
84 | target=$(_config_cases "$@") | ||
85 | |||
86 | touch $config | ||
87 | [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $config && echo $target >> $config | ||
88 | cat $config | ||
68 | } | 89 | } |
69 | 90 | ||
70 | _auth() { | 91 | _ask_username() { |
71 | echo 'Hi fellow! To access your gists, I need your Github username and a personal token with scope which allows "gist"!' | 92 | while [[ ! $user =~ ^[[:alnum:]]+$ ]]; do |
72 | read -p "Github username: " user < /dev/tty | 93 | [[ ! -z $user ]] && echo "Not a valid username" |
73 | if [[ $user =~ ^[[:alnum:]]+$ ]]; then | 94 | read -p "Github username: " user < /dev/tty |
74 | mkdir -p ~/.config | 95 | done |
75 | umask 0077 | 96 | _configure user $user |
76 | echo user=$user > $config | 97 | } |
77 | else | ||
78 | echo "Not a valid username" | ||
79 | return 0 | ||
80 | fi | ||
81 | 98 | ||
99 | _ask_token() { | ||
82 | echo -n "Create a new token from web browser? [Y/n] " | 100 | echo -n "Create a new token from web browser? [Y/n] " |
83 | read answer < /dev/tty | 101 | read answer < /dev/tty |
84 | if [[ ! $answer =~ ^(N|n|No|NO|no)$ ]]; then | 102 | if [[ ! $answer =~ ^(N|n|No|NO|no)$ ]]; then |
85 | python -mwebbrowser https://github.com/settings/tokens/new\?scopes\=gist; | 103 | python -mwebbrowser https://github.com/settings/tokens/new\?scopes\=gist; |
86 | fi | 104 | fi |
87 | 105 | ||
88 | read -p "Paste your token here: " new_token < /dev/tty | 106 | while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do |
89 | [[ $new_token =~ ^[[:alnum:]]{40}$ ]] \ | 107 | [[ ! -z $token ]] && echo "Not a valid token" |
90 | && echo token=$new_token >> $config \ | 108 | read -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty |
91 | || echo "Not a valid token" | 109 | done |
110 | _configure token $token | ||
92 | } | 111 | } |
93 | 112 | ||
94 | case "$1" in | 113 | _apply_config() { |
95 | config | c) ;; | 114 | if [[ ! -e $config ]] || [[ -z $user ]]; then |
96 | *) | 115 | echo 'Hi fellow! To access your gists, I need your Github username, also a personal token with scope which allows "gist"!' |
97 | while ! source $config 2> /dev/null || [[ -z "$token" ]] || [[ -z "$user" ]]; do | 116 | _ask_username |
98 | _auth | 117 | _ask_token |
99 | done;; | 118 | elif [[ -z $token ]] && [[ $2 =~ ^(n|new|e|edit|D|delete)$ ]]; then |
100 | esac | 119 | _ask_token |
120 | fi | ||
121 | |||
122 | source $config | ||
123 | } | ||
124 | |||
125 | _apply_config | ||
101 | 126 | ||
102 | github_api=https://api.github.com | ||
103 | auth_header="Authorization: token $token" | 127 | auth_header="Authorization: token $token" |
104 | 128 | ||
105 | [[ -z "$folder" ]] && folder=~/gist | 129 | [[ -z "$folder" ]] && folder=~/gist |
@@ -112,8 +136,7 @@ starred=$folder/starred | |||
112 | # TODO show git status outdated | 136 | # TODO show git status outdated |
113 | _show_list() { | 137 | _show_list() { |
114 | if [[ ! -e "$1" ]]; then | 138 | if [[ ! -e "$1" ]]; then |
115 | echo No local file found for last update | 139 | echo 'No local file found for last update, please run command:' |
116 | echo Please run command: | ||
117 | echo " gist update" | 140 | echo " gist update" |
118 | exit 0 | 141 | exit 0 |
119 | fi | 142 | fi |
@@ -143,7 +166,7 @@ _update() { | |||
143 | local mark="" | 166 | local mark="" |
144 | [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s" | 167 | [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s" |
145 | 168 | ||
146 | curl -s -H "$auth_header" $github_api/$route \ | 169 | curl -s $github_api/$route \ |
147 | | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file \ | 170 | | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file \ |
148 | && _show_list $list_file \ | 171 | && _show_list $list_file \ |
149 | || echo Fail to update gists | 172 | || echo Fail to update gists |
@@ -151,6 +174,12 @@ _update() { | |||
151 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 174 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi |
152 | } | 175 | } |
153 | 176 | ||
177 | ## parse JSON from STDIN with string of commands | ||
178 | AccessJsonElement() { | ||
179 | PYTHONIOENCODING=utf-8 python -c "from __future__ import print_function; import sys, json; raw = json.load(sys.stdin); $1" | ||
180 | return "$?" | ||
181 | } | ||
182 | |||
154 | # equal to: jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' | 183 | # equal to: jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |
155 | _handle_gists() { | 184 | _handle_gists() { |
156 | echo ' | 185 | echo ' |
@@ -339,29 +368,6 @@ _help_message() { | |||
339 | sed -E -n ' /^$/ q; 8,$ s/^#//p' $0 | 368 | sed -E -n ' /^$/ q; 8,$ s/^#//p' $0 |
340 | } | 369 | } |
341 | 370 | ||
342 | _cases() { | ||
343 | if [[ $1 == 'token' ]]; then | ||
344 | [[ ${#2} -eq 40 ]] && echo $1=$2 \ | ||
345 | || echo -e Invalid token format, it is not 40 chars '\n' > /dev/tty | ||
346 | elif [[ $1 == 'auto_sync' ]]; then | ||
347 | [[ $2 == 'false' ]] && echo $1=$2 \ | ||
348 | || echo $1=true | ||
349 | elif [[ $1 == 'folder' ]]; then | ||
350 | [[ -n "$2" ]] && echo $1=$2 \ | ||
351 | || echo $1=~/gist | ||
352 | elif [[ $1 == 'user' ]]; then | ||
353 | echo $1=$2 | ||
354 | fi | ||
355 | } | ||
356 | |||
357 | _configure() { | ||
358 | [[ -z "$@" ]] && (vim $config) && exit 0 | ||
359 | target=$(_cases "$@") | ||
360 | |||
361 | [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $config && echo $target >> $config | ||
362 | cat $config | ||
363 | } | ||
364 | |||
365 | case "$1" in | 371 | case "$1" in |
366 | "") | 372 | "") |
367 | _show_list $index ;; | 373 | _show_list $index ;; |