diff options
| author | typebrook <typebrook@gmail.com> | 2020-02-09 20:43:56 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-02-09 20:43:56 +0800 |
| commit | fd110204d35f4d66a0deb9dadf74d81603f59a23 (patch) | |
| tree | f94f6af263a9486d87624075d8ad72845b716c8c | |
| parent | 62e660aae8d101c5b7b0d3d7b576eeb3ac1d786a (diff) | |
update
| -rwxr-xr-x | gist | 305 |
1 files changed, 186 insertions, 119 deletions
| @@ -8,45 +8,139 @@ | |||
| 8 | # Description: Manage your gists with git and Github API v3 | 8 | # Description: Manage your gists with git and Github API v3 |
| 9 | # Usage: gist [command] [<args>] | 9 | # Usage: gist [command] [<args>] |
| 10 | # | 10 | # |
| 11 | # [star | s] list your gists with format below, star for your starred gists: | 11 | # [star | s] List your gists with format below, star for your starred gists: |
| 12 | # [index_of_gist] [url] [file_num] [comment_num] [short description] | 12 | # [index_of_gist] [url] [file_num] [comment_num] [short description] |
| 13 | # update, u [star | s] update the local list of your gists, star for your starred gists | 13 | # fetch, f [star | s] Update the local list of your gists, star for your starred gists |
| 14 | # <index_of_gist> [--no-action] show the path of local gist repo and do custom actions | 14 | # <index_of_gist> [--no-action] Show the path of local gist repo and do custom actions |
| 15 | # new, n [-d | --desc <description>] [-p] <files>... create a new gist with files | 15 | # new, n [-d | --desc <description>] [-p] <files>... create a new gist with files |
| 16 | # new, n [-d | --desc <description>] [-p] [-f | --file <file_name>] create a new gist from STDIN | 16 | # new, n [-d | --desc <description>] [-p] [-f | --file <file_name>] create a new gist from STDIN |
| 17 | # detail, d <index_of_gist> show the detail of a gist | 17 | # detail, d <index_of_gist> Show the detail of a gist |
| 18 | # edit, e <index_of_gist> edit a gist's description | 18 | # edit, e <index_of_gist> Edit a gist's description |
| 19 | # delete, D <index_of_gist>... delete a gist | 19 | # delete, D <index_of_gist>... Delete a gist |
| 20 | # clean, C clean removed gists in local | 20 | # clean, C Clean removed gists in local |
| 21 | # config, c [token | user | folder | auto_sync | EDITOR | action [value] ] do configuration | 21 | # config, c [token | user | folder | auto_sync | EDITOR | action [value] ] Do configuration |
| 22 | # user, U <user> get gists from a given Github user | 22 | # user, U <user> Get gists from a given Github user |
| 23 | # grep, g <pattern> grep gists by a given pattern | 23 | # grep, g <pattern> Grep gists by a given pattern |
| 24 | # github, G <index_of_gist> Import this gist as a new Github repo | 24 | # push, p <pattern> Push changes by git (well, better to make commit by youself) |
| 25 | # push, p <pattern> push changes by git | 25 | # github, G <index_of_gist> Import selected gist as a new Github repo |
| 26 | # help, h show this help message | 26 | # help, h Show this help message |
| 27 | # version Get the tool version | ||
| 28 | # update Update Bash-Snippet Tools | ||
| 27 | # | 29 | # |
| 28 | # Example: | 30 | # Example: |
| 29 | # gist (Show your gists) | 31 | # gist (Show your gists) |
| 30 | # gist update (update the list of gists from github.com) | 32 | # gist update (update the list of gists from github.com) |
| 31 | # gist 3 (show the repo path of your 3rd gist, and do custom actions) | 33 | # gist 3 (show the repo path of your 3rd gist, and do custom actions) |
| 32 | # gist 3 --no-action (show the repo path of your 3rd gist, and do not perform actions) | 34 | # gist 3 --no-action (show the repo path of your 3rd gist, and do not perform actions) |
| 33 | # gist new foo --desc bar (create a new gist with file and description) | 35 | # gist new --desc bar foo (create a new gist with file and description) |
| 34 | # | 36 | # |
| 35 | # Since now a gist is a local cloned repo | 37 | # Since now a gist is a local cloned repo |
| 36 | # It is your business to do git commit and git push | 38 | # It is your business to do git commit and git push |
| 37 | 39 | ||
| 38 | # TODO test on bats, mac and remote machine | 40 | # TODO test on bats, mac and remote machine |
| 39 | # TODO completion | 41 | currentVersion="1.23.0" |
| 40 | # FIXME mac: tac=tail -r | 42 | configuredClient="" |
| 43 | |||
| 44 | GITHUB_API=https://api.github.com | ||
| 45 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config | ||
| 46 | |||
| 47 | folder=~/gist && mkdir -p $folder | ||
| 48 | action="${EDITOR:-vi} ." | ||
| 49 | auto_sync=true # automatically clone the gist repo | ||
| 41 | 50 | ||
| 42 | # Shell configuration | 51 | # Shell configuration |
| 43 | set -o pipefail | 52 | set -o pipefail |
| 44 | [ "$TRACE" ] && set -x | 53 | [ "$TRACE" ] && set -x |
| 54 | [ $(uname) == 'Darwin' ] && alias tac='tail -r' | ||
| 45 | trap 'rm -f "$http_data" "$tmp_file"' EXIT | 55 | trap 'rm -f "$http_data" "$tmp_file"' EXIT |
| 46 | 56 | ||
| 47 | GITHUB_API=https://api.github.com | 57 | # This function determines which http get tool the system has installed and returns an error if there isnt one |
| 48 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config | 58 | getConfiguredClient() { |
| 49 | configuredClient="" | 59 | if command -v curl &>/dev/null; then |
| 60 | configuredClient="curl" | ||
| 61 | elif command -v wget &>/dev/null; then | ||
| 62 | configuredClient="wget" | ||
| 63 | elif command -v http &>/dev/null; then | ||
| 64 | configuredClient="httpie" | ||
| 65 | else | ||
| 66 | echo "Error: This tool requires either curl, wget, or httpie to be installed." >&2 | ||
| 67 | return 1 | ||
| 68 | fi | ||
| 69 | } | ||
| 70 | |||
| 71 | # Allows to call the users configured client without if statements everywhere | ||
| 72 | http_method() { | ||
| 73 | local METHOD=$1; shift | ||
| 74 | case "$configuredClient" in | ||
| 75 | curl) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" | ||
| 76 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="--data" | ||
| 77 | curl -X $METHOD -A curl -s $extra "$header" $extra2 @$http_data "$@" ;; | ||
| 78 | wget) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" | ||
| 79 | [[ $METHOD =~ (POST|PATCH) ]] && extra2='--body-file' | ||
| 80 | wget --method=$METHOD -qO- $extra "$header" $extra2 $http_data "$@" ;; | ||
| 81 | httpie) [[ -n $token ]] && header="Authorization:token $token" | ||
| 82 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="@$http_data" | ||
| 83 | http -b $METHOD "$@" "$header" $extra2 ;; | ||
| 84 | esac | ||
| 85 | } | ||
| 86 | |||
| 87 | httpGet(){ | ||
| 88 | http_method GET "$@" | ||
| 89 | } | ||
| 90 | |||
| 91 | # parse JSON from STDIN with string of commands | ||
| 92 | _process_json() { | ||
| 93 | PYTHONIOENCODING=utf-8 \ | ||
| 94 | python -c "from __future__ import print_function; import sys, json; $1" | ||
| 95 | return "$?" | ||
| 96 | } | ||
| 97 | |||
| 98 | checkInternet() { | ||
| 99 | httpGet github.com 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query github with a get request | ||
| 100 | } | ||
| 101 | |||
| 102 | update() { | ||
| 103 | # Author: Alexander Epstein https://github.com/alexanderepstein | ||
| 104 | # Update utility version 2.2.0 | ||
| 105 | # To test the tool enter in the defualt values that are in the examples for each variable | ||
| 106 | repositoryName="Bash-Snippets" #Name of repostiory to be updated ex. Sandman-Lite | ||
| 107 | githubUserName="alexanderepstein" #username that hosts the repostiory ex. alexanderepstein | ||
| 108 | nameOfInstallFile="install.sh" # change this if the installer file has a different name be sure to include file extension if there is one | ||
| 109 | latestVersion=$(httpGet https://api.github.com/repos/$githubUserName/$repositoryName/tags | grep -Eo '"name":.*?[^\\]",'| head -1 | grep -Eo "[0-9.]+" ) #always grabs the tag without the v option | ||
| 110 | |||
| 111 | if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]]; then | ||
| 112 | echo "Error: update utility has not been configured correctly." >&2 | ||
| 113 | exit 1 | ||
| 114 | elif [[ $latestVersion == "" ]]; then | ||
| 115 | echo "Error: no active internet connection" >&2 | ||
| 116 | exit 1 | ||
| 117 | else | ||
| 118 | if [[ "$latestVersion" != "$currentVersion" ]]; then | ||
| 119 | echo "Version $latestVersion available" | ||
| 120 | echo -n "Do you wish to update $repositoryName [Y/n]: " | ||
| 121 | read -r answer | ||
| 122 | if [[ "$answer" == [Yy] ]]; then | ||
| 123 | cd ~ || { echo 'Update Failed'; exit 1; } | ||
| 124 | if [[ -d ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi | ||
| 125 | echo -n "Downloading latest version of: $repositoryName." | ||
| 126 | git clone -q "https://github.com/$githubUserName/$repositoryName" && touch .BSnippetsHiddenFile || { echo "Failure!"; exit 1; } & | ||
| 127 | while [ ! -f .BSnippetsHiddenFile ]; do { echo -n "."; sleep 2; };done | ||
| 128 | rm -f .BSnippetsHiddenFile | ||
| 129 | echo "Success!" | ||
| 130 | cd $repositoryName || { echo 'Update Failed'; exit 1; } | ||
| 131 | git checkout "v$latestVersion" 2> /dev/null || git checkout "$latestVersion" 2> /dev/null || echo "Couldn't git checkout to stable release, updating to latest commit." | ||
| 132 | chmod a+x install.sh #this might be necessary in your case but wasnt in mine. | ||
| 133 | ./$nameOfInstallFile "update" || exit 1 | ||
| 134 | cd .. | ||
| 135 | rm -r -f $repositoryName || { echo "Permissions Error: update succesfull but cannot delete temp files located at ~/$repositoryName delete this directory with sudo"; exit 1; } | ||
| 136 | else | ||
| 137 | exit 1 | ||
| 138 | fi | ||
| 139 | else | ||
| 140 | echo "$repositoryName is already the latest version" | ||
| 141 | fi | ||
| 142 | fi | ||
| 143 | } | ||
| 50 | 144 | ||
| 51 | # handle configuration cases | 145 | # handle configuration cases |
| 52 | _configure() { | 146 | _configure() { |
| @@ -66,7 +160,7 @@ _configure() { | |||
| 66 | fi | 160 | fi |
| 67 | 161 | ||
| 68 | umask 0077 && touch $CONFIG | 162 | umask 0077 && touch $CONFIG |
| 69 | sed -i'' -e "/^$1=/ d" $CONFIG && [[ "$target" =~ [^=]$ ]] && echo $target >> $CONFIG | 163 | sed -i'' -e "/^$1=/ d" $CONFIG && [[ -n "$target" ]] && echo $target >> $CONFIG |
| 70 | cat $CONFIG | 164 | cat $CONFIG |
| 71 | } | 165 | } |
| 72 | 166 | ||
| @@ -97,8 +191,9 @@ _ask_token() { | |||
| 97 | 191 | ||
| 98 | # check configuration is fine with user setting | 192 | # check configuration is fine with user setting |
| 99 | _validate_config(){ | 193 | _validate_config(){ |
| 100 | source $CONFIG 2> /dev/null || true | 194 | source $CONFIG 2> /dev/null |
| 101 | if [[ ! -e $CONFIG || -z $user ]]; then | 195 | [[ $1 =~ ^(c|config|h|help|u|user|update|version) ]] && return 0 |
| 196 | if [[ -z $user ]]; then | ||
| 102 | echo 'Hi fellow! To access your gists, I need your Github username' | 197 | echo 'Hi fellow! To access your gists, I need your Github username' |
| 103 | echo "Also a personal token with scope which allows "gist"!'" | 198 | echo "Also a personal token with scope which allows "gist"!'" |
| 104 | echo | 199 | echo |
| @@ -108,7 +203,7 @@ _validate_config(){ | |||
| 108 | echo 'To create/edit/delete a gist, a token is needed' | 203 | echo 'To create/edit/delete a gist, a token is needed' |
| 109 | return 1 | 204 | return 1 |
| 110 | fi | 205 | fi |
| 111 | elif [[ -z $token && $1 =~ ^(u|update)$ && $2 =~ ^(s|star) ]]; then | 206 | elif [[ -z $token && $1 =~ ^(f|fetch)$ && $2 =~ ^(s|star) ]]; then |
| 112 | if ! (_ask_token); then | 207 | if ! (_ask_token); then |
| 113 | echo 'To get user starred gists, a token is needed' | 208 | echo 'To get user starred gists, a token is needed' |
| 114 | return 1 | 209 | return 1 |
| @@ -121,46 +216,27 @@ _apply_config() { | |||
| 121 | _validate_config "$@" || return 1 | 216 | _validate_config "$@" || return 1 |
| 122 | 217 | ||
| 123 | AUTH_HEADER="Authorization: token $token" | 218 | AUTH_HEADER="Authorization: token $token" |
| 124 | [[ -z "$action" ]] && action="${EDITOR:-vi} ." | ||
| 125 | [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder | ||
| 126 | INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX | 219 | INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX |
| 127 | } | 220 | } |
| 128 | 221 | ||
| 129 | # This function determines which http get tool the system has installed and returns an error if there isnt one | 222 | _check_repo_status() { |
| 130 | getConfiguredClient() { | 223 | if [[ ! -d $1 ]]; then |
| 131 | if command -v curl &>/dev/null; then | 224 | if $auto_sync; then |
| 132 | configuredClient="curl" | 225 | echo "\e[32m[cloning]\e[0m"; |
| 133 | elif command -v wget &>/dev/null; then | 226 | else |
| 134 | configuredClient="wget" | 227 | echo "\e[32m[Not cloned yet]\e[0m"; |
| 135 | elif command -v http &>/dev/null; then | 228 | fi |
| 136 | configuredClient="httpie" | 229 | else |
| 137 | elif command -v fetch &>/dev/null; then | 230 | cd $1 |
| 138 | configuredClient="fetch" | 231 | if [[ -n $(git status --short) ]] &>/dev/null; then |
| 139 | else | 232 | echo "\e[36m[working]\e[0m" |
| 140 | echo "Error: This tool requires either curl, wget, httpie or fetch to be installed." >&2 | 233 | else |
| 141 | return 1 | 234 | [[ $(_blob_code $1) != $2 ]] 2>/dev/null && echo "\e[31m[outdated]\e[0m" |
| 142 | fi | 235 | [[ -n $(git cherry) ]] 2>/dev/null && echo "\e[31m[ahead]\e[0m" |
| 143 | } | 236 | fi |
| 144 | 237 | fi | |
| 145 | # Allows to call the users configured client without if statements everywhere | ||
| 146 | http_method() { | ||
| 147 | local METHOD=$1; shift | ||
| 148 | case "$configuredClient" in | ||
| 149 | curl) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" | ||
| 150 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="--data" | ||
| 151 | curl -X $METHOD -A curl -s $extra "$header" $extra2 @$http_data "$@" ;; | ||
| 152 | wget) [[ -n $token ]] && local extra="--header" local header="Authorization: token $token" | ||
| 153 | [[ $METHOD =~ (POST|PATCH) ]] && extra2='--body-file' | ||
| 154 | wget --method=$METHOD -qO- $extra "$header" $extra2 $http_data "$@" ;; | ||
| 155 | httpie) [[ -n $token ]] && header="Authorization:token $token" | ||
| 156 | [[ $METHOD =~ (POST|PATCH) ]] && extra2="@$http_data" | ||
| 157 | http -b $METHOD "$@" "$header" $extra2 ;; | ||
| 158 | # TODO add other methods | ||
| 159 | fetch) fetch -q "$@" ;; | ||
| 160 | esac | ||
| 161 | } | 238 | } |
| 162 | 239 | ||
| 163 | # TODO Split into 2 funcs for filter and display | ||
| 164 | # Show the list of gist, but not updated time | 240 | # Show the list of gist, but not updated time |
| 165 | _show_list() { | 241 | _show_list() { |
| 166 | if [[ ! -e $INDEX ]]; then | 242 | if [[ ! -e $INDEX ]]; then |
| @@ -168,28 +244,22 @@ _show_list() { | |||
| 168 | echo ' gist update' | 244 | echo ' gist update' |
| 169 | return 0 | 245 | return 0 |
| 170 | fi | 246 | fi |
| 171 | [[ -z $1 ]] && local filter='/^ *s/ d; /^$/ d' | 247 | local filter='/^ *s/ d; /^$/ d' |
| 172 | [[ $1 == "s" ]] && local filter='/^ *[^ s]/ d; /^$/ d' | 248 | [[ $mark == "s" ]] && filter='/^ *[^ s]/ d; /^$/ d' |
| 173 | 249 | ||
| 174 | while read index link blob_code file_num comment_num author description; do | 250 | sed -e "$filter" $INDEX \ |
| 251 | | while read index link blob_code file_num comment_num author description; do | ||
| 175 | [[ $1 == "s" ]] && local name=$author | 252 | [[ $1 == "s" ]] && local name=$author |
| 176 | local repo=$folder/$(echo $link | sed 's#.*/##') | 253 | local repo=$folder/$(echo $link | sed 's#.*/##') |
| 177 | local occupy=0 | 254 | local extra=$(_check_repo_status $repo $blob_code) |
| 178 | local extra="$file_num $comment_num" | 255 | [[ -z $extra ]] && extra="$file_num $comment_num" |
| 179 | 256 | ||
| 180 | # if repo is not yet cloned, show green message "Not cloned yet" | 257 | echo -e "$(printf "% 3s" $index)" $link $name $extra $description \ |
| 181 | [[ ! -d $repo ]] && extra="\e[32m[Not cloned yet]\e[0m" && occupy=16 | 258 | | cut -c -$(tput cols) |
| 182 | # if there are some changes in git index or working directory, show blue message "working" | 259 | done |
| 183 | [[ -n $(cd $repo && git status --short) ]] 2>/dev/null && extra="\e[36m[working]\e[0m" && occupy=9 | ||
| 184 | # if there files are different, show red message "outdated" | ||
| 185 | [[ $(_blob_code $repo) != $blob_code ]] 2>/dev/null && extra="\e[31m[outdated]\e[0m" && occupy=10 | ||
| 186 | # if there is a commit not yet push, show red message "ahead" | ||
| 187 | [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 | ||
| 188 | 260 | ||
| 189 | echo -e "$(printf "% 3s" $index)" $link $name $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) | 261 | $hint && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ |
| 190 | done < $INDEX \ | 262 | || return 0 |
| 191 | | sed "$filter" | ||
| 192 | echo -e '\nrun "gist help" or "gist h" for more details' > /dev/tty | ||
| 193 | } | 263 | } |
| 194 | 264 | ||
| 195 | # TODO support filenames, file contents | 265 | # TODO support filenames, file contents |
| @@ -197,7 +267,6 @@ _grep_content() { | |||
| 197 | _show_list | grep -i $1 | 267 | _show_list | grep -i $1 |
| 198 | } | 268 | } |
| 199 | 269 | ||
| 200 | # TODO support filenames, file contents | ||
| 201 | _import_to_github() { | 270 | _import_to_github() { |
| 202 | _gist_id $1 | 271 | _gist_id $1 |
| 203 | echo put the folowing URL into webpage: | 272 | echo put the folowing URL into webpage: |
| @@ -211,15 +280,8 @@ _push_to_remote() { | |||
| 211 | && git commit --allow-empty-message -m '' && git push origin master | 280 | && git commit --allow-empty-message -m '' && git push origin master |
| 212 | } | 281 | } |
| 213 | 282 | ||
| 214 | # parse JSON from STDIN with string of commands | 283 | _parse_gists() { |
| 215 | AccessJsonElement() { | 284 | _process_json ' |
| 216 | PYTHONIOENCODING=utf-8 \ | ||
| 217 | python -c "from __future__ import print_function; import sys, json; $1" | ||
| 218 | return "$?" | ||
| 219 | } | ||
| 220 | |||
| 221 | _handle_gists() { | ||
| 222 | echo ' | ||
| 223 | raw = json.load(sys.stdin) | 285 | raw = json.load(sys.stdin) |
| 224 | for gist in raw: | 286 | for gist in raw: |
| 225 | print(gist["html_url"], end=" ") | 287 | print(gist["html_url"], end=" ") |
| @@ -233,63 +295,58 @@ for gist in raw: | |||
| 233 | } | 295 | } |
| 234 | 296 | ||
| 235 | # TODO check if a user has no gist | 297 | # TODO check if a user has no gist |
| 236 | # FIXME replace space with sed | ||
| 237 | # parse response from gists require | 298 | # parse response from gists require |
| 238 | _parse_response() { | 299 | _parse_response() { |
| 239 | tee a \ | 300 | _parse_gists \ |
| 240 | | AccessJsonElement "$(_handle_gists)" \ | 301 | | tac | sed -e 's/, /,/g' | nl -s' ' \ |
| 241 | | tee b \ | ||
| 242 | | tail -r | sed -e 's/, /,/g' | nl -s' ' \ | ||
| 243 | | tee ba \ | ||
| 244 | | while read index link file_url_array public file_num comment_num author description; do | 302 | | while read index link file_url_array public file_num comment_num author description; do |
| 245 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) | 303 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
| 246 | [[ $public == 'False' ]] && local mark=p | 304 | [[ $public == 'False' ]] && local mark=p |
| 247 | [[ -n $1 ]] && local index=$1 | 305 | [[ -n $1 ]] && local index=$1 |
| 248 | echo $mark$index $link $blob_code $file_num $comment_num $author $description | tr -d '"' | 306 | echo $mark$index $link $blob_code $file_num $comment_num $author $description | tr -d '"' |
| 249 | done | tee c | 307 | done |
| 250 | } | 308 | } |
| 251 | 309 | ||
| 252 | # TODO pagnation for more than 30 gists | 310 | # TODO pagnation for more than 30 gists |
| 253 | # TODO add files and date of a gist | 311 | # TODO add files and date of a gist |
| 254 | # get latest list of gists from Github API | 312 | # get latest list of gists from Github API |
| 255 | _update() { | 313 | _fetch_gists() { |
| 256 | echo "fetching $user's gists from $GITHUB_API..." | 314 | echo "fetching $user's gists from $GITHUB_API..." |
| 257 | echo | 315 | echo |
| 258 | local route="users/$user/gists" | 316 | local route="users/$user/gists" |
| 259 | local mark="" | ||
| 260 | local filter='/^[^s]/ d; /^$/ d' | 317 | local filter='/^[^s]/ d; /^$/ d' |
| 261 | if [[ "$1" =~ ^(star|s)$ ]];then | 318 | if [[ "$1" =~ ^(star|s)$ ]];then |
| 262 | route="gists/starred" | 319 | route="gists/starred" |
| 263 | mark="s" | 320 | local mark="s" |
| 264 | filter='/^[s]/ d; /^$/ d' | 321 | filter='/^[s]/ d; /^$/ d' |
| 265 | fi | 322 | fi |
| 266 | 323 | ||
| 267 | result=$(http_method GET $GITHUB_API/$route | _parse_response) | 324 | result=$(http_method GET $GITHUB_API/$route | mark=$mark _parse_response) |
| 268 | [[ -z $result ]] && echo Failed to update gists && return 1 | 325 | [[ -z $result ]] && echo Failed to update gists && return 1 |
| 269 | 326 | ||
| 270 | sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX | 327 | sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX |
| 271 | _show_list $mark | 328 | mark=$mark hint=true _show_list |
| 272 | 329 | ||
| 273 | if [[ $auto_sync != 'false' ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 330 | $auto_sync && (_sync_repos $1 > /dev/null 2>&1 &) |
| 274 | } | 331 | } |
| 275 | 332 | ||
| 276 | _query_user() { | 333 | _query_user() { |
| 277 | local route="users/$1/gists" | 334 | local route="users/$1/gists" |
| 278 | result=$(http_method GET $GITHUB_API/$route | _parse_response) | 335 | result=$(http_method GET $GITHUB_API/$route | _parse_response) |
| 279 | [[ -z $result ]] && echo Failed to update gists && return 1 | 336 | [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 |
| 280 | 337 | ||
| 281 | echo "$result" \ | 338 | echo "$result" \ |
| 282 | | while read index link blob_code file_num extra description; do | 339 | | while read index link blob_code file_num extra description; do |
| 283 | echo $link $file_num $extra $(echo $description | cut -c -70 ) | 340 | echo $link $file_num $extra $description | cut -c -$(tput cols) |
| 284 | done | 341 | done |
| 285 | } | 342 | } |
| 286 | 343 | ||
| 287 | _blob_code() { | 344 | _blob_code() { |
| 288 | cd $1 \ | 345 | cd $1 && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-' |
| 289 | && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-' | ||
| 290 | } | 346 | } |
| 291 | 347 | ||
| 292 | # update local git repos | 348 | # update local git repos |
| 349 | # TODO support HTTPS protocol | ||
| 293 | _sync_repos() { | 350 | _sync_repos() { |
| 294 | # clone repos which are not in the local | 351 | # clone repos which are not in the local |
| 295 | comm -13 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ | 352 | comm -13 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ |
| @@ -363,8 +420,8 @@ _clean_repos() { | |||
| 363 | } | 420 | } |
| 364 | 421 | ||
| 365 | # parse JSON from gist detail | 422 | # parse JSON from gist detail |
| 366 | _handle_gist() { | 423 | _parse_gist() { |
| 367 | echo ' | 424 | _process_json ' |
| 368 | raw = json.load(sys.stdin) | 425 | raw = json.load(sys.stdin) |
| 369 | print("site:", raw["html_url"]) | 426 | print("site:", raw["html_url"]) |
| 370 | print("description:", raw["description"]) | 427 | print("description:", raw["description"]) |
| @@ -375,12 +432,12 @@ print("updated_at:", raw["updated_at"]) | |||
| 375 | print("files:") | 432 | print("files:") |
| 376 | for file in raw["files"].keys(): | 433 | for file in raw["files"].keys(): |
| 377 | print(" ", file) | 434 | print(" ", file) |
| 378 | ' | 435 | ' |
| 379 | } | 436 | } |
| 380 | 437 | ||
| 381 | # equal to jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' | 438 | # equal to jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' |
| 382 | _handle_comment() { | 439 | _parse_comment() { |
| 383 | echo ' | 440 | _process_json ' |
| 384 | raw = json.load(sys.stdin); | 441 | raw = json.load(sys.stdin); |
| 385 | for comment in raw: | 442 | for comment in raw: |
| 386 | print() | 443 | print() |
| @@ -388,16 +445,16 @@ for comment in raw: | |||
| 388 | print("|", "created_at:", comment["created_at"]) | 445 | print("|", "created_at:", comment["created_at"]) |
| 389 | print("|", "updated_at:", comment["updated_at"]) | 446 | print("|", "updated_at:", comment["updated_at"]) |
| 390 | print("|", comment["body"]) | 447 | print("|", comment["body"]) |
| 391 | ' | 448 | ' |
| 392 | } | 449 | } |
| 393 | 450 | ||
| 394 | _show_detail() { | 451 | _show_detail() { |
| 395 | _gist_id $1 | 452 | _gist_id $1 |
| 396 | http_method GET $GITHUB_API/gists/$GIST_ID \ | 453 | http_method GET $GITHUB_API/gists/$GIST_ID \ |
| 397 | | AccessJsonElement "$(_handle_gist)" | 454 | | _parse_gist |
| 398 | 455 | ||
| 399 | http_method GET $GITHUB_API/gists/$GIST_ID/comments \ | 456 | http_method GET $GITHUB_API/gists/$GIST_ID/comments \ |
| 400 | | AccessJsonElement "$(_handle_comment)" | 457 | | _parse_comment |
| 401 | } | 458 | } |
| 402 | 459 | ||
| 403 | # set filename/description/permission for a new gist | 460 | # set filename/description/permission for a new gist |
| @@ -433,7 +490,7 @@ _new_file() { | |||
| 433 | } | 490 | } |
| 434 | 491 | ||
| 435 | _gist_body(){ | 492 | _gist_body(){ |
| 436 | echo " | 493 | _process_json " |
| 437 | import os.path | 494 | import os.path |
| 438 | files_json = {} | 495 | files_json = {} |
| 439 | files = sys.stdin.readline().split() | 496 | files = sys.stdin.readline().split() |
| @@ -455,7 +512,7 @@ _create_gist() { | |||
| 455 | http_data=$(mktemp) | 512 | http_data=$(mktemp) |
| 456 | 513 | ||
| 457 | echo -e "$files\n$description" \ | 514 | echo -e "$files\n$description" \ |
| 458 | | AccessJsonElement "$(_gist_body)" > $http_data \ | 515 | | _gist_body > $http_data \ |
| 459 | && http_method POST $GITHUB_API/gists \ | 516 | && http_method POST $GITHUB_API/gists \ |
| 460 | | sed -e '1 s/^/[/; $ s/$/]/' \ | 517 | | sed -e '1 s/^/[/; $ s/$/]/' \ |
| 461 | | _parse_response $(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) \ | 518 | | _parse_response $(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) \ |
| @@ -481,7 +538,7 @@ _edit_gist() { | |||
| 481 | http_data=$(mktemp) | 538 | http_data=$(mktemp) |
| 482 | echo { \"description\": \"$(echo $DESC | sed -e 's/"/\\"/g')\" } > $http_data | 539 | echo { \"description\": \"$(echo $DESC | sed -e 's/"/\\"/g')\" } > $http_data |
| 483 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ | 540 | http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ |
| 484 | && _update | 541 | && _fetch_gists |
| 485 | } | 542 | } |
| 486 | 543 | ||
| 487 | usage() { | 544 | usage() { |
| @@ -489,15 +546,17 @@ usage() { | |||
| 489 | } | 546 | } |
| 490 | 547 | ||
| 491 | _apply_config "$@" || exit 1 | 548 | _apply_config "$@" || exit 1 |
| 492 | getConfiguredClient | 549 | getConfiguredClient || exit 1 |
| 493 | if [[ $init ]]; then _update; exit 0; fi | 550 | if [[ $init ]]; then _fetch_gists; exit 0; fi |
| 494 | case "$1" in | 551 | case "$1" in |
| 495 | "") | 552 | "") |
| 496 | _show_list ;; | 553 | [[ -z "$hint" ]] && hint=true |
| 554 | hint=$hint _show_list ;; | ||
| 497 | star | s) | 555 | star | s) |
| 498 | _show_list s ;; | 556 | [[ -z "$hint" ]] && hint=true |
| 499 | update | u) | 557 | hint=$hint mark=s _show_list ;; |
| 500 | _update "$2" ;; | 558 | fetch | f) |
| 559 | _fetch_gists "$2" ;; | ||
| 501 | new | n) | 560 | new | n) |
| 502 | shift | 561 | shift |
| 503 | _create_gist "$@" ;; | 562 | _create_gist "$@" ;; |
| @@ -528,6 +587,14 @@ case "$1" in | |||
| 528 | push | p) | 587 | push | p) |
| 529 | shift | 588 | shift |
| 530 | _push_to_remote "$1" ;; | 589 | _push_to_remote "$1" ;; |
| 590 | version) | ||
| 591 | echo "Version $currentVersion" | ||
| 592 | exit 0 ;; | ||
| 593 | update) | ||
| 594 | checkInternet || exit 1 | ||
| 595 | update | ||
| 596 | exit 0 | ||
| 597 | ;; | ||
| 531 | help | h) | 598 | help | h) |
| 532 | usage ;; | 599 | usage ;; |
| 533 | *) | 600 | *) |