aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/gist36
1 files changed, 24 insertions, 12 deletions
diff --git a/scripts/gist b/scripts/gist
index 558b659..5bfb9d6 100755
--- a/scripts/gist
+++ b/scripts/gist
@@ -4,12 +4,14 @@ github_api_token=$(cat $SETTING_DIR/tokens/github)
4user=typebrook 4user=typebrook
5folder=~/git/gist 5folder=~/git/gist
6 6
7github_api=https://api.github.com
8auth_header="Authorization: token $github_api_token"
7mkdir -p $folder 9mkdir -p $folder
8index=$folder/index 10index=$folder/index
9 11
12# get the list of gists
10_update() { 13_update() {
11 # get the list of gists 14 curl -s -H "$auth_header" $github_api/users/$user/gists |\
12 curl -s -H "Authorization: token $github_api_token" https://api.github.com/users/$user/gists |\
13 jq '.[] | "\( .html_url ) \(.files | keys | length) \( .description )"' |\ 15 jq '.[] | "\( .html_url ) \(.files | keys | length) \( .description )"' |\
14 tr -d '"' | tac | nl |\ 16 tr -d '"' | tac | nl |\
15 while read line_num link file_num description; do 17 while read line_num link file_num description; do
@@ -17,8 +19,8 @@ _update() {
17 done | tee $index 19 done | tee $index
18} 20}
19 21
22# clone repos which are not in the local
20_sync_repos() { 23_sync_repos() {
21 # clone repos which are not in the local machine
22 comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ 24 comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \
23 <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ 25 <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\
24 xargs -I{} git clone git@github.com:{}.git $folder/{} 26 xargs -I{} git clone git@github.com:{}.git $folder/{}
@@ -32,10 +34,11 @@ _goto_gist() {
32 34
33_delete_gist() { 35_delete_gist() {
34 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') 36 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##')
35 curl -X DELETE -s -H "Authorization: token $github_api_token" https://api.github.com/gists/$GIST_ID 37 curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID
36 _update 38 _update
37} 39}
38 40
41# remove repos which are not in user gists anymore
39_clean_repos() { 42_clean_repos() {
40 comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ 43 comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \
41 <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ 44 <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\
@@ -44,9 +47,10 @@ _clean_repos() {
44 47
45_show_detail() { 48_show_detail() {
46 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') 49 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##')
47 curl -s -H "Authorization: token $github_api_token" https://api.github.com/gists/$GIST_ID 50 curl -s -H "$auth_header" $github_api/gists/$GIST_ID
48} 51}
49 52
53# create a new gist with a file and description
50_create_gist() { 54_create_gist() {
51 FILE=$(basename $1) 55 FILE=$(basename $1)
52 56
@@ -57,9 +61,14 @@ _create_gist() {
57 }, 61 },
58 description: ($DESC) 62 description: ($DESC)
59 }' -R -s $1 |\ 63 }' -R -s $1 |\
60 curl -s -H "Authorization: token $github_api_token" \ 64 curl -s -H "$auth_header" --data @- $github_api/gists
61 --data @- \ 65}
62 https://api.github.com/gists 66
67_edit_gist() {
68 GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##')
69
70 jq -n --arg DESC "$2" '{ description: ($DESC) }' |\
71 curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID
63} 72}
64 73
65if [[ $# -eq 0 ]]; then 74if [[ $# -eq 0 ]]; then
@@ -69,7 +78,10 @@ fi
69 78
70case "$1" in 79case "$1" in
71 create | c) 80 create | c)
72 _create_gist $2 "$3" 81 _create_gist "$2" "$3"
82 ;;
83 edit | e)
84 _edit_gist "$2" "$3"
73 ;; 85 ;;
74 update | u) 86 update | u)
75 _update 87 _update
@@ -78,15 +90,15 @@ case "$1" in
78 _sync_repos 90 _sync_repos
79 ;; 91 ;;
80 detail | d) 92 detail | d)
81 _show_detail $2 93 _show_detail "$2"
82 ;; 94 ;;
83 delete | D) 95 delete | D)
84 _delete_gist $2 96 _delete_gist "$2"
85 ;; 97 ;;
86 clean | C) 98 clean | C)
87 _clean_repos 99 _clean_repos
88 ;; 100 ;;
89 *) 101 *)
90 _goto_gist $1 102 _goto_gist "$1"
91 ;; 103 ;;
92esac 104esac