summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-01-22 16:48:16 +0800
committertypebrook <typebrook@gmail.com>2020-01-22 16:48:16 +0800
commite90f4db75ba47b87d84f510a50e37dcbae23540d (patch)
tree3357dc55cc5f7cf9d82671b210c310991a0a98ca
parentadc7f042d676e4fe13f11d8a621c9f181cf8dffb (diff)
update
-rwxr-xr-xscripts/gist32
1 files changed, 16 insertions, 16 deletions
diff --git a/scripts/gist b/scripts/gist
index 5786dac..1fbceb2 100755
--- a/scripts/gist
+++ b/scripts/gist
@@ -48,7 +48,6 @@
48# * show this help message 48# * show this help message
49# gist (help | h) 49# gist (help | h)
50 50
51# TODO pipe syntax fix
52# TODO error handling, unit test 51# TODO error handling, unit test
53# TODO parallel branch works with json parsing on python 52# TODO parallel branch works with json parsing on python
54# TODO parallel branch works with wget and other stuff 53# TODO parallel branch works with wget and other stuff
@@ -60,7 +59,7 @@ config=~/.config/gistrc
60 59
61# TODO error handling while password is not true 60# TODO error handling while password is not true
62_auth() { 61_auth() {
63 data="{\"scopes\":[\"gist\"], \"note\": \"gist-$(date -u +'%Y-%m-%dT%H:%M:%SZ')\"}" 62 local data="{\"scopes\":[\"gist\"], \"note\": \"gist-$(date -u +'%Y-%m-%dT%H:%M:%SZ')\"}"
64 read -p "Github username: " user 63 read -p "Github username: " user
65 read -sp "Github password: " password 64 read -sp "Github password: " password
66 mkdir -p ~/.config && umask 0077 && echo user=$user > $config 65 mkdir -p ~/.config && umask 0077 && echo user=$user > $config
@@ -101,7 +100,7 @@ _show_list() {
101 fi 100 fi
102 cat $1 \ 101 cat $1 \
103 | while read line_num link file_url_array file_num extra description; do 102 | while read line_num link file_url_array file_num extra description; do
104 repo=$folder/$(echo $link | sed 's#.*/##') 103 local repo=$folder/$(echo $link | sed 's#.*/##')
105 104
106 # if repo is not yet cloned, show green message "Sync Now" 105 # if repo is not yet cloned, show green message "Sync Now"
107 cd $repo 2>/dev/null || extra="\e[32m[Sync Now]\e[0m" 106 cd $repo 2>/dev/null || extra="\e[32m[Sync Now]\e[0m"
@@ -119,9 +118,9 @@ _show_list() {
119_update() { 118_update() {
120 echo "fetching from api.github.com..." 119 echo "fetching from api.github.com..."
121 echo 120 echo
122 list_file=$index 121 local list_file=$index
123 route="users/$user/gists" 122 local route="users/$user/gists"
124 mark="" 123 local mark=""
125 [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s" 124 [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s"
126 125
127 curl -s -H "$auth_header" $github_api/$route \ 126 curl -s -H "$auth_header" $github_api/$route \
@@ -135,13 +134,13 @@ _parse_response() {
135 jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' \ 134 jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' \
136 | tac \ 135 | tac \
137 | while read link file_url_array file_num comment_num description; do 136 | while read link file_url_array file_num comment_num description; do
138 blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') 137 local blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-')
139 echo $link $blob_code $file_num $comment_num $description | tr -d '"' 138 echo $link $blob_code $file_num $comment_num $description | tr -d '"'
140 done 139 done
141} 140}
142 141
143_sync_repos() { 142_sync_repos() {
144 list_file=$index 143 local list_file=$index
145 [[ "$1" == "--star" ]] && list_file=$starred && route="gists/starred" 144 [[ "$1" == "--star" ]] && list_file=$starred && route="gists/starred"
146 145
147 # clone repos which are not in the local 146 # clone repos which are not in the local
@@ -152,9 +151,8 @@ _sync_repos() {
152 # pull if remote repo has different blob objects 151 # pull if remote repo has different blob objects
153 cat $index | cut -d' ' -f2,3 \ 152 cat $index | cut -d' ' -f2,3 \
154 | while read url blob_code_remote; do 153 | while read url blob_code_remote; do
155 repo=$folder/$(echo $url | sed 's#.*/##') 154 local repo=$folder/$(echo $url | sed 's#.*/##')
156 blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-') 155 local blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-')
157
158 cd $repo && \ 156 cd $repo && \
159 [[ $blob_code_local != $blob_code_remote ]] && \ 157 [[ $blob_code_local != $blob_code_remote ]] && \
160 [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] && \ 158 [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] && \
@@ -223,16 +221,17 @@ _set_gist() {
223 esac 221 esac
224 done 222 done
225 if [[ "$1" == '--' ]]; then shift; fi 223 if [[ "$1" == '--' ]]; then shift; fi
224 # TODO could be simplified?
226 files="$@" && echo $files | xargs ls > /dev/null || exit 1 225 files="$@" && echo $files | xargs ls > /dev/null || exit 1
227} 226}
228 227
229_new_file() { 228_new_file() {
230 [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty 229 [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty
231 tmp_file=$(mktemp) 230 local tmp_file=$(mktemp)
232 cat > $tmp_file 231 cat > $tmp_file
233 echo -e '\n' > /dev/tty 232 echo -e '\n' > /dev/tty
233 # FIXME when file content is from STDIN, read fails
234 [[ -z "$1" ]] && echo -n 'Type file name: ' > /dev/tty && read filename 234 [[ -z "$1" ]] && echo -n 'Type file name: ' > /dev/tty && read filename
235 # FIXME filename is invalid
236 mv $tmp_file /tmp/$filename 235 mv $tmp_file /tmp/$filename
237 echo /tmp/$filename 236 echo /tmp/$filename
238} 237}
@@ -242,10 +241,11 @@ _new_file() {
242_create_gist() { 241_create_gist() {
243 _set_gist "$@" 242 _set_gist "$@"
244 [[ -z "$files" ]] && files=$(_new_file $filename) 243 [[ -z "$files" ]] && files=$(_new_file $filename)
244 # FIXME when file content is from STDIN, read fails
245 [[ -z "$description" ]] && echo -n 'Type description: ' && read description 245 [[ -z "$description" ]] && echo -n 'Type description: ' && read description
246 246
247 for file in $files; do 247 for file in $files; do
248 FILE=$(basename $file) 248 local FILE=$(basename $file)
249 jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file 249 jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file
250 done \ 250 done \
251 | jq --slurp --arg DESC "$description" '{ 251 | jq --slurp --arg DESC "$description" '{
@@ -278,7 +278,7 @@ _help_message() {
278 278
279_cases() { 279_cases() {
280 if [[ $1 == 'token' ]]; then 280 if [[ $1 == 'token' ]]; then
281 [[ ${#2} -eq 40 ]] && echo token=$2 ||\ 281 [[ ${#2} -eq 40 ]] && echo $1=$2 ||\
282 echo -e Invalid token format, it is not 40 chars '\n' > /dev/tty 282 echo -e Invalid token format, it is not 40 chars '\n' > /dev/tty
283 elif [[ $1 == 'auto_sync' ]]; then 283 elif [[ $1 == 'auto_sync' ]]; then
284 [[ $2 == 'false' ]] && echo $1=$2 ||\ 284 [[ $2 == 'false' ]] && echo $1=$2 ||\
@@ -293,7 +293,7 @@ _cases() {
293 293
294_configure() { 294_configure() {
295 [[ -z "$@" ]] && (vim $config) && exit 0 295 [[ -z "$@" ]] && (vim $config) && exit 0
296 target=$(_cases "$@") 296 local target=$(_cases "$@")
297 297
298 [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $config && echo $target >> $config 298 [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $config && echo $target >> $config
299 cat $config 299 cat $config