diff options
author | typebrook <typebrook@gmail.com> | 2019-12-25 12:10:48 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2019-12-25 12:10:48 +0800 |
commit | c4b487095fae8eff858ccf7e5d0958652c78b92a (patch) | |
tree | d95ef79d04092d2375d2f312a1f5362f374a6f29 /scripts | |
parent | 28160b5e8d8b75a99518c54f64da438cee4d1cdd (diff) |
update
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/gist | 36 |
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) | |||
4 | user=typebrook | 4 | user=typebrook |
5 | folder=~/git/gist | 5 | folder=~/git/gist |
6 | 6 | ||
7 | github_api=https://api.github.com | ||
8 | auth_header="Authorization: token $github_api_token" | ||
7 | mkdir -p $folder | 9 | mkdir -p $folder |
8 | index=$folder/index | 10 | index=$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 | ||
65 | if [[ $# -eq 0 ]]; then | 74 | if [[ $# -eq 0 ]]; then |
@@ -69,7 +78,10 @@ fi | |||
69 | 78 | ||
70 | case "$1" in | 79 | case "$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 | ;; |
92 | esac | 104 | esac |