diff options
| author | typebrook <typebrook@gmail.com> | 2020-01-15 18:39:36 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-01-15 18:39:36 +0800 |
| commit | 0078e7e9e1e1ab46833cb108df11dd4f4aa8c4b9 (patch) | |
| tree | 5b4b3798a98c28452443191e42e91cbee358ae2e | |
| parent | 8dc3ab646f1afa93fdcf7f8e61d0385ac0c8c8d3 (diff) | |
update
| -rwxr-xr-x | gist | 34 |
1 files changed, 24 insertions, 10 deletions
| @@ -1,12 +1,12 @@ | |||
| 1 | #!/usr/bin/env bash | 1 | #!/usr/bin/env bash |
| 2 | # | 2 | # |
| 3 | # Author: Hsieh Chin Fan | 3 | # Author: Hsieh Chin Fan (typebrook) |
| 4 | # License: MIT | 4 | # License: MIT |
| 5 | # https://gist.github.com/typebrook/b0d2e7e67aa50298fdf8111ae7466b56 | 5 | # https://gist.github.com/typebrook/b0d2e7e67aa50298fdf8111ae7466b56 |
| 6 | # | 6 | # |
| 7 | # | 7 | # |
| 8 | # This script host your gists as local Github repo | 8 | # This script host your gists as local Github repo |
| 9 | # It works with jq and curl, which are easy to get in most cases | 9 | # It works under GNU with jq and curl, both are easy to get in most cases |
| 10 | # | 10 | # |
| 11 | # Use the following commands to manage your gists: | 11 | # Use the following commands to manage your gists: |
| 12 | # | 12 | # |
| @@ -38,7 +38,11 @@ | |||
| 38 | # * clean removed gists in local | 38 | # * clean removed gists in local |
| 39 | # gist [clean | C] | 39 | # gist [clean | C] |
| 40 | 40 | ||
| 41 | # * show this help message | ||
| 42 | # gist [help | h] | ||
| 43 | |||
| 41 | # define your environmemnts here | 44 | # define your environmemnts here |
| 45 | # TODO support auth prompt | ||
| 42 | #------------------- | 46 | #------------------- |
| 43 | github_api_token=$(cat $SETTING_DIR/tokens/github) | 47 | github_api_token=$(cat $SETTING_DIR/tokens/github) |
| 44 | user=typebrook | 48 | user=typebrook |
| @@ -59,14 +63,14 @@ _show_list() { | |||
| 59 | } | 63 | } |
| 60 | 64 | ||
| 61 | # get the list of gists | 65 | # get the list of gists |
| 66 | # TODO support secret gist | ||
| 62 | _update() { | 67 | _update() { |
| 63 | curl -s -H "$auth_header" $github_api/users/$user/gists |\ | 68 | curl -s -H "$auth_header" $github_api/users/$user/gists |\ |
| 64 | tee jojo |\ | ||
| 65 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ | 69 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ |
| 66 | tac | nl |\ | 70 | tac | nl |\ |
| 67 | while read line_num link blobs file_num comment_num description; do | 71 | while read line_num link file_url_array file_num comment_num description; do |
| 68 | blob_code=$(echo $blobs | jq -r '.[]' | sed -r 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 72 | blob_code=$(echo $file_url_array | jq -r '.[]' | sed -r 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') |
| 69 | echo $line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -80) | tr -d '"' | 73 | echo $line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -70) | tr -d '"' |
| 70 | done > $index && \ | 74 | done > $index && \ |
| 71 | _show_list | 75 | _show_list |
| 72 | } | 76 | } |
| @@ -88,6 +92,7 @@ _sync_repos() { | |||
| 88 | [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] && \ | 92 | [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] && \ |
| 89 | git pull | 93 | git pull |
| 90 | done | 94 | done |
| 95 | echo Everything is fine! | ||
| 91 | } | 96 | } |
| 92 | 97 | ||
| 93 | _gist_id() { | 98 | _gist_id() { |
| @@ -100,7 +105,7 @@ _goto_gist() { | |||
| 100 | echo Not a valid gist number: $1 | 105 | echo Not a valid gist number: $1 |
| 101 | echo Use the number in the first column instead: | 106 | echo Use the number in the first column instead: |
| 102 | echo | 107 | echo |
| 103 | show_list | 108 | _show_list |
| 104 | return 0 | 109 | return 0 |
| 105 | fi | 110 | fi |
| 106 | 111 | ||
| @@ -119,9 +124,12 @@ _delete_gist() { | |||
| 119 | _clean_repos() { | 124 | _clean_repos() { |
| 120 | comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 125 | comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ |
| 121 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ | 126 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ |
| 122 | xargs -I{} rm -rf $folder/{} | 127 | while read dir; do |
| 128 | mv $folder/$dir /tmp && echo move $folder/$dir to /tmp | ||
| 129 | done | ||
| 123 | } | 130 | } |
| 124 | 131 | ||
| 132 | # TODO star count | ||
| 125 | _show_detail() { | 133 | _show_detail() { |
| 126 | GIST_ID=$(_gist_id $1) | 134 | GIST_ID=$(_gist_id $1) |
| 127 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ | 135 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ |
| @@ -132,7 +140,6 @@ _show_detail() { | |||
| 132 | } | 140 | } |
| 133 | 141 | ||
| 134 | # create a new gist with files | 142 | # create a new gist with files |
| 135 | # TODO support folder of files | ||
| 136 | _create_gist() { | 143 | _create_gist() { |
| 137 | echo -n 'description: ' | 144 | echo -n 'description: ' |
| 138 | read DESC | 145 | read DESC |
| @@ -142,7 +149,7 @@ _create_gist() { | |||
| 142 | FILE=$(basename $file) | 149 | FILE=$(basename $file) |
| 143 | jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file | 150 | jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file |
| 144 | done |\ | 151 | done |\ |
| 145 | jq -s --arg DESC "$DESC" '{ | 152 | jq --slurp --arg DESC "$DESC" '{ |
| 146 | public: true, | 153 | public: true, |
| 147 | files: add, | 154 | files: add, |
| 148 | description: ($DESC) | 155 | description: ($DESC) |
| @@ -160,6 +167,10 @@ _edit_gist() { | |||
| 160 | _update | 167 | _update |
| 161 | } | 168 | } |
| 162 | 169 | ||
| 170 | _help_message() { | ||
| 171 | sed -r -n ' /^$/ q; 8,$ s/^#//p' $0 | ||
| 172 | } | ||
| 173 | |||
| 163 | case "$1" in | 174 | case "$1" in |
| 164 | "") | 175 | "") |
| 165 | _show_list | 176 | _show_list |
| @@ -186,6 +197,9 @@ case "$1" in | |||
| 186 | clean | C) | 197 | clean | C) |
| 187 | _clean_repos | 198 | _clean_repos |
| 188 | ;; | 199 | ;; |
| 200 | help | h) | ||
| 201 | _help_message | ||
| 202 | ;; | ||
| 189 | *) | 203 | *) |
| 190 | _goto_gist "$1" | 204 | _goto_gist "$1" |
| 191 | ;; | 205 | ;; |