diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/gist | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/scripts/gist b/scripts/gist index da30e4b..e9b95c0 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -73,8 +73,8 @@ _auth() { | |||
73 | curl https://api.github.com/authorizations \ | 73 | curl https://api.github.com/authorizations \ |
74 | --user "$user:$password" -H "X-GitHub-OTP: $OTP" \ | 74 | --user "$user:$password" -H "X-GitHub-OTP: $OTP" \ |
75 | --data "$data" |\ | 75 | --data "$data" |\ |
76 | sed '1 s/[^{]//g' | jq -r .token |\ | 76 | sed '1 s/[^{]//g' | jq -r .token \ |
77 | sed 's/^/token=/' >> $config | 77 | | sed 's/^/token=/' >> $config |
78 | } | 78 | } |
79 | 79 | ||
80 | while ! source $config; do | 80 | while ! source $config; do |
@@ -99,8 +99,8 @@ _show_list() { | |||
99 | echo " gist update" | 99 | echo " gist update" |
100 | exit 0 | 100 | exit 0 |
101 | fi | 101 | fi |
102 | cat $1 |\ | 102 | cat $1 \ |
103 | while read line_num link file_url_array file_num extra description; do | 103 | | while read line_num link file_url_array file_num extra description; do |
104 | repo=$folder/$(echo $link | sed 's#.*/##') | 104 | repo=$folder/$(echo $link | sed 's#.*/##') |
105 | 105 | ||
106 | # if repo is not yet cloned, show green message "Sync Now" | 106 | # if repo is not yet cloned, show green message "Sync Now" |
@@ -124,17 +124,17 @@ _update() { | |||
124 | mark="" | 124 | mark="" |
125 | [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s" | 125 | [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s" |
126 | 126 | ||
127 | curl -s -H "$auth_header" $github_api/$route |\ | 127 | curl -s -H "$auth_header" $github_api/$route \ |
128 | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file && \ | 128 | | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file && \ |
129 | _show_list $list_file | 129 | _show_list $list_file |
130 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 130 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi |
131 | } | 131 | } |
132 | 132 | ||
133 | # TODO check if a user create a very first gist | 133 | # TODO check if a user create a very first gist |
134 | _parse_response() { | 134 | _parse_response() { |
135 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ | 135 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' \ |
136 | tac |\ | 136 | | tac \ |
137 | while read link file_url_array file_num comment_num description; do | 137 | | 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 '-') | 138 | 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 '"' | 139 | echo $link $blob_code $file_num $comment_num $description | tr -d '"' |
140 | done | 140 | done |
@@ -146,12 +146,12 @@ _sync_repos() { | |||
146 | 146 | ||
147 | # clone repos which are not in the local | 147 | # clone repos which are not in the local |
148 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 148 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ |
149 | <(cat $list_file | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ | 149 | <(cat $list_file | cut -d' ' -f2 | sed 's#.*/##' | sort) \ |
150 | xargs -I{} git clone git@github.com:{}.git $folder/{} | 150 | | xargs -I{} git clone git@github.com:{}.git $folder/{} |
151 | 151 | ||
152 | # pull if remote repo has different blob objects | 152 | # pull if remote repo has different blob objects |
153 | cat $index | cut -d' ' -f2,3 |\ | 153 | cat $index | cut -d' ' -f2,3 \ |
154 | while read url blob_code_remote; do | 154 | | while read url blob_code_remote; do |
155 | repo=$folder/$(echo $url | sed 's#.*/##') | 155 | 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 '-') | 156 | blob_code_local=$(cd $repo && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-') |
157 | 157 | ||
@@ -196,8 +196,8 @@ _delete_gist() { | |||
196 | # remove repos which are not in user gists anymore | 196 | # remove repos which are not in user gists anymore |
197 | _clean_repos() { | 197 | _clean_repos() { |
198 | comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 198 | comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ |
199 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ | 199 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) \ |
200 | while read dir; do | 200 | | while read dir; do |
201 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp | 201 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp |
202 | done | 202 | done |
203 | } | 203 | } |
@@ -205,11 +205,11 @@ _clean_repos() { | |||
205 | # TODO format with simple text | 205 | # TODO format with simple text |
206 | _show_detail() { | 206 | _show_detail() { |
207 | _gist_id $1 | 207 | _gist_id $1 |
208 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ | 208 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID \ |
209 | jq '{site: .html_url, description: .description, public: .public, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' | 209 | | jq '{site: .html_url, description: .description, public: .public, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' |
210 | 210 | ||
211 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\ | 211 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments \ |
212 | jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' | 212 | | jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' |
213 | } | 213 | } |
214 | 214 | ||
215 | _set_gist() { | 215 | _set_gist() { |
@@ -246,16 +246,16 @@ _create_gist() { | |||
246 | for file in $files; do | 246 | for file in $files; do |
247 | FILE=$(basename $file) | 247 | FILE=$(basename $file) |
248 | jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file | 248 | jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file |
249 | done |\ | 249 | done \ |
250 | jq --slurp --arg DESC "$description" '{ | 250 | | jq --slurp --arg DESC "$description" '{ |
251 | public: true, | 251 | public: true, |
252 | files: add, | 252 | files: add, |
253 | description: ($DESC) | 253 | description: ($DESC) |
254 | }' |\ | 254 | }' \ |
255 | curl -H "$auth_header" --data @- $github_api/gists |\ | 255 | | curl -H "$auth_header" --data @- $github_api/gists \ |
256 | sed '1 s/^/[/; $ s/$/]/' |\ | 256 | | sed '1 s/^/[/; $ s/$/]/' \ |
257 | _parse_response |\ | 257 | | _parse_response \ |
258 | sed -E "s/^/$(( $(wc -l $index | cut -d' ' -f1) + 1 )) /" >> $index && \ | 258 | | sed -E "s/^/$(( $(wc -l $index | cut -d' ' -f1) + 1 )) /" >> $index && \ |
259 | echo -e '\nGist created' | 259 | echo -e '\nGist created' |
260 | _show_list $index | tail -1 | 260 | _show_list $index | tail -1 |
261 | } | 261 | } |
@@ -266,8 +266,8 @@ _edit_gist() { | |||
266 | 266 | ||
267 | echo -n 'Type new description: ' | 267 | echo -n 'Type new description: ' |
268 | read DESC | 268 | read DESC |
269 | jq -n --arg DESC "$DESC" '{ description: ($DESC) }' |\ | 269 | jq -n --arg DESC "$DESC" '{ description: ($DESC) }' \ |
270 | curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \ | 270 | | curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null && \ |
271 | _update | 271 | _update |
272 | } | 272 | } |
273 | 273 | ||
@@ -278,7 +278,7 @@ _help_message() { | |||
278 | _cases() { | 278 | _cases() { |
279 | if [[ $1 == 'token' ]]; then | 279 | if [[ $1 == 'token' ]]; then |
280 | [[ ${#2} -eq 40 ]] && echo token=$2 ||\ | 280 | [[ ${#2} -eq 40 ]] && echo token=$2 ||\ |
281 | echo Invalid token format, it is not 40 chars '\n' > /dev/tty | 281 | echo -e Invalid token format, it is not 40 chars '\n' > /dev/tty |
282 | elif [[ $1 == 'auto_sync' ]]; then | 282 | elif [[ $1 == 'auto_sync' ]]; then |
283 | [[ $2 == 'false' ]] && echo $1=$2 ||\ | 283 | [[ $2 == 'false' ]] && echo $1=$2 ||\ |
284 | echo $1=true | 284 | echo $1=true |
@@ -290,7 +290,6 @@ _cases() { | |||
290 | fi | 290 | fi |
291 | } | 291 | } |
292 | 292 | ||
293 | # TODO consider the case that $2 is empty -> remove original setting | ||
294 | _configure() { | 293 | _configure() { |
295 | [[ -z "$@" ]] && (vim $config) && exit 0 | 294 | [[ -z "$@" ]] && (vim $config) && exit 0 |
296 | target=$(_cases "$@") | 295 | target=$(_cases "$@") |