diff options
-rwxr-xr-x | scripts/gist | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/scripts/gist b/scripts/gist index f815e3e..558b659 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -15,14 +15,16 @@ _update() { | |||
15 | while read line_num link file_num description; do | 15 | while read line_num link file_num description; do |
16 | echo $line_num $link $file_num $(echo $description | cut -c -70) | 16 | echo $line_num $link $file_num $(echo $description | cut -c -70) |
17 | done | tee $index | 17 | done | tee $index |
18 | } | ||
18 | 19 | ||
20 | _sync_repos() { | ||
19 | # clone repos which are not in the local machine | 21 | # clone repos which are not in the local machine |
20 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 22 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ |
21 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ | 23 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ |
22 | xargs -I{} git clone git@github.com:{}.git $folder/{} | 24 | xargs -I{} git clone git@github.com:{}.git $folder/{} |
23 | } | 25 | } |
24 | 26 | ||
25 | _go_to_gist() { | 27 | _goto_gist() { |
26 | GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') | 28 | GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') |
27 | echo this gist is at $folder/$GIST_ID | 29 | echo this gist is at $folder/$GIST_ID |
28 | cd $folder/$GIST_ID && tig --all 2> /dev/null | 30 | cd $folder/$GIST_ID && tig --all 2> /dev/null |
@@ -34,15 +36,21 @@ _delete_gist() { | |||
34 | _update | 36 | _update |
35 | } | 37 | } |
36 | 38 | ||
39 | _clean_repos() { | ||
40 | comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | ||
41 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ | ||
42 | xargs -I{} rm -rf $folder/{} | ||
43 | } | ||
44 | |||
37 | _show_detail() { | 45 | _show_detail() { |
38 | GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') | 46 | GIST_ID=$(cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##') |
39 | curl -s -H "Authorization: token $github_api_token" https://api.github.com/gists/$GIST_ID | 47 | curl -s -H "Authorization: token $github_api_token" https://api.github.com/gists/$GIST_ID |
40 | } | 48 | } |
41 | 49 | ||
42 | _create() { | 50 | _create_gist() { |
43 | FILE=$(basename $1) | 51 | FILE=$(basename $1) |
44 | 52 | ||
45 | jq --arg FILE "$FILE" --arg DESC ""$2"" '. as $content | { | 53 | jq --arg FILE "$FILE" --arg DESC "$2" '. as $content | { |
46 | public: true, | 54 | public: true, |
47 | files: { | 55 | files: { |
48 | ($FILE): {content: $content} | 56 | ($FILE): {content: $content} |
@@ -61,18 +69,24 @@ fi | |||
61 | 69 | ||
62 | case "$1" in | 70 | case "$1" in |
63 | create | c) | 71 | create | c) |
64 | _create $2 $3 | 72 | _create_gist $2 "$3" |
65 | ;; | 73 | ;; |
66 | update | u) | 74 | update | u) |
67 | _update | 75 | _update |
68 | ;; | 76 | ;; |
77 | sync | s) | ||
78 | _sync_repos | ||
79 | ;; | ||
69 | detail | d) | 80 | detail | d) |
70 | _show_detail $2 | 81 | _show_detail $2 |
71 | ;; | 82 | ;; |
72 | delete | D) | 83 | delete | D) |
73 | _delete_gist $2 | 84 | _delete_gist $2 |
74 | ;; | 85 | ;; |
86 | clean | C) | ||
87 | _clean_repos | ||
88 | ;; | ||
75 | *) | 89 | *) |
76 | _go_to_gist $2 | 90 | _goto_gist $1 |
77 | ;; | 91 | ;; |
78 | esac | 92 | esac |